最近,发现了 strtotime 语法的一些妙用,记录一下:
有时候如果想要获取特殊日期,如:上周六,下周六,又或是指定日期的上周六,很多时候可以通过 strtotime 来得到这个日期。
定义和用法
strtotime() 函数将任何英文文本的日期或时间描述解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数)。
注意:如果年份表示使用两位数格式,则值 0-69 会映射为 2000-2069,值 70-100 会映射为 1970-2000。
注意:请注意 m/d/y 或 d-m-y 格式的日期,如果分隔符是斜线(/),则使用美洲的 m/d/y 格式。如果分隔符是横杠(-)或者点(.),则使用欧洲的 d-m-y 格式。为了避免潜在的错误,您应该尽可能使用 YYYY-MM-DD 格式或者使用 date_create_from_format() 函数。
语法
strtotime(time,now);
参数 | 描述 |
---|---|
time | 必需。规定日期/时间字符串。 |
now | 可选。规定用来计算返回值的时间戳。如果省略该参数,则使用当前时间。 |
以上是文档的官方解释。
举例一些特殊的用法:
如
这个月的最后一个周日:
strtotime('last sunday of this month'),
这个月的最后一个周日晚上八点:
date('Y-m-d 20:00:00', strtotime('last sunday of this month'))
这个月的最后一天:
strtotime('last day of this month')
指定日期的最后一个周日
strtotime('last sunday ', strtotime('2022-10-29'))
依次类推,只需要知道一些英文单词就可以得到
first:第一个
last:最后一个
Monday--Sunday等等....