1

How can I limit this date or print this date from the star_date to end_date? ex.

$start_date="2011-05-15";//june 15 2011

$end_date="2011-07-30";//july -7,2011

The result should be.

$dates[]="2011-05-15";
$dates[]="2011-05-16";
$dates[]="2011-05-17";
$dates[]="2011-05-18";
$dates[]="....";
$dates[]="....";
$dates[]="....";
$dates[]="....";
until it reaches.
$dates[]="2011-07-30";
spicykimchi
  • 1,151
  • 5
  • 22
  • 41
  • 1
    possible duplicate of [PHP: Loop thru all months in date range?](http://stackoverflow.com/questions/2155110/php-loop-thru-all-months-in-date-range) – Gordon Jul 29 '11 at 06:04

6 Answers6

3

I would suggest take the start date as an object and keep adding 1 day (http://www.php.net/manual/en/datetime.add.php) in a loop until you reach the end date.

<?php
$start_date = new DateTime('2011-05-15');
$end_date = new DateTime('2011-07-30');
while($end_date > $start_date)
{
   echo $start_date->format('Y-m-d') . "\n";
   $start_date->add(new DateInterval('P1D'));
}
?>

The above code has not been tested.

Lord Loh.
  • 2,437
  • 7
  • 39
  • 64
2

This could do it ...

$start_date = strtotime('2011-05-15'); //june 15 2011
$end_date = strtotime('2011-07-30'); //july -7,2011
$dates = array();
for ($i=$start_date; $i<=$end_date; $i+=86400) {
    $dates[] = date('Y-m-d',$i);
}
designosis
  • 5,182
  • 1
  • 38
  • 57
2
$start_date="2011-05-15";
$end_date="2011-07-30";
$date=$start_date;
while (strtotime($new_date) != strtotime($end_date))
{
   echo $new_date=date("Y-m-d",strtotime("+1 day", strtotime($date)))."<br>";
   $dates[]=$new_date;
   $date=$new_date;       
}
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
2

And another....

date_default_timezone_set('America/Los_Angeles');

$startDate='2011-05-15';
$endDate='2011-07-30';

$t1=strtotime($startDate);
$days=(strtotime($endDate)-$t1)/86400;

for($i=0;$i<=$days;$i++) $dates[]=date('Y-m-d',$t1+($i*86400));

print_r($dates);

And for the 'not very efficient but will do in most cases oneliner':

for($t=strtotime($startDate);$t<=strtotime($endDate);$t+=86400) $dates[]=date('Y-m-d',$t);

And one for those who know, the last for this mornings exercises:

$dates=array_map(create_function('$t','return date("Y-m-d",$t);'),range(strtotime($startDate),strtotime($endDate),86400));
zaf
  • 22,776
  • 12
  • 65
  • 95
  • 1
    yeah, but setting Los Angeles as the epicenter automatically adds an hour with traffic... (cringe) – designosis Jul 29 '11 at 06:11
  • @neokio sorry about that, I couldn't figure your timezone and LA was one of the first in the list... – zaf Jul 29 '11 at 06:18
1

This works:

$start_date = "2011-05-15";
$end_date = "2011-07-07";

$dates = array();
$stop = strtotime($end_date);
for($i = strtotime($start_date); $i <= $stop; $i += 86400)
    $dates[] = date('Y-m-d', $i);

PS. I changed your July date to 07-07 to match up with your comment.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • 1
    @mocca 86400 is the number of seconds in one day. The value in `$i` is in seconds, so I increment it by 86400 to get to the next day. – Paul Jul 29 '11 at 07:22
1
$start_date = strtotime('2011-05-15');
$end_date = strtotime('2011-07-30');
$dates = array();
for ($i = $start_date; $i<=$end_date; $i+= (strtotime('+1 day') - strtotime('now'))) {
   $dates[] = date('Y-m-d',$i);
}
Rukmi Patel
  • 2,619
  • 9
  • 29
  • 41