php date 函数格式化
t 指定月份的天数; 如: “28” 至 “31”
$date = '2018-08-08';
echo date('Y-m-t',strtotime($date));
strtotime 字符串时间修饰词
last day of this month 时间字符 类似我们常说的 -1 day
echo date('Y-m-d',strtotime("last day of this month",strtotime('2018-02-01')));
echo date('Y-m-d',strtotime("last day of 2018-02"));
php DateTime类 面向对象方式
$date = new \DateTime('2000-02-01');
$date->modify('last day of this month');
echo $date->format('Y-m-d');
其实这题主要是我们常见的面试题演变的。主要是想看怎么考虑问题,很多的时候,我们陷入了一个误区里,考虑了复杂的实现,其实就是两个函数的使用,一个是 date 和 strtotime
求昨天的日期,strtotime('-1 day')
当然使用php内置函数 时最简单的,但是很多时候第一时间没有想到或者不知道内置函数有这个功能,就会采用前两种方法。所以说 PHP内置函数还是很香的!!!!
接下来
本文介绍了如何使用PHP的内置函数date、strtotime、DateTime和mktime来获取指定月份的最后一天日期。重点讨论了使用date结合strtotime的'last day of this month'修饰词以及DateTime类的方法。这些技巧在面试中常被考察,并强调了PHP内置函数的便利性。
订阅专栏 解锁全文
384

被折叠的 条评论
为什么被折叠?



