PHP 日期增加


PHP date increase

我写了一段代码,将日期转换为特定格式并将其增加 1 天。

<?php
date_default_timezone_set('Europe/Moscow');
$mehdate = "2011-11-25";
$mehdate = date ('d m Y', strtotime ('+1 day', strtotime($mehdate)));
echo $mehdate, "'n";
?>

但是我必须再增加一次$mehdate 1 天。我不明白该怎么做。我已经试过了

$mehdate = date ('d m Y', strtotime ("+1 day", $mehdate));

$mehdate = date ('d m Y', strtotime ('+1 day', strtotime($mehdate)));

再次,但它不起作用,因为

strtotime($mehdate)

返回 FALSE。那么,如何增加已格式化的$mehdate?

如果使用日期时间类,则可以轻松解决您的问题。

试试这个:

$mehdate = new DateTime('2011-11-25');
$mehdate->modify('+1 day');
echo $mehdate->format('d m Y')."'n";  // Gives 26 11 2011
$mehdate->modify('+1 day');
echo $mehdate->format('d m Y');       // Gives 27 11 2011
date_default_timezone_set('Europe/Moscow');
$mehdate = "2011-11-25";
$mehdate = strtotime ('+1 day', strtotime($mehdate));
$mehdate = date ('d m Y', $mehdate);
echo $mehdate, "'n";

结果

26 11 2011

对于所有像我这样的新手,有一个简单的建议:不要使用"d m Y"格式,你最好使用"d-m-Y"进行操作。

或者你必须使用 DateTime 类,正如对象操纵器建议的那样。