Jayanath
Jayanath

Reputation: 220

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

Upvotes: 14

Views: 26147

Answers (8)

Joe Landsman
Joe Landsman

Reputation: 2177

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

Upvotes: 8

Jaylord Ferrer
Jaylord Ferrer

Reputation: 95

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

shortest solution in my own opinion.

Upvotes: -1

Givantha Kalansuriya
Givantha Kalansuriya

Reputation: 65

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

Upvotes: 2

Bailey Parker
Bailey Parker

Reputation: 15905

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

Upvotes: 4

K Mehta
K Mehta

Reputation: 10533

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).

Upvotes: 2

Marco
Marco

Reputation: 867

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

Upvotes: 3

cwallenpoole
cwallenpoole

Reputation: 81988

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

Upvotes: 2

Roshan Wijesena
Roshan Wijesena

Reputation: 3136

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

Upvotes: 28

Related Questions