PHP自带函数实现
  1. php date 函数格式化
    t 指定月份的天数; 如: “28” 至 “31”
  1. $date = '2018-11-02';
  2. echo date('Y-m-t',strtotime($date));//结果 2018-11-30
  3. $date = '2018-02-02';
  4. echo date('Y-m-t',strtotime($date));//结果 2018-02-28
  1. strtotime 字符串时间修饰词
  1. last day of this month 时间字符 类似我们常说的 -1 day
  2. echo date('Y-m-d',strtotime('last day of this month',strtotime('2018-02-02')));//结果 2018-02-28
  3. echo date('Y-m-d',strtotime('last day of 2018-02'));//结果 2018-02-28
  1. php DateTime类 面向对象方式
  1. $date = new \DateTime('2018-02-02');
  2. $date->modify('last day of this month');
  3. echo $date->format('Y-m-d');////结果 2018-02-28