使用strtotime的总和总是返回01/01/1970

问题描述 投票:-2回答:2

你好,我想用strotime总结一个日期到日期的1天,但我听不懂,总是返回02/01/1970

$date = date ("d/m/Y H:i:s", filemtime($directory));
$newdate = date("d/m/Y", strtotime($date));
$tomorrow = date('d/m/Y',strtotime($newdate . "+1 days"));
echo $tomorrow; //Always return 02/01/1970
php strtotime
2个回答
0
投票

因为strtotime()通过查看日期分隔符来区分美国日期格式和明智的日期格式,如果您想在中间日期中使用明智的日期格式(例如,像这样),您需要做的就是使用-分隔符操作

$date = date ("d-m-Y H:i:s", filemtime($directory));
$newdate = date("d-m-Y", strtotime($date));
$tomorrow = date('d/m/Y',strtotime($newdate . "+1 days"));
echo $tomorrow; //Always return 02/01/1970

从手册开始

注意:

m / d / y或d-m-y格式的日期通过查看各个组成部分之间的分隔符来消除歧义:如果分隔符为斜杠(/),则假定为美国m / d / y;相反,如果分隔符是破折号(-)或点(。),则采用欧洲d-m-y格式。但是,如果年份以两位数字格式给出,并且分隔符为破折号(-),则日期字符串将解析为y-m-d。

为了避免潜在的歧义,最好在可能的情况下使用ISO 8601(YYYY-MM-DD)日期或DateTime :: createFromFormat()。


0
投票

更好地使用DateTime()

$date = new DateTime(strtotime(filemtime($directory)));
echo $newdate = $date->format('d/m/Y');
$date->modify('+1 day');
echo $tomorrow = $date->format('d/m/Y');

输出:

20/01/2020
21/01/2020

0
投票

如果filemtime($directory)返回格式化为date()掩码的字符串,我的意思是d/m/Y H:i:s,则可以执行下一步:

  • 例如,根据此蒙版,它看起来像:
$s = "02/06/2019 22:23:22";
  • 现在您可以执行strtotime()
$date = date ("d/m/Y H:i:s", strtotime($s));
  • 然后将其转换为DateTime对象
$st_date = new DateTime($date); 
  • 现在您可以根据需要简单地对其进行修改
$st_date->modify('+1 days'); 
  • 查看结果字符串值使用:
$tomorrow = $st_date->format('d/m/Y');
echo 'tomorrow -> '.$tomorrow;

输出:

date->02/06/2019 22:23:22
tomorrow -> 03/06/2019

Demo

© www.soinside.com 2019 - 2024. All rights reserved.