Get Days Between Two Dates Using PHP | edrackham




This is a simple snippet of code that will return an array of days between two dates.

Read on for a little more info…

function GetDays($sStartDate, $sEndDate){
  // Firstly, format the provided dates.
  // This function works best with YYYY-MM-DD
  // but other date formats will work thanks
  // to strtotime().
  $sStartDate = gmdate("Y-m-d", strtotime($sStartDate));
  $sEndDate = gmdate("Y-m-d", strtotime($sEndDate));

  // Start the variable off with the start date
  $aDays[] = $sStartDate;

  // Set a 'temp' variable, sCurrentDate, with
  // the start date - before beginning the loop
  $sCurrentDate = $sStartDate;

  // While the current date is less than the end date
  while($sCurrentDate < $sEndDate){
    // Add a day to the current date
    $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));

    // Add this new day to the aDays array
    $aDays[] = $sCurrentDate;
  }

  // Once the loop has finished, return the
  // array of days.
  return $aDays;
}

Usage:

$aDays = GetDays('5th Feb 2007', '10th Feb 2007');

You can use most date formats, such as:

GetDays('2007-01-01', '2007-01-31');

or

GetDays('19-02-2007', '25-02-2007');

Whatever strtotime can handle, you can pass to this function.

Hope it helps!


Categories: PHP


27 Responses so far.


  1. Ben McDougal says:

    Saint!

  2. Manish says:

    Great and very usefull script..thank you

  3. Javed says:

    Thx buddy i really enjoyed this function… as i truely req. it in my proj….

    Again thanking u….

  4. bernard says:

    Kudos to you dude. that saved me some time! thank you!

  5. rob says:

    Thank you! This is exactly what I had been looking for.

  6. yamama says:

    This is exactly what i want..but it does not work well…i alway get this error

    Fatal error: Maximum execution time of 30 seconds exceeded

    i don’t know why..!!!!i understand the code well but…
    thanks alot..thanks for any help…

  7. yamama says:

    by the way….i use a logical two days!!!!!!!!!! the start date less than the end date and between them not more than 25 days..:(

  8. Midhun says:

    Thanx a lot for the function

  9. lekshmi says:

    am not getting any output. The result is displayed as array

  10. james says:

    That is a horrible solution! Very inefficient, slow and bloated.

    Try this instead.

    $time_diff_in_seconds = strtotime($date1) – strtotime($date2);

    $time_diff_in_days = ceil($time_diff_in_seconds / 86400);

    • David says:

      Your solution does not even work. A difference in days does not necessarily equal the division by 86400 seconds. Between 31 dec 1999 23:59:59 and 01 jan 2000 0:01:01 is one day difference but only 2 seconds.

      Ed Rackham solution is working very well, except when you cross the internation date line when you could have an end date ‘before’ a start date.

      A plane leaving aukcland on the 23 jan can arriving one day earlier in Cook island on the 22 jan.

      Thank Ed for sharing.

  11. james says:

    ah, your getting days as strings.. ok , google was wrong. blame them.

  12. Kunal Sagar says:

    Thanks…. Simple but very useful…

  13. John says:

    You are a life saver

  14. Johan says:

    PERFECT! AWESOME!!! THX!!

  15. Rofhiwa says:

    An answer I have been looking for. This a great.
    Thank you so much.

  16. [...] Edrackham – Get days between two dates [...]

  17. I always inspired by you, your opinion and attitude, again, thanks for this nice post.

    - Norman

  18. raju bogati says:

    only output iz ‘array’. no solution

  19. Tibi says:

    Thanks very much, I’m using this function in a project of mine

  20. bimal says:

    Hi,

    This is what actually i am looking for. But can you please help me in case of different years. It doesn’t return the dates

    for example:
    Start date : 2010-11-25
    End date : 2011-01-12

    Thanks in advance..

  21. dheeraj says:

    Thanks very much …for calculate age i required this very much

  22. ZigPress says:

    Many thanks – a great time-saver.

  23. samir says:

    this function not works for the month month having 31st

  24. Rich says:

    Thanks a lot !!

  25. koning says:

    THANX! very usefull

  26. tushar says:

    waste of time!!! bad solution

Leave a Reply