date and time difference function pre php 5.3

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paulb
Forum Newbie
Posts: 12
Joined: Tue Sep 29, 2009 12:51 am

date and time difference function pre php 5.3

Post 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! :banghead:

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!!!! 8O

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.
paulb
Forum Newbie
Posts: 12
Joined: Tue Sep 29, 2009 12:51 am

Re: date and time difference function pre php 5.3

Post by paulb »

Can someone please help with this?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: date and time difference function pre php 5.3

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 12:44 pm, edited 1 time in total.
paulb
Forum Newbie
Posts: 12
Joined: Tue Sep 29, 2009 12:51 am

Re: date and time difference function pre php 5.3

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: date and time difference function pre php 5.3

Post 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.
Last edited by requinix on Wed Oct 14, 2009 8:33 pm, edited 1 time in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: date and time difference function pre php 5.3

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 12:45 pm, edited 1 time in total.
paulb
Forum Newbie
Posts: 12
Joined: Tue Sep 29, 2009 12:51 am

Re: date and time difference function pre php 5.3

Post by paulb »

McInfo wrote: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);
?>

Thank you very much for that. I will have a try of it later. :) :) :) :D :D :D
paulb
Forum Newbie
Posts: 12
Joined: Tue Sep 29, 2009 12:51 am

Re: date and time difference function pre php 5.3

Post by paulb »

That did the trick. Thanks again :P
Post Reply