Finding difference of days between two dates in PHP

Yesterday, my friend Roshan was was struggling to find the days difference between to two dates in PHP. Well, after a while I came up with the solution for him. The date format he’s using was “YYYY-MM-DD”, which is the standard format of date value stored in the MySql database.

Function to find the difference of days between two dates

function daysDifference($endDate, $beginDate)

   //explode the date by "-" and storing to array
   $date_parts1=explode("-", $beginDate);
   $date_parts2=explode("-", $endDate);
   //gregoriantojd() Converts a Gregorian date to Julian Day Count
   $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
   $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
   return $end_date - $start_date;

The above function is fairly simple, first of all the dates values in the format “YYYY-MM-DD” is separated into array by using explode() function. The first element of array contains the year, the second element contains the month and the last element contains the day value.

And the most import function in finding the days difference is gregoriantojd(), this function converts a Gregorian date to Julian Day Count. This function takes three parameters- month, day and year.

And in the last line of the function, the difference between the Julian Day count of two dates are calculated and returned.

Example :

   echo daysDifference('2008-03-12','2008-03-09');

What is the output of the above statement, I don’t think I need to write it down. But, I’m a nice guy and never leave the suspense, it’s 3 :-) .

23 thoughts on “Finding difference of days between two dates in PHP

  1. val

    hi! nice function.. but was just wondering why the function had to convert a Gregorian date to Julian day Count? [sorry.. it’s the first time I saw it..]

  2. dear val..Julian day Count in PHP supports dates ranging from 4714 B.C. to 9999 A.D so I’ve converted the Gregorian date to Julian day Count in this function.

  3. val

    ah.. i see.. uwkiee.. :) thanks for answering my question..

  4. Bill Evans

    I have found all of your articles very useful and to the point. Thank you.

  5. Thanks bill for the appreciation…

  6. matt

    thankyou thankyou thankyou 😀

  7. vishwa

    can u please tell me how to compaire day month and year given two date form two date field……………………! (‘,’)

  8. Max

    Thanks for the code, it works great! :)

  9. vaskar

    Thank you excellent function too much helpful

  10. Nirmal

    I think this is better than aboe proposed solution
    function daysDifference($endDate, $beginDate)
    $beginDate = strtotime($beginDate);
    $endDate = strtotime($endDate);
    $diff = $endDate – $beginDate;
    $diff = ceil($diff / (60*60*24)) ;
    return $diff;

  11. Thank you. I have been looking around for this function.

  12. Patel Dharmendra M

    Thanks dear all my friends this example very good for my website

  13. XianD6

    Hi Roshan,

    Calculate How Many Days User Has Stayed With The Book Once She Return It).
    I am learning PHP/MySQL, and I am also writting the Library Management
    System to make sure that I practice what I am learning.

    My Problem is:

    I want to capture the date when the library user borrow the book and then
    check that date to see if the book have exceeded the number of days (to
    calculate how many days user has stayed with the book once she return it).
    Please help me how I can do that. I am using mysql as my database.

  14. XianD6

    i find your codes very useful,
    thanks in advance…

  15. Hi Roshan, the following page contains a reference to this article. Great information.

    Calculate the difference between two Dates (and time) using Php. The following page provides a range of different methods (7 in total) for performing date / time calculations using Php, to determine the difference in time (hours, munites), days, months or years between two dates.

    See Php Date Time – 7 Methods to Calculate the Difference between 2 dates

  16. Thanks for the scrpt!

  17. Pravin

    Nice Code Roshan !!

  18. Johnte

    Calculating the date difference based on time stamp is most robust way since it takes care off the difference in date and time formats. example

    function GetDateDifference ($Date1,$Date2)
    $ConvertToTimeStamp _Date1 = strtotime($Date1);
    $ConvertToTimeStamp_Date2 = strtotime($Date2);
    $DateDifference = (int)$ConvertToTimeStamp _Date1 – (int)$ConvertToTimeStamp_Date2;
    return round($DateDifference/86400);

  19. Not working dude

  20. Nice code, great job

  21. Eren

    Ya this is good solution… But i can’t get the result.

  22. dillip

    thanks for your script

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>