Adding days to a timestamp

后端 未结 7 1302
春和景丽
春和景丽 2021-02-05 07:59

Giving a starting date, I\'m adding four times seven days to get 5 different dates separated exactly a week each one.

//$date = \'28-10-2010\';
$timestamp = mkti         


        
7条回答
  •  不知归路
    2021-02-05 08:27

    I will never understand why people use this horrible mktime function.

    In case you are on PHP5.3 yet, you might want to consider using

    $period = new DatePeriod(
        new DateTime('28-10-2010'),
        DateInterval::createFromDateString('1 week'),
        4);
    
    foreach ( $period as $dt ) {
      echo $dt->format( "l Y-m-d H:i:s\n" );
    }
    

    Without PHP5.3 you can still use

    for($i=0; $i<=4; $i++) {
        echo date('Y-m-d', strtotime("+$i weeks 28-10-2010")), PHP_EOL;
    }
    

    DateTime questions are pretty common on SO, so you might find interesting cases when searching.

提交回复
热议问题