find date difference

find date difference

by Nitin Yadav -
Number of replies: 7
Hi

moodle stores date fields in the big int format, can anyone suggest how to find the difference between two date values

4 Aug 2009 is stored as 1249340400
5 Aug 2009 is stored as 1249426800

is there any date difference function difined somewhere?

Thanks,
Nitin
Average of ratings: -
In reply to Nitin Yadav

Re: find date difference

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Nitin,
Do you want a formula to calculate the difference between two Unix timestamp dates ... in days? in days and hours ? etc. Please be more specific.
Joseph
In reply to Joseph Rézeau

Re: find date difference

by Nitin Yadav -
yes please.

i need to calculate difference in days

Thanks,
Nitin
In reply to Nitin Yadav

Re: find date difference

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

You could use/adapt the function by Charles W. Reace, Jr. here.

If you have the start and the end dates in unix timestamp format, then instead of

$start = strtotime('2009-08-4 00:00:00');
$end = strtotime('2009-08-5 00:00:00');

just put the unix timestamps:

$start = strtotime('1249340400');
$end = strtotime('1249426800');

Please note that you may have to add one second to the end date. And if you are only interested in days, you'll have to modify the timeDiff() function accordingly.

Joseph

Average of ratings: Useful (1)
In reply to Nitin Yadav

Re: find date difference

by Hubert Chathi -
Given that a UNIX timestamp is just a number of seconds, you can just subtract one time from the other, divide by (24*60*60), and apply appropriate rounding.
Average of ratings: Useful (1)
In reply to Hubert Chathi

Re: find date difference

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Hubert, could you please tell us how you would "apply appropriate rounding" to a number of UNIX timestamp seconds?
TIA
Joseph
Average of ratings: Useful (1)
In reply to Joseph Rézeau

Re: find date difference

by Hubert Chathi -
When you divide the number of seconds by 24*60*60, you may get a fractional result. Depending on how you want to treat a fraction of a day, you may want to use, e.g. the PHP round, ceil, or floor functions. Or you could do something more complicated if, say, you wanted 23 hours to be counted as a full day, but 22 hours, 59 minutes, and 59 seconds as not a full day.
Average of ratings: Useful (1)