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

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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵