View Full Version : Difference between two datetime

06-01-2009, 09:12 AM
Hi All ,

I need to find out the difference between two date times like
$logged_time='2009-06-01 01:38:35';
$current_time='2009-06-01 07:48:45';

I need the differnce between $logged_time and $current_time

can any one help me ...


06-01-2009, 11:41 AM
Are you sure this is a Javascript question? The format of your variables suggest PHP.

06-01-2009, 01:14 PM
If you do need to handle ISO time formats with javascript, just remember that they refer to GMT time to seconds, and months are 1-12.

Javascript Dates are local and to milliseconds, and months range from 0-11.

A bulletproof method to convert ISO to a javascript date would have to include a switch for the
short versions that use year, week in year and day in week, and the year and day in year formats,
but this will handle the common long format you show:

Date.fromISO= function(s){
var i= 0, A= s.split(/\D+/);
if(!A[i]) A[i]= 0;
else A[i]= parseInt(A[i], 10);
return new Date(Date.UTC(A[0], A[1], A[2], A[3], A[4], A[5]));

var s1='2009-06-01 01:38:35';
var s2='2009-06-01 07:48:45';
alert((Date.fromISO(s2)-Date.fromISO(s1))/1000+' seconds');