【问题标题】:Trying to find the previous six days given any date在给定任何日期的情况下尝试查找前六天
【发布时间】:2010-03-25 20:59:06
【问题描述】:

我使用 strtotime 在我的数据库中查找前一周和下周的条目很好,但我似乎找不到的是如果用户选择过去的日期,如何找到前六天。

以下是我如何知道今天和六天前的情况:

$today = date("Y-m-d");
$minus6 = date('Y-m-d', strtotime('-6 days'));

现在,我如何根据用户输入的内容将 $today 与 $dateString 进行切换? 根据我的谷歌搜索,我想到了类似的东西,但它没有产生任何结果:

$dateString = 2010-01-25; // for example
$minus6 = date('Y-m-d', strtotime('-6 days, $dateString');

我是否遗漏了一些有关日期、strtotime 或什么的基本信息?感谢您的帮助。

【问题讨论】:

    标签: php date strtotime


    【解决方案1】:

    strtotime 的第二个参数是计算第一个参数的时间戳:

    echo date('Y-m-d', strtotime('-6 days', strtotime($dateString));
    

    但你也可以这样做like Gavin suggested

    【讨论】:

      【解决方案2】:

      您应该将实际日期放在 strtotime() 的任何修饰符之前。例如:

      $dateString = 2010-01-25; // for example
      $minus6 = date('Y-m-d', strtotime('-6 days, $dateString'));
      

      应该变成:-

      $dateString = "2010-01-25"; // for example
      $minus6 = date('Y-m-d', strtotime("$dateString -6 days"));
      

      ...或根据 Gordon 的回答将其作为显式时间戳作为第二个参数传递给 strtotime()。

      【讨论】:

      • +1。不知道您可以一次完成日期字符串和修饰符。将单引号更改为双引号,因为单引号 $datestring 不会被评估。
      • 没错,我不确定自己是否有合适的首选方法。为发现引号而欢呼。
      • 好的,做到了。双引号使它起作用。但是,我认为让我感到困惑的是 '$dateString, -6 days' 之间的逗号。
      猜你喜欢
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多