Jump to content

Recommended Posts

Hey, all! ;D

 

Ok, i've been researching about this problem (if we can call it a problem) for a week now, and i simply can't find a good solution for it. So, i was wondering if you can help me with this one. I would appreciate this a lot.

 

What i need to do is a PHP code, that will tell me difference between two dates in days/minutes/seconds.

Let's say, that i've a date in format YYYY-MM-DD HH:MM:SS, and the current date.

 

Yeah, this seems very simple, but i haven't found the right code for it.

 

Thanks again.

Link to comment
https://forums.phpfreaks.com/topic/82512-difference-between-two-dates/
Share on other sites

Here you go... I created this custom function to compare any past or future date with today's date. It uses strtotime(), so your format of YYYY-MM-DD HH:MM:SS will work perfectly. Any valid strtotime() argument will work... It returns an array which tells if the comparison was in the past, and individual values for days, hrs, mins and secs. Use the array elements as you see fit....

 

<?php
function compare2now($compare) {
    $diff['compare'] = strtotime($compare);
    if ( $diff['compare'] == -1 || $diff['compare'] == false ) {
        return false;
    }
    $difference = $diff['compare'] - time();
    if ( $difference < 0 ) {
        $difference = abs($difference);
        $diff['ago'] = true;
    }
    $diff['days'] = intval($difference/86400);
    $remain = $difference%86400;
    $diff['hours'] = intval($remain/3600);
    $remain = $remain%3600;
    $diff['mins'] = intval($remain/60);
    $diff['secs'] = $remain%60;
    return $diff;

}

 

If not false (bad arguments to strtotime()), it returns this array:

 

Array
(
    [compare] => Unix Timestamp of compared date
    [ago] => true or false
    [days] => integer
    [hours] => integer
    [mins] => integer
    [secs] => integer
)

 

Here's an example of usage. I've used an array and a foreach loop to simulate several possibilities:

 

<?php
$compArray = array('2008-1-1 00:00:00',
                   '2008-2-29 00:00:00',
                   '2008-2-30 00:00:00',
                   '2007-2-1 00:00:00',
                   '2007-12-20 20:10:30'
                  );
foreach ( $compArray as $value ) {
    $compare = compare2now($value);
    if ( is_array($compare) ) {
        if ( $compare['ago'] ) {
            $was = ' was ';
            $ago = ' ago ';
        } else {
            $was = ' is ';
            $ago = ' from now ';
        }
        echo 'Today is ' . date("l, F jS, Y") . ' at ' . date("H:i:s");
        echo '<br>';
        echo date("l, F jS, Y", $compare['compare']) . ' at ' . date("H:i:s", $compare['compare']) . $was;
        echo $compare['days'] . ' day(s) ';
        echo $compare['hours'] . ' hour(s) ';
        echo $compare['mins'] . ' min(s) ';
        echo $compare['secs'] . ' secs(s) ';
        echo $ago . '<br><br>';
    } else {
        die('Invalid date format in compare2now() function!');
    }
}
?>

 

Prints:

 

Today is Thursday, December 20th, 2007 at 14:04:52
Tuesday, January 1st, 2008 at 00:00:00 is 11 day(s) 9 hour(s) 55 min(s) 8 secs(s) from now 

Today is Thursday, December 20th, 2007 at 14:04:52
Friday, February 29th, 2008 at 00:00:00 is 70 day(s) 9 hour(s) 55 min(s) 8 secs(s) from now 

Today is Thursday, December 20th, 2007 at 14:04:52
Saturday, March 1st, 2008 at 00:00:00 is 71 day(s) 9 hour(s) 55 min(s) 8 secs(s) from now 

Today is Thursday, December 20th, 2007 at 14:04:52
Thursday, February 1st, 2007 at 00:00:00 was 322 day(s) 14 hour(s) 4 min(s) 52 secs(s) ago 

Today is Thursday, December 20th, 2007 at 14:04:52
Thursday, December 20th, 2007 at 20:10:30 is 0 day(s) 6 hour(s) 5 min(s) 38 secs(s) from now

 

As you can see, strtotime() fixes what might seem like invalid dates. Feb 29th 2008 is valid, because it will be a leap year. strtotime() converts Feb 30th 2008 into Mar 1st 2008. If you desire further validity up front, you will need to use checkdate() on your comparison date up front using explode with checkdate().

 

Enjoy -

 

PhREEEk

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.