php 如何查找任何给定年份和月份的开始日期和结束日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7184953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find a start date & end date of any given year & month
提问by Jayanath
I have problem in php find start date & end date of month & year , when i know the year and month ?
当我知道年份和月份时,我在 php 中查找开始日期和结束日期的月份和年份有问题?
ex:
前任:
input - > year = 2011 , month = 08
output -> start date = 01 , end date = 31
回答by Roshan Wijesena
echo date('m-01-Y 00:00:00',strtotime('this month')) . '<br/>';
echo date('m-t-Y 12:59:59',strtotime('this month')) . '<br/>';
回答by Joe Landsman
Start date will always be 1 and you can find the end date with the following function.
开始日期将始终为 1,您可以使用以下函数找到结束日期。
cal_days_in_month(CAL_GREGORIAN, $month, $year);
cal_days_in_month ( int $calendar , int $month , int $year ) : int
cal_days_in_month ( int $calendar , int $month , int $year ) : int
回答by Bailey Parker
Use date (t format gives days in year) and create a time for it:
使用日期(t 格式给出一年中的天数)并为其创建一个时间:
$year = 2011; $month = 6;
$starts = 1;
$ends = date('t', strtotime($month.'/'.$year)); //Returns days in month 6/2011
回答by Marco
i really can't understand you clearly but to get the start date here is the code
我真的无法清楚地理解你,但要获得开始日期,这里是代码
date('Y-m-d');
this code above will get you the day of today and to get the end of the running month this code i used before
上面的这段代码将使您获得今天的日期,并获得我之前使用的运行月份的结束时间
date('Y-m-d',strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00′))));
i hope this help you in your issue
我希望这对您的问题有所帮助
回答by K Mehta
PHP may have a more elegant way of doing this, but if you want a generic algorithm, here's what you need to do...
PHP 可能有一种更优雅的方式来做到这一点,但如果你想要一个通用算法,你需要做的是......
All months other than February have a fixed number of days. February has 29 only when it's a leap year. Here are the rules to check if it's a leap year:
除二月以外的所有月份都有固定的天数。2 月只有在闰年时才有 29。以下是检查是否为闰年的规则:
- If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
- If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
- If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
- The year is a leap year (February has 29 days).
- The year is not a leap year (February has 28 days).
- 如果年份能被 4 整除,则转至步骤 2。否则转至步骤 5。
- 如果年份能被 100 整除,则转至步骤 3。否则转至步骤 4。
- 如果年份能被 400 整除,则转至步骤 4。否则转至步骤 5。
- 这一年是闰年(二月有29天)。
- 这一年不是闰年(二月有 28 天)。
回答by Givantha Kalansuriya
hi Try this way u can do it
嗨试试这种方式你可以做到
function firstOfMonth() {
return date("m/d/Y", strtotime(date('m').'/01/'.date('Y').' 00:00:00'));
}
function lastOfMonth() {
return date("m/d/Y", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));
}
$date_start = firstOfMonth();
$date_end = lastOfMonth();`
回答by cwallenpoole
You should look into strtotime:
你应该看看strtotime:
echo date("D, M j, Y", strtotime("FIRST DAY OF MAY 2012"));
// Tue, May 1, 2012
echo date("D, M j, Y", strtotime("last DAY june 2012")); // gotcha! using June.
// Thu, May 31, 2012
回答by Jaylord Ferrer
$year = '2017';
$month = '05';
echo date("$year-$month-01");
echo "<br>";
echo date("$year-$month-t");
shortest solution in my own opinion.
我个人认为最短的解决方案。