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 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路
  • ¥15 内网办公电脑进行向日葵
  • ¥15 如何输入双曲线的参数a然后画出双曲线?我输入处理函数加上后就没有用了,不知道怎么回事去掉后双曲线可以画出来
  • ¥15 soildworks装配体的尺寸问题
  • ¥100 有偿寻云闪付SDK转URL技术