dskm94301 2018-10-15 14:18
浏览 736
已采纳

PHP strtotime获取给定年份的最后一天

How can i get the last day of a year passed as parameter using strtotime?

I try this but the results are not the correct last days :

$yearEnd2015 = date('Y-m-d', strtotime('last day of december this year -3'));
    $yearEnd2016 = date('Y-m-d', strtotime('last day of december this year -2'));
    $yearEnd2017 = date('Y-m-d', strtotime('last day of december this year -1'));
    $yearEnd     = date('Y-m-d', strtotime('last day of december this year'));

    echo " <br>2015 : ".$yearEnd2015;
    echo " <br>2016 : ".$yearEnd2016;
    echo " <br>2017 : ".$yearEnd2017;
    echo " <br>2018 : ".$yearEnd;
  • 写回答

5条回答 默认 最新

  • dqvj51875 2018-10-15 14:36
    关注

    You can do it like this, just send the last day in the year(31 Dec) to the strtotime function, for example to get the date name of the last day in 2015:

    $yearEnd2015 = date('l', strtotime('2015-12-31'));
    echo $yearEnd2015;
    

    Or you can store the desired year in a variable(if you want to use it in a function or the year changes dynamically): for example to get all the last date names from 2000 till now:

    $year = 2000;
    while($year++ <= 2018) {
      $yearEnd = date('l', strtotime($year . '-12-31'));
      echo $yearEnd . '<br />';
    }
    

    Hope I pushed you further.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?