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

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 MapReduce实现倒排索引失败
  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题