-1

我正在尝试将 1 天添加到使用日期,strtotime但我无法让它工作。它总是返回 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
4

3 回答 3

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 或 dmy 格式的日期通过查看各个组件之间的分隔符来消除歧义:如果分隔符是斜杠 (/),则假定为美式 m/d/y;而如果分隔符是破折号 (-) 或点 (.),则假定为欧洲 dmy 格式。但是,如果年份以两位数格式给出并且分隔符是破折号 (-),则日期字符串将被解析为 ymd。

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

于 2020-01-20T09:56:20.643 回答
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

演示

于 2020-01-20T10:04:22.243 回答
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
于 2020-01-20T10:02:49.287 回答