PHP的函数 strtotime 的一次使用

今天遇到一个小问题,页面中需要显示某个月份,最后一天的日期

常规按天计算,习惯用 86400 做天数的秒,来做加减
其实strtotime还是很强大的

// 明天的这个时间
echo strtotime('-1 day');
// 一小时以后
echo strtotime('+1 hour');
// 三月以前
echo strtotime('-3 month');
// 根据指定时间
strtotime('2016-12-22 -3 month');

3月的最后一天

    $month = 3;
    $monthFirstDay = date('Y') . '-' . $month . '-01';
    $monthLastDay = date('Y年n月j日', strtotime("$monthFirstDay +1 month -1 day"));

后来发现,还有last day of 的用法

$year = '2016'
$month = '05';
// 年月格式的
$lastday = date( "Y-m-d", strtotime( "last day of {$year}-{$month}" ) );
// 年月日格式的,
$lastday = date( "Y-m-d", strtotime( "last day of {$year}-{$month}-01" ) );

这些方法都是基础,平时用的不多的,就很容易被忽略

参考及扩展阅读:
https://www.pocketdigi.com/20130713/1138.html
http://php.net/manual/zh/function.date.php
http://php.net/manual/zh/datetime.formats.relative.php