PHP: get the next 13 dates from a date?

I am trying to get an array of a date and the following 13 dates to get a 14-day chart starting at a specific date.

here is my function:

$time = strtotime($s_row['schedule_start_date']); // 20091030
$day = 60*60*24;
for($i = 0; $i<14; $i++)
{
    $the_time = $time+($day*$i);
    $date = date('Y-m-d',$the_time);
    array_push($dates,$date);
}

But he seems to be repeating the date when the month switches.

here is what i get:

2009-10-30 | 2009-10-31 | 2009-11-01 | 2009-11-01 | 2009-11-02 | 2009-11-03 | 2009-11-04 | 2009-11-05 | 2009-11-06 | 2009-11-07 | 2009-11-08 | 2009-11-09 | 2009-11-10 | 2009-11-11

Please note that 2009-11-01 is repeated. I can’t understand why?

What am I doing wrong?

Thanks!!

+3
source share
3 answers

I would use strtotime

$start = strtotime($s_row['schedule_start_date']);
$dates=array();
for($i = 1; $i<=14; $i++)
{
    array_push($dates,date('Y-m-d', strtotime("+$i day", $start)));
}
print_r($dates);
+6
source

- . 24*60*60 , , 2 / . , 1 . , 25*60*60 , .

mktime(). :

## calculate seconds from epoch start for tomorrow
$tomorrow_epoch = mktime(0, 0, 0, date("m"), date("d")+1, date("Y"));
## format result in the way you need
$tomorrow_date = date("M-d-Y", $tomorrow_epoch);

:

$dates = array();
$now_year = date("Y");
$now_month = date("m");
$now_day = date("d");
for($i = 0; $i < 14; $i++) {
    $next_day_epoch = mktime(0, 0, 0, $now_month, $now_day + $i, $now_year);
    array_push(
        $dates,
        date("Y-m-d", $next_day_epoch)
    );
}
+7

I recommend something like:

for($i=1;$i<=14;$i++){
     echo("$i day(s) away: ".date("m/d/Y",strtotime("+$i days")));
}
+4
source

Source: https://habr.com/ru/post/1721322/


All Articles