Date or Time Comparison in PHP
Posted on December 25, 2007
Filed Under Date-Time manipulation, php
If you’ve to compare the difference between two dates or times values. How you’re going to accomplish it in PHP ? If you don’t know how to do it, then here is simple fuction for you to compare the date (as well as time) in PHP. For this comparison to take place, the argument supplied to this function must be in a USA and ISO date format.
Function to compare Date or Time in PHP
function greaterDate($start_date,$end_date)
{
$start = strtotime($start_date);
$end = strtotime($end_date);
if ($start-$end > 0)
return 1;
else
return 0;
}
so if there two date or time values stored in $date1 and $data2 variables then you can call that function in the following way
$date1='2007-10-10'; $date1='2007-10-11'; if(greaterDate($date1,$date2)) echo "First parameter is greater"; else echo "Second parameter is greater";
well you can guess the result, it prints out “Second parameter is greater”and if you call the same function with these different values
$date1='2007-10-10 12:15:27'; $date2='2007-10-10 11:17:37';
The result will be “First parameter is greater”
Popularity: 13% [?]
Follow me on twitter at http://twitter.com/roshanbh.
Related Posts
» Solving time difference between hosting server and local timezone in PHP
» Date format validation in PHP
» Finding difference of days between two dates in PHP
» roshanbh.com..Is that a coincidence or bargaining attempt?
Comments
9 Responses to “Date or Time Comparison in PHP”
Leave a Reply






Your approach fails for dates that are outside of the unix time stamp date range (before 1970 and after 2038).
My recommendation would be to use PHP5-s new datetime functionality instead and just do:
$date1 = new Datetime(’2007-10-10 12:15:27′);
$date2 = new Datetime(’2007-11-10 12:15:27′);
print $date2 > $date1;
You get a few more niceties with that extension too, just check the manual for details.
Well i’m sorry to say that you are right dude but only upto some extent, this class DateTime is introduced after PHP 5.1.0 and the example you’ve given always return false result either way,
print $date2 > $date1; or
print $date1 > $date2;
To print out the proper result you’ve to do like this,
print $date2->format(’U')>$date1->format(’U');
No you don’t. Try this:
$date1 = new Datetime(’2007-11-10 12:15:27′);
$date2 = new Datetime(’2007-10-10 12:15:27′);
$date3 = new Datetime(’2007-10-10 11:15:27′);
$date4 = new Datetime(’2007-10-10 12:15:27′);
if ($date2 > $date1) {
print “date2 is later than date1″;
} else {
print “date1 is later than date2″;
}
print “\n”;
if ($date3 > $date4) {
print “date3 is later than date4″;
} else {
print “date4 is later than date3″;
}
print “\n”;
[~]> php date.php
date1 is later than date2
date4 is later than date3
[~]> php -v
PHP 5.2.4 (cli) (built: Dec 16 2007 23:51:11)
Ok, I take it back.
For some weird reason the code works. I get correct results when playing with the values, but this seems to be a simple side effect, there is nothing built into the datetime class to do that kind of comparisions, it is just a simple object instance comparision.
damn, I don’t get it
I found this - http://bugs.php.net/bug.php?id=40691
The sample code there gives expected response to me e.g. DateTime Equal? false
And I’m on php 5.2.4
Hello,
I have observed that this function
if(greaterDate($date1,$date2))
echo “First parameter is greater”;
else
echo “Second parameter is greater”;
work well for below values
$date1=’2007-10-10 12:15:27?;
$date1=’2007-10-10 11:17:37?;
but NOT for
$date1=’2007-10-10 9:15:27?;
$date1=’2007-10-10 11:17:37?;
Please reply.
No problem at all man…
when i tried with
if(greaterDate(’2007-10-10 09:15:27′,’2007-10-10 11:17:37′))
echo “First parameter is greater”;
else
echo “Second parameter is greater”;
I got the result
“Second parameter is greater”
If first date is 17/05/2008 and second date is 18/04/2008
greaterDate($date1,$date2);
will display as 0. Meaning 17/05/2008 is less than 18/04/2008 which is wrong. It is comparing only the dates.
This function doesn’t support the UK datetime format..and that’s why you’re getting improper result…it’s beeter to have the date and time in “YYYY-MM-DD” format or “YYYY-MM-DD HH:MM:SS” format…