开发者

How to find a start date & end date of any given year & month

I have problem in php find start date & end date of month & year , when i know the year and month ?

ex:

input - > year = 2011 , month = 08
output -> start date = 01 , 开发者_开发百科end date = 31


echo date('m-01-Y 00:00:00',strtotime('this month')) . '<br/>';
echo date('m-t-Y 12:59:59',strtotime('this month')) . '<br/>';


Start date will always be 1 and you can find the end date with the following function.

cal_days_in_month(CAL_GREGORIAN, $month, $year);

reference:

cal_days_in_month ( int $calendar , int $month , int $year ) : int


Use date (t format gives days in year) and create a time for it:

$year = 2011; $month = 6;

$starts = 1;
$ends = date('t', strtotime($month.'/'.$year)); //Returns days in month 6/2011


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


You should look into 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


PHP may have a more elegant way of doing this, but if you want a generic algorithm, here's what you need to do...

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:

  1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
  2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
  3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
  4. The year is a leap year (February has 29 days).
  5. The year is not a leap year (February has 28 days).


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();`


$year = '2017';
$month = '05';
echo date("$year-$month-01");
echo "<br>";
echo date("$year-$month-t");

shortest solution in my own opinion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜