Uzm Posted December 20, 2007 Share Posted December 20, 2007 Hey, all! 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. Quote Link to comment https://forums.phpfreaks.com/topic/82512-difference-between-two-dates/ Share on other sites More sharing options...
JasonLewis Posted December 20, 2007 Share Posted December 20, 2007 i'm sure if you run a search through these forums you will un-cover the answer because I am sure I have seen similar topics. Quote Link to comment https://forums.phpfreaks.com/topic/82512-difference-between-two-dates/#findComment-419421 Share on other sites More sharing options...
PHP_PhREEEk Posted December 20, 2007 Share Posted December 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/82512-difference-between-two-dates/#findComment-419812 Share on other sites More sharing options...
Jessica Posted December 20, 2007 Share Posted December 20, 2007 Oh nice. Bookmarked! Quote Link to comment https://forums.phpfreaks.com/topic/82512-difference-between-two-dates/#findComment-419817 Share on other sites More sharing options...
Uzm Posted December 21, 2007 Author Share Posted December 21, 2007 Thanks a lot, PHP_PhREEEk! Quote Link to comment https://forums.phpfreaks.com/topic/82512-difference-between-two-dates/#findComment-420410 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.