【问题标题】:Only future dates with PHP strtotime()只有 PHP strtotime() 的未来日期
【发布时间】:2016-08-09 14:28:59
【问题描述】:

PHP 的 strtotime() 默认使用当前年份。如何只获取未来的日期?

echo date('l d. M Y', strtotime('first sunday of april')); // Sunday 03. Apr 2016

我无法获得下一个四月的第一个星期日。日期不能是过去的日期,并且必须始终是相对的(不能硬编码 2017 或 2018)。

echo date('l d. M Y', strtotime('first sunday of next april')); // fails

echo date('l d. M Y', strtotime('first sunday of april next year')); // wrong from January until that Sunday in April

我想我可以分多个步骤执行此操作,或者创建一个函数来检查当前时间是否在第一个星期日之前/之后,并在末尾插入“明年”。

但我想知道 strtotime() 是否有一个简单的解决方案

【问题讨论】:

  • strtotime 有时看起来很神奇,但它不是人工智能,也不是无所不知的。如果它无法解析你正在喂它的东西,有时你只需要牵着它的鼻子,并提供一个带有可选第二个参数的基本时间戳,例如提供“2017 年 1 月 1 日”作为基准时间
  • 这是一个很好的观点,但我认为它在我的情况下效果不佳,因为您不能只省略关键字“今年”或“明年”。无论如何,你是对的...... PHP 人员有空间添加更多的精灵灰尘 :-)

标签: php strtotime


【解决方案1】:

我不认为这特别优雅,但它确实有效,我希望它是你想要的?

echo date('l d. M Y', strtotime('first sunday of april', strtotime('first day of next year')));

然而,这似乎是一个更好、可维护、易读的解决方案

$d = new DateTime();
$d->modify( 'first day of next year');
echo $d->format('l d. M Y') . PHP_EOL;
$d->modify( 'first sunday of april');
echo $d->format('l d. M Y') . PHP_EOL;

这给了

Tuesday 01. Aug 2017
Sunday 02. Apr 2017

年份变化日期的回声,你不需要做,它只是为了证明年份变化

【讨论】:

  • 感谢@Mitkosoft,因为我基本上只是将我的修改应用于他的回答建议的 DateTime 类,但不再需要硬编码年份。希望这不会冒犯你 mitko
  • 谢谢大家!我要使用类似... ( date('m')
【解决方案2】:

我来这里是为了解决这篇文章的标题。我想每次都获得 strtotime 结果的未来日期。

date("Y-m-d", strtotime("Jan 2"));

如果今天的日期是 2018 年 1 月 1 日,它将返回未来的日期 2018-01-02,但如果今天的日期是 2018 年 1 月 3 日,它将返回相同的日期(现在是过去的日期)。在那种情况下,我宁愿回到 2019-01-02。

我知道 OP 说他们不想要函数,但这似乎是最简单的解决方案。所以,这是我为获取下一个匹配的未来 strtotime 所做的快速函数。

function future_strtotime($d){
    return (strtotime($d)>time())?strtotime($d):strtotime("$d +1 year");
}

使用...获得美好的约会

date("Y-m-d", future_strtotime("Jan 2"));

【讨论】:

    【解决方案3】:

    这是一个更强大的解决方案。它替换了strtotime,但需要第二个参数——pastfuture 的字符串,并根据其进行偏移。

    <?php
    function strtotimeForce($string, $direction) {
        $periods = array("day", "week", "month", "year");
        if($direction != "past" && $direction != "future") return strtotime($string);
        else if($direction == "past" && strtotime($string) <= strtotime("now")) return strtotime($string);
        else if($direction == "future" && strtotime($string) >= strtotime("now")) return strtotime($string);
        else if($direction == "past" && strtotime($string) > strtotime("now")) {
            foreach($periods as $period) {
                if(strtotime($string) < strtotime("+1 $period") && strtotime($string, strtotime("-1 $period"))) {
                    return strtotime($string, strtotime("-1 $period"));
                }
            }
            return strtotime($string);
        }
        else if($direction == "future" && strtotime($string) < strtotime("now")) {
            foreach($periods as $period) {
                if(strtotime($string) > strtotime("-1 $period") && strtotime($string, strtotime("+1 $period")) > strtotime("now")) {
                    return strtotime($string, strtotime("+1 $period"));
                }
            }
            return strtotime($string);
        }
        else return strtotime($string);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      相关资源
      最近更新 更多