【问题标题】:strtotime function giving wrong output, How to solve? [duplicate]strtotime 函数输出错误,如何解决? [复制]
【发布时间】:2017-03-30 06:30:56
【问题描述】:

我在我的项目中使用了 php strtotime 函数,它工作正常,但是今天有用户报告了一个问题,我检查并发现 strtotime 函数没有给出正确的输出。 我的代码是:

echo date('M Y', strtotime('-1 month'));

它正在输出:

Mar 2017 instead of Feb 2017

我在代码在线工具上检查了上面的代码,它在那里给出了相同的输出,那么是 php 核心功能问题还是我犯了一些错误? 注意:我在网上查了以下代码

echo date('Y-m-d', strtotime('-1 month'));

它的输出让我很奇怪

2017-03-02

【问题讨论】:

  • 使用这个日期("MY", strtotime("-1 month", strtotime(date("F") . "1")) );

标签: php strtotime


【解决方案1】:
date("M Y", strtotime("-1 months"));

这是真的,但今天是 3 月 30 日,这就是为什么你没有得到你想要的准确结果。因此,我们应该使用每月的第一天,然后应用减法。

【讨论】:

  • 从 hanky 建议的问题中得到答案 stackoverflow.com/questions/9058523/…
  • 我需要在代码之前添加这一行 $base = strtotime(date('Y-m',time()) . '-01 00:00:01');然后通过在 strtotime 函数的第二个参数中使用这个变量,它解决了问题。
  • 没错,如果我们在stackoverflow本身中搜索,我们将有很多类似这个问题的问答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-14
  • 2014-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-06
相关资源
最近更新 更多