Kenno’s OpenNote

PHP: number of days between two dates

Posted in Tips by kenno on July 4, 2009

It’s Saturday, what am I doing in front of the computer? I should be out and enjoy life. ^o^

A colleague asked to customize a theme for his blog using WordPress. Because I didn’t have any good plan this Saturday, learning PHP sound like a logical and useful choice. Well, I bribed myself with a bottle of Tui.

Then, I found an interesting PHP related post by Chorn Sokun entitled “PHP date diff“. The author was trying to calculate the number days between two give dates. At the end of the post, he asked “What would you do instead?”

Here is my answer.

< ?php
/*
 * Function to calculate the number of days between
 * two given dates.
 * Assumption:
 *              $date2 > $date1
 *              Date format: mm/dd/yyyy e.g.: 31/07/2009
 * Author: kenno
 * Date: July 4, 2009
 */
function number_of_days($date1, $date2) {
$date1Array = explode('/', $date1);
$date1Epoch = mktime(0, 0, 0, $date1Array[1],
$date1Array[0], $date1Array[2]);

$date2Array = explode('/', $date2);
$date2Epoch = mktime(0, 0, 0, $date2Array[1],
$date2Array[0], $date2Array[2]);

$date_diff = $date2Epoch - $date1Epoch;
return round($date_diff / 60 / 60 / 24);
}

echo number_of_days("04/7/2009", "12/7/2009"); // 8
?>

Now, it’s my turn to ask. How could I make it better? How would you do it? :)

Update:

Added the abs( ) to round up the number of days to integer as suggested by Sokun.
Added round( ) to round up the number of days on line 21. I confused round( ) with abs( ).

Tagged with:

9 Responses

Subscribe to comments with RSS.

  1. Chorn Sokun said, on July 4, 2009 at 2:27 pm

    brilliant :)

  2. Chorn Sokun said, on July 4, 2009 at 2:36 pm

    Small improvement to make to your code and make it perfect on line 11 abs()

  3. Mansoor said, on July 14, 2009 at 8:44 am

    Helo can you give me this code in (“Y-m-d”) format

    • Kenno said, on July 14, 2009 at 11:00 pm

      Mansoor, I think the best way is to try to understand how the code works. I could give you the code you asked, but you would not learn anything.

      Here is the hint, read the PHP: mktime manual. It should answer your question.

  4. Craig said, on September 14, 2009 at 7:05 pm

    Hi,

    Saw your reply on a similar code comment, but with that one I ran into a problem with large differences in dates. It would display 1293.33333 or whatever.

    So I tried your code and even this one does the same thing! For example:

    28/2/2006 – 14/9/2006 (today)

    outputs:

    1293.95833333 days

    Why does that happen? Seems to happen with large differences, smaller ones (around 900+ days) are fine. Can you help lol

    Thanks
    Craig.

    • kenno said, on September 18, 2009 at 2:10 pm

      Craig,

      In your example “28/2/2006 – 14/9/2006 (today)”, I believe the “today” date when you posted the comment was 14/9/2009, not “2006″. The duration between 28/2/2006 and 14/9/2009 should be 1,293.95 days.

      I just added the round( ) function on line 21 to round up the number such as 1,293.95 to 1,294.

      Please let me know if you find any bugs. I’ll fix it, and feel free to ask any questions.

  5. Craig said, on September 14, 2009 at 7:52 pm

    Hmm, now it’s doing it for a number of 233 days!

    It outputs:

    233.958333333

    I’m going to bed lol Argh :P

    • kenno said, on September 18, 2009 at 2:11 pm

      What are the start and ending dates?


Leave a Reply