【问题标题】:PHP - reading date from json and modifying as date not stringPHP - 从json读取日期并修改为日期而不是字符串
【发布时间】:2022-01-28 18:14:21
【问题描述】:

我有一个 json 文件,它只是一个日期 - current_date.json:

["2022-01-28"]

然后我尝试用 PHP 读取这个日期,如下所示:

$string = file_get_contents("current_date.json");
$json = json_decode($string,true);

$end = date($json[0]);
$begin = date($end, strtotime("-1 days"));

这将我的 $end 变量作为字符串而不是日期,并且 $begin 变量没有被修改,它只是同一个字符串。

将这些变量作为日期的最佳方法是什么?

我这样做的原因是我想一次向后工作一天,此时我将覆盖 json 文件中的日期,以便下次运行该文件时它将获得前一天。

【问题讨论】:

  • 为什么将2022-01-28 作为第一个参数传递给date?该函数需要一个日期 format 作为第一个参数。
  • Read the Manual - date() SO 不能替代手册。手册应该是您的第一个停靠港,而不是 SO

标签: php date type-conversion


【解决方案1】:

$begin 值这样

$begin = date('Y-m-d',strtotime($end."-1 days")); // 2022-01-27

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-25
    • 2020-03-26
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多