Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP中m-d-Y H:i:s格式的日期中减去2天_Php_Date_Datetime - Fatal编程技术网

从PHP中m-d-Y H:i:s格式的日期中减去2天

从PHP中m-d-Y H:i:s格式的日期中减去2天,php,date,datetime,Php,Date,Datetime,我使用以下代码获取当前日期: $currentdate= date('m-d-Y H:i:s'); echo $currentdate; // prints 06-22-2019 11:02:49 $date = date('m-d-Y H:i:s ', strtotime('-2 days', strtotime($currentdate))); echo $date; // print's 12-30-1969 01:00:00 要从当前日期减去两天,我使用以下代码:

我使用以下代码获取当前日期:

$currentdate= date('m-d-Y H:i:s');

echo $currentdate; // prints 06-22-2019 11:02:49
 $date = date('m-d-Y H:i:s ', strtotime('-2 days', strtotime($currentdate)));
 echo $date; // print's 12-30-1969 01:00:00       
要从当前日期减去两天,我使用以下代码:

$currentdate= date('m-d-Y H:i:s');

echo $currentdate; // prints 06-22-2019 11:02:49
 $date = date('m-d-Y H:i:s ', strtotime('-2 days', strtotime($currentdate)));
 echo $date; // print's 12-30-1969 01:00:00       
预期输出为2019年6月20日11:02:49//基本要求是日期应为当前日期-2天。
我做错了什么?如果日期是Y-m-d H:i:s格式,则此代码非常有效。

因此,根据此问题评论中的建议,我能够使用此代码通过将
-
替换为
/
获得输出:

$currentdate= date('m/d/Y H:i:s'); //prints 06/22/2019 11:26:05
$date = date('m/d/Y H:i:s ', strtotime('-2 days', strtotime($currentdate)));
echo str_replace('/', '-', $date);   //prints 06-20-2019 11:20:50 which is the desired output

因此,根据对这个问题的评论中的建议,我能够通过将
-
替换为
/
来获得使用此代码的输出:

$currentdate= date('m/d/Y H:i:s'); //prints 06/22/2019 11:26:05
$date = date('m/d/Y H:i:s ', strtotime('-2 days', strtotime($currentdate)));
echo str_replace('/', '-', $date);   //prints 06-20-2019 11:20:50 which is the desired output

检查php日期时间格式没有
m“-”d“-”y
格式

但是您可以使用
M“-”d“-”y
M“-”d“-”y


检查php日期时间格式没有
m“-“d”-“y
格式

但是您可以使用
M“-”d“-”y
M“-”d“-”y


strotime理解m/d/Y和d-m-Y,因为
strotime
不支持
m-d-Y
。好的,谢谢您的建议。那么,在这种情况下,如何实现预期的输出呢?将“-”替换为“/”perfect。这很有效。现在我将用-strotime理解m/d/Y和d-m-Y替换输出,因为
strotime
不支持
m-d-Y
。好的,谢谢您的建议。那么,在这种情况下,如何实现预期的输出呢?将“-”替换为“/”perfect。这很有效。现在,我将把输出替换为-您仍然可以使用模板
m-d-Y H:I:s
显示日期。只需
strotime('-2天',strotime(str_replace('-','/',$currentdate))
您仍然可以使用模板显示日期
m-d-Y H:i:s
。只需
strotime('-2天',strotime(str_替换('-','/',$currentdate))