为日期分配变量

时间:2011-08-10 06:50:20

标签: php date pagination assign

我需要在今天的日期分配一个特定的号码,如1559,并根据此计算前一天和下一天。例如2天前是1557,昨天是1558,今天是1559,明天是1560等等。有了这个,我将用post方法为日子创建一个分页。我的日期是YYYY-MM-DD格式,存储在mySQL的日期变量中。我应该怎么做才能将数字分配到日期并相应地计算日期?

我的代码看起来像这样btw:

include('connect.php');

    date_default_timezone_set("Etc/GMT+7");
    $timestamp= date("Y-m-d");
    $day1=1357;

    echo ('<form action="total_inf.php" method="post" name="inf_day">');
    for($i=0;$i<10;$i++)

    {
    $newdate=strtotime('-$i days',strtotime($timestamp));
    $newdate = date ( 'Y-m-d' , $newdate );

    $prevdays=$day1-$i;

    print ('<input type="submit" name="bugun" value="'.$prevdays.'">&nbsp;');

    }
    echo ('</form>');       

    $day_selected=$_POST[bugun];

    $sql1="SELECT * FROM userdata WHERE zaman='$timestamp'";
    $result1=mysql_query($sql1,$conn);

1 个答案:

答案 0 :(得分:0)

$current_date = new DateTime('2011-08-10');
$current_date->modify('+1 day'); //next day
$current_date->modify('-1 day');// prev day 

$other_date = new DateTime('1970-01-01'); //another date

请参阅DateTime reference

更新:

$day1 = new DateTime();
$day1->modify('-1357 days'); 
//if you need it to be some fixed date, use $day1= new DateTime("2011-08-01");

$now_date = new DateTime();
for($i=0;$i<10;$i++){
    $date_diff = $now_date->diff($zero_date);
    $prevdays = $date_diff->format("#a")-$i;
    print ('<input type="submit" name="bugun" value="'.$prevdays.'">&nbsp;');
}

适用于PHP&gt; = 5.3.0。见DateInterval::format