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”

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

17 Comments on “Date or Time Comparison in PHP”

  • Anti Veeranna wrote on 29 December, 2007, 22:57

    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.

  • Roshan wrote on 30 December, 2007, 16:11

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

  • Anti Veeranna wrote on 30 December, 2007, 17:30

    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)

  • Anti Veeranna wrote on 30 December, 2007, 17:42

    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.

  • Anti Veeranna wrote on 30 December, 2007, 17:45

    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

  • vipin wrote on 31 March, 2008, 11:20

    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.

  • Roshan wrote on 31 March, 2008, 18:50

    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”

  • shels wrote on 17 May, 2008, 15:49

    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.

  • Roshan wrote on 18 May, 2008, 5:23

    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…

  • Mann wrote on 6 October, 2008, 12:14


    but the result is not correct

  • cc wrote on 13 January, 2009, 2:11

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

  • IMF wrote on 9 June, 2009, 16:59

    Looks good, I shall try it.

  • Rebecca wrote on 17 July, 2009, 5:48

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

  • Sheetal wrote on 29 April, 2010, 17:42

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

  • Mayor sipayung wrote on 25 June, 2010, 16:31

    Thank you for the script

  • Muneeb wrote on 25 August, 2010, 9:29

    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

  • Tapan Kumar Thapa wrote on 18 February, 2011, 9:14

    Thanks it works…:-)

    Tapan Thapa

Write a Comment


Copyright © 2015 Roshan Bhattarai's Blog. All rights reserved.
Powered by, Custom Theme and Calling Card Company.