Page 1 of 1
date and time difference function pre php 5.3
Posted: Wed Oct 14, 2009 4:25 am
by paulb
Hi,
I am trying to calculate the date and time difference between two values. I have searched the net for a simple method to calculate the difference but I can seem to find one!!! Ones which I have found dont seem to work!
The format of the date and time are of iso 8601 format. Like
2004-02-12T15:19:21+00:00
I tried using the one posted by date_diff in link:
http://uk.php.net/manual/en/function.date-diff.php
That doesnt seem to work given certain dates and times and whilst changing it is an option I can imagine it getting into a bit of beast!!!!
There currently is a dateTime diff method for php > 5.3 version but I have a requirement to stay with version 5.2
http://uk.php.net/manual/en/datetime.diff.php
Hope someone can help.
Re: date and time difference function pre php 5.3
Posted: Wed Oct 14, 2009 11:41 am
by paulb
Can someone please help with this?
Re: date and time difference function pre php 5.3
Posted: Wed Oct 14, 2009 11:47 am
by McInfo
Have you tried using
strtotime() and subtracting two timestamps to get the difference in seconds?
Edit: This post was recovered from search engine cache.
Re: date and time difference function pre php 5.3
Posted: Wed Oct 14, 2009 1:12 pm
by paulb
McInfo wrote:Have you tried using
strtotime() and subtracting two timestamps to get the difference in seconds?
I have used it. But now you mention it - I think you have just triggered my brain into solving the problem another way. Is there a function that can take the seconds and convert it into an array of days,hours, minutes, seconds etc. That would be helpful.
Re: date and time difference function pre php 5.3
Posted: Wed Oct 14, 2009 4:31 pm
by requinix
paulb wrote:I have used it. But now you mention it - I think you have just triggered my brain into solving the problem another way. Is there a function that can take the seconds and convert it into an array of days,hours, minutes, seconds etc. That would be helpful.
Could always write it yourself. Simple math.
There is a hack, but only for differences up to 365 days (though it can be improved to about 760 days).
Code: Select all
list($days, $hours, $minutes, $seconds) = explode("-", gmdate("z-G-i-s"));
Works because 0 seconds (since the Unix epoch) is January 1st 1970. 12345678 seconds is May 23rd
9:21:18pm, which is
21 hours, 21 minutes, and 18 seconds after the 142nd day of the year.
Should be using gmdate(), otherwise timezones will throw off the measurements.
Re: date and time difference function pre php 5.3
Posted: Wed Oct 14, 2009 7:13 pm
by McInfo
Here is a class that could be built on.
Code: Select all
<?php
class Duration {
var $days;
var $hours;
var $minutes;
var $seconds;
function Duration ($seconds) {
$s = abs((int) $seconds);
$d = floor($s / 86400);
$s %= 86400;
$h = floor($s / 3600);
$s %= 3600;
$m = floor($s / 60);
$s %= 60;
$this->days = $d;
$this->hours = $h;
$this->minutes = $m;
$this->seconds = $s;
}
}
$t1 = strtotime('2005-03-12T15:19:25+00:00');
$t2 = strtotime('2004-02-12T16:12:21+00:00');
$dur = new Duration($t1 - $t2);
print_r($dur);
?>
Edit: This post was recovered from search engine cache.
Re: date and time difference function pre php 5.3
Posted: Thu Oct 15, 2009 2:00 am
by paulb
Re: date and time difference function pre php 5.3
Posted: Fri Oct 16, 2009 1:19 am
by paulb
That did the trick. Thanks again
