为什么我的 PHP 代码给我错误的结果如下代码:
date_default_timezone_set('UTC');
echo date('d-m-Y');
正在工作和生产:
30-01-2013
但是这段代码:
date_default_timezone_set('UTC');
echo date('d-m-Y',strtotime("+1 month"));
正在生产这个日期:
02-03-2013
代替:
28-02-2013
但是我只需要月份数。
它实际上是正确的。
今天是30-01。+1 个月应该是30-02. 该日期不存在,因此变为02-03(28-02 + 2 天)
通过使用解决
http://derickrethans.nl/obtaining-the-next-month-in-php.html
代码:
echo date('d-m-Y',strtotime("first day of next month"));
因为我只需要月份数。
PHP 开发人员在此处解释了该问题。您可以使用一些变通方法,但您必须解释得出结论的逻辑,即 1 月 31 日 + 1 个月是 2 月 2 日。
因为二月只有28天。strtotime +1 月增加 30 天,尝试使用 DateTime 类代替: http: //php.net/manual/en/class.datetime.php
查看 ,
<?php
date_default_timezone_set('UTC');
//Current date
$date= date("Y-m-d");
// Timestamp of new date after adding 1 month
$timestamp= strtotime(date("Y-m-d", strtotime($date)) . "+1 month");
//Converting timestamp of new date to readable date.
$newdate= date("Y-m-d",$timestamp);
?>