简体   繁体   English

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

[英]PHP strtotime to get the last day of a given year

How can i get the last day of a year passed as parameter using strtotime? 如何使用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;

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: 您可以这样操作,只需将一年中的最后一天(12月31日)发送到strtotime函数,例如获取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: 或者,您可以将所需的年份存储在变量中(如果要在函数中使用它,或者年份可以动态更改):例如,获取2000年至今的所有最近的日期名称:

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

Hope I pushed you further. 希望我能进一步推动您。

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;

You don't even need strtotime (even though you could definitely use it). 您甚至不需要strtotime(即使您绝对可以使用它)。 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 但是可以满足您的需要

echo date('l',strtotime(date('Y')));

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM