简体   繁体   English

PHP:不正确的strtotime()

[英]PHP: Incorrect strtotime()

I had gone through various stackoverflow solutions and other blogs but still it doesn't fix my problem. 我经历了各种stackoverflow解决方案和其他博客,但是仍然不能解决我的问题。

Let's say that the date today is: 2013-12-28 and I want to get the date after 1 month and it is supposed to display 2014-01-28 . 假设今天的日期是: 2013-12-28 ,我想在1个月后得到日期,并且应该显示2014-01-28

$date = date('o-m-d');
$final = date('o-m-d', strtotime("+1 month", $date));
echo $final;

Above is my code. 以上是我的代码。 It returns 02/01/1970 . 它返回02/01/1970

I have also tried the mktime method but still it displays the 1970 output. 我也尝试了mktime方法,但仍然显示1970输出。

What am I doing wrong? 我究竟做错了什么?

BTW. 顺便说一句。 I am working this on a hosted server. 我正在托管服务器上工作。

Thanks ahead. 谢谢你 :) :)

Use DateTime function modify 使用DateTime函数修改

$date = new DateTime( 'o-m-d' );  
echo $date->modify( '+1 month' )->format('o-m-d');

If you want the current date +1 month use: 如果您希望当前日期+1个月,请使用:

$final = date('o-m-d', strtotime("+1 month"));

Or with a given date: 或指定日期:

$date = date('o-m-d');
$final = date('o-m-d', strtotime($date . " +1 month"));
echo $final;

If you want to use the second parameter of strtotime it has to be a timestamp. 如果要使用strtotime的第二个参数,则必须使用时间戳记。

Go the OOP way.. OOP方式。

<?php
$date = new DateTime('2013-12-28');
$date->add(new DateInterval('P1M'));
echo $date->format('Y-m-d'); //prints 2014-01-28

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM