PHP: get next 13 dates from date?

后端 未结 3 1283
遇见更好的自我
遇见更好的自我 2021-01-20 15:36

I am trying to get an array of a date plus the next 13 dates to get a 14 day schedule starting from a given date.

here is my function:

$time = strtot         


        
3条回答
  •  天涯浪人
    2021-01-20 16:05

    You have the same date because of daylight saving time switch. It's not safe to add 24*60*60 seconds to find next day, because 2 days in the year have more/less seconds in them. When you switch from summer to winter time you are adding 1 hour to a day. So it'll be 25*60*60 seconds in that day, that's why it's not switched in your code.

    You can do your calculation by mktime(). For example:

    ## 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);
    

    Or the full version for your code:

    $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)
        );
    }
    

提交回复
热议问题