我想将四个即将到来的日期存储到数据库中?

时间:2016-05-02 19:13:07

标签: php datetime

我想在数据库中存储四个即将到来的日期,从当前日期开始增加7天。例如,今天的日期是2016/05/02,接下来的四个日期是 2016/05/09,2016/05/16,2016/05/23,2016/05/30

我正试图用这种方法做到这一点:

$today = date('Y/m/d', strtotime('+0 days'));
$_SESSION['nextweek'] = $today;
for($i = 1; $i < 5; $i++){
    $j = $i + 7;
    $_SESSION['nextweek'] = date($_SESSION['nextweek'], strtotime('$j days'));   
}

4 个答案:

答案 0 :(得分:1)

我不确定您要达到的目标,但当前日期 4天 7天 ,你可以(并且应该)使用DateTime类:

<?php
session_start();
date_default_timezone_set( 'Europe/Lisbon' );
$datetime = new DateTime();
for( $i=1 ; $i<5; $i++ ){
    $datetime->modify( '+7 day' );
    $_SESSION['nextweek'] = $datetime->format( 'Y-m-d' );
    echo $_SESSION['nextweek'];
}
/*
2016-05-09
2016-05-16
2016-05-23
2016-05-30
*/

Ideone Demo

答案 1 :(得分:0)

你的循环必须是这样的

$today = date('Y/m/d',strtotime('+0 days'));
$_SESSION['nextweek'] = $today;
$j=7;
for($i= 0;$i<4;$i++)
{
    $nextdate = date('Y/m/d',strtotime("+$j day", strtotime('+0 days'))); 
    $j+=7;
    print_r($nextdate); 
    echo "<br />";
}

此输出

2016/05/09
2016/05/16
2016/05/23
2016/05/30

答案 2 :(得分:0)

$today = date('Y/m/d',strtotime('+0 days'));
$_SESSION['nextweek'] = $today;
for($i= 7;$i<=28;$i++){
$_SESSION['nextweek']= date($_SESSION['nextweek'],strtotime('$i days'));  
$i =$i+6; // now it increments with 7 days.
}

这应该给你正确的日期

答案 3 :(得分:0)

for($i= 1;$i<5;$i++){
    $j = $i*7;
    $_SESSION['nextweek']= date('Y/m/d',strtotime("+".$j." days"));   
}