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
Saint!
Great and very usefull script..thank you
Thx buddy i really enjoyed this function… as i truely req. it in my proj….
Again thanking u….
Kudos to you dude. that saved me some time! thank you!
Thank you! This is exactly what I had been looking for.
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…
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..:(
Thanx a lot for the function
am not getting any output. The result is displayed as array
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);
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.
ah, your getting days as strings.. ok , google was wrong. blame them.
Thanks…. Simple but very useful…
You are a life saver
PERFECT! AWESOME!!! THX!!
An answer I have been looking for. This a great.
Thank you so much.
[...] Edrackham – Get days between two dates [...]
I always inspired by you, your opinion and attitude, again, thanks for this nice post.
- Norman
only output iz ‘array’. no solution
Thanks very much, I’m using this function in a project of mine
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..
Thanks very much …for calculate age i required this very much
Many thanks – a great time-saver.
this function not works for the month month having 31st
Thanks a lot !!
THANX! very usefull
waste of time!!! bad solution