【发布时间】: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 人员有空间添加更多的精灵灰尘 :-)