Date or Time Comparison in 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;
   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

   echo "First parameter is greater";
   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”

17 thoughts on “Date or Time Comparison in PHP

  1. Anti Veeranna

    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.

  2. Roshan

    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’);

  3. Anti Veeranna

    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)

  4. Anti Veeranna

    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.

  5. Anti Veeranna

    damn, I don’t get it :)

    I found this –

    The sample code there gives expected response to me e.g. DateTime Equal? false

    And I’m on php 5.2.4

  6. vipin

    I have observed that this function

    echo “First parameter is greater”;
    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.

  7. 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”;
    echo “Second parameter is greater”;

    I got the result
    “Second parameter is greater”

  8. shels

    If first date is 17/05/2008 and second date is 18/04/2008
    will display as 0. Meaning 17/05/2008 is less than 18/04/2008 which is wrong. It is comparing only the dates.

  9. 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…

  10. Mann


    but the result is not correct

  11. cc

    i think u dont have to do $start-$end just do $start>$end

  12. IMF

    Looks good, I shall try it.

  13. Rebecca

    Thank you for the script. Unlike make scripts on the net, it compares the date, the month and the year. Its logically correct.

  14. Could you please tell me either time is in 24 hr or in 12 hrs format?

  15. Thank you for the script

  16. Muneeb

    guys you can also use mysql for this purpose
    the query SELECT TIMEDIFF(‘$Date1′ , ‘$Date2′) wil do the job
    here is the link
    also there are many other time and date manipulation functions in mysql you might want to check out on the link given above

  17. Thanks it works…:-)

    Tapan Thapa

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>