t_machine Posted June 29, 2009 Share Posted June 29, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/164158-solved-how-to-get-days-between-two-dates/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 29, 2009 Share Posted June 29, 2009 $sStartDate = 2009-06-22; and $sEndDate = 2009-06-29; are not dates, they are 1981 (2009 - 6 - 22) and 1974 (2009 - 6 - 29). You need some quotes around those values to make them strings that represent dates. Quote Link to comment https://forums.phpfreaks.com/topic/164158-solved-how-to-get-days-between-two-dates/#findComment-865972 Share on other sites More sharing options...
.josh Posted June 29, 2009 Share Posted June 29, 2009 $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/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/164158-solved-how-to-get-days-between-two-dates/#findComment-865973 Share on other sites More sharing options...
aggrav8d Posted June 29, 2009 Share Posted June 29, 2009 Crayon - what happens when the time between start and end is less than 24h but they are still on different days? Quote Link to comment https://forums.phpfreaks.com/topic/164158-solved-how-to-get-days-between-two-dates/#findComment-865988 Share on other sites More sharing options...
.josh Posted June 29, 2009 Share Posted June 29, 2009 well considering his dates do not seem to cover anything more granular than 24 hour spans... Quote Link to comment https://forums.phpfreaks.com/topic/164158-solved-how-to-get-days-between-two-dates/#findComment-865996 Share on other sites More sharing options...
papaface Posted June 29, 2009 Share Posted June 29, 2009 Ignore Quote Link to comment https://forums.phpfreaks.com/topic/164158-solved-how-to-get-days-between-two-dates/#findComment-866000 Share on other sites More sharing options...
t_machine Posted June 30, 2009 Author Share Posted June 30, 2009 Thank you very much. Works great! Quote Link to comment https://forums.phpfreaks.com/topic/164158-solved-how-to-get-days-between-two-dates/#findComment-866118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.