Jump to content

[SOLVED] How to get days between two dates?


t_machine

Recommended Posts

Hi, I am using the following function to get the actual days between two given dates but it is having memory allocation issues.

 

 


$sStartDate = 2009-06-22;
$sEndDate = 2009-06-29;

function _GetDays($sStartDate, $sEndDate){
  $sStartDate = gmdate("Y-m-d", strtotime($sStartDate));
  $sEndDate = gmdate("Y-m-d", strtotime($sEndDate));
  $aDays[] = $sStartDate;
  $sCurrentDate = $sStartDate;
  
  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;
  }
  return $aDays;
}//EOF

 

What I am trying to do is get it to display 2009-06-22, ..23, ..24, 25, 26, 27, 28 and the 2009-06-29. This would be the days in between the start and end date  including both dates.

 

Any help or a better code would be really appreciated.

 

Thanks :)

 

 

$sStartDate = "2009-06-22";
$sEndDate = "2009-07-04";

$sStart = strtotime($sStartDate);
$sEnd = strtotime($sEndDate);
$step = 60 * 60 * 24; // sec * min * hour
for ($day = $sStart; $day <= $sEnd; $day += $step) {
  echo date("Y-m-d",$day) . "<br/>";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.