Fork me on GitHub
RSS
 

Get Days Between Two Dates Using PHP

01 Nov

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!

 
43 Comments

Posted in PHP

 

Tags: , ,

Leave a Reply

 

 
  1. Ben McDougal

    August 18, 2008 at 9:53 am

    Saint!

     
  2. Manish

    December 23, 2008 at 3:34 pm

    Great and very usefull script..thank you

     
  3. Javed

    January 5, 2009 at 5:24 am

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

    Again thanking u….

     
  4. bernard

    January 21, 2009 at 4:27 pm

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

     
  5. rob

    March 19, 2009 at 11:24 pm

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

     
  6. yamama

    April 3, 2009 at 4:41 pm

    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

    April 3, 2009 at 4:43 pm

    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

    May 14, 2009 at 9:46 am

    Thanx a lot for the function

     
  9. lekshmi

    September 18, 2009 at 4:42 am

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

     
  10. james

    November 11, 2009 at 2:59 am

    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

      April 18, 2011 at 6:26 pm

      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

    November 11, 2009 at 3:00 am

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

     
  12. Kunal Sagar

    April 12, 2010 at 9:54 am

    Thanks…. Simple but very useful…

     
  13. John

    May 22, 2010 at 6:41 am

    You are a life saver

     
  14. Johan

    May 25, 2010 at 11:15 am

    PERFECT! AWESOME!!! THX!!

     
  15. Rofhiwa

    June 23, 2010 at 7:25 am

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

     
  16. PHP Resources | devdevote.com

    July 19, 2010 at 8:13 pm

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

     
  17. Poker Spielen

    September 21, 2010 at 5:03 am

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

    - Norman

     
  18. raju bogati

    September 27, 2010 at 4:01 pm

    only output iz ‘array’. no solution

     
  19. Tibi

    November 2, 2010 at 2:14 pm

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

     
  20. bimal

    November 25, 2010 at 9:37 am

    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

    April 16, 2011 at 9:40 am

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

     
  22. ZigPress

    August 17, 2011 at 1:05 pm

    Many thanks – a great time-saver.

     
  23. samir

    September 21, 2011 at 11:47 am

    this function not works for the month month having 31st

     
  24. Rich

    November 17, 2011 at 6:45 pm

    Thanks a lot !!

     
  25. koning

    December 8, 2011 at 9:49 am

    THANX! very usefull

     
  26. tushar

    February 15, 2012 at 7:24 am

    waste of time!!! bad solution

     
  27. Aneeq

    March 29, 2012 at 10:17 am

    I have found a good solution on the site below. Works like charm for me.

    http://phphelp.co/2012/03/28/how-to-print-all-the-months-and-years-between-two-dates-in-php/

    Or

    http://addr.pk/ac8b

     
  28. Rahul

    June 7, 2012 at 9:57 am

    Thanks a lots, worked for me like magic, you saved my lots of time

     
  29. Elias

    July 4, 2012 at 9:55 pm

    Excellent, thank you

     
  30. sletis

    July 19, 2012 at 2:23 pm

    thanks for this function :)

     
  31. Anonymous

    October 31, 2012 at 12:24 pm

    WORST SCRIPT I HAVE EVER SEEN IN MY LIFE.

     
  32. Alberto Lourenço

    November 28, 2012 at 5:56 pm

    Muito bom, muitíssimo obrigado! :D

     
  33. Tim

    June 3, 2013 at 3:44 pm

    change gmdate() to date() if you have problems with the while() loop

     
  34. Nida

    February 19, 2014 at 11:41 am

    I am just curious how to exclude Saturday and Sunday.

     
    • aaron

      February 21, 2014 at 2:06 pm

      Helpfull….thanks….

       
  35. internet business

    October 13, 2014 at 1:08 am

    Hello there, just became aware of your blog through
    Google, and found that it’s really informative.
    I’m going to watch out for brussels. I will be grateful if you continue this in future.
    A lot of people will be benefited from your writing. Cheers!

     
  36. Granillo

    December 9, 2014 at 5:50 pm

    the 101th coins generator which i am looking, so.. 1 out of 100 are working :|

     
  37. Geert

    February 17, 2015 at 4:07 pm

    thx!! greap script!

     
  38. 旺旺

    May 21, 2015 at 8:42 am

    谢谢,最关键的就是这句
    gmdate(“Y-m-d”, strtotime(“+1 day”,strtotime($sCurrentDate)));

    thank you

     
  39. Andris

    December 19, 2015 at 4:29 am

    Thanks Bro…
    Great function…

     
  40. honey

    March 11, 2016 at 4:46 pm

    hello? what if the dates values are not static? whereas the user must be able to input the dates..

     
  41. van cleef arpels alhambra braccialetto fortunato falso

    March 1, 2017 at 1:38 am

    cartierlovejesduas A mí me pasa igual, pero súmale que además me he estado leyendo los manga (también los que adaptan los siguientes juegos) así que el monazo que tengo de más Corpse Party es gordo.
    van cleef arpels alhambra braccialetto fortunato falso http://www.braccialegioielli.cn/