Finding difference of days between two dates in PHP

Posted on March 18, 2008 
Filed Under Date-Time manipulation, php, tutorial

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 :

<?php
   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 :-) .

Popularity: 13% [?]

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


Follow me on twitter at http://twitter.com/roshanbh.

Related Posts

» Solving time difference between hosting server and local timezone in PHP
» I’m Sorry !!! But Thank you for the achievement
» Diplomatic statements from Project Mangers
» Getting filename and extension in PHP using explode() ,basename() and pathinfo()

Comments

6 Responses to “Finding difference of days between two dates in PHP”

  1. PHP Coding School » Blog Archive » php tips [2008-03-18 18:36:28] on March 18th, 2008 6:43 pm

    [...] Finding difference of days between two dates in PHP By Roshan 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 … Roshan Bhattarai’s Blog - PHP… - http://roshanbh.com.np [...]

  2. val on March 28th, 2008 4:57 am

    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..]

  3. Roshan on March 28th, 2008 9:04 am

    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.

  4. val on April 2nd, 2008 7:08 am

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

  5. Bill Evans on April 19th, 2008 12:13 pm

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

  6. Roshan on April 19th, 2008 12:21 pm

    Thanks bill for the appreciation…

Leave a Reply