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

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.

    已采纳该答案
    打赏 评论
  • douyong6589 2018-10-15 14:28

    i think you get year from user input . u refer this one

    $lastdate="-12-31";
    $year=$_POST['year'];//lets say you get yearr from post
    $day=date('l',strtotime($year.$lastdate));
    echo $day;
    
    打赏 评论
  • dongwei9365 2018-10-15 14:29

    You don't even need strtotime (even though you could definitely use it). If you get the year as an input parameter:

    $year = '2018';
    $lastday = date($year.'-12-31');
    echo $lastday;
    

    This is a simple hack that only works because you're actually building the same calendar date and only changing the year. But does the trick for what you need

    打赏 评论
  • doulutian4843 2018-10-15 14:31
    echo date('l',strtotime(date('Y')));
    
    打赏 评论
  • drdr123456 2018-10-15 14:43

    You need to write code like this

    $yearEnd2015 = date('Y-m-d D', strtotime('2015-12-31'));
    $yearEnd2016 = date('Y-m-d D', strtotime('2016-12-31'));
    $yearEnd2017 = date('Y-m-d D', strtotime('2017-12-31'));
    $yearEnd     = date('Y-m-d D', strtotime('2018-12-31'));
    
    echo " <br>2015 : ".$yearEnd2015;
    echo " <br>2016 : ".$yearEnd2016;
    echo " <br>2017 : ".$yearEnd2017;
    echo " <br>2018 : ".$yearEnd;
    
    打赏 评论

相关推荐 更多相似问题