How can I get the last day of the month in PHP?
Given:
$a_date = "2009-11-23"
I want 2009-11-30; and given
$a_date = "2009-12-23"
I want 2009-12-31.
t
returns the number of days in the month of a given date (see the docs for date
):
$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));
The code using strtotime() will fail after year 2038. (as given in the first answer in this thread) For example try using the following:
$a_date = "2040-11-23";
echo date("Y-m-t", strtotime($a_date));
It will give answer as: 1970-01-31
So instead of strtotime, DateTime function should be used. Following code will work without Year 2038 problem:
$d = new DateTime( '2040-11-23' );
echo $d->format( 'Y-m-t' );
I know this is a little bit late but i think there is a more elegant way of doing this with PHP 5.3+
by using the DateTime class :
$date = new DateTime('now');
$date->modify('last day of this month');
echo $date->format('Y-m-d');
There is also the built in PHP function cal_days_in_month()?
"This function will return the number of days in the month of year for the specified calendar." http://php.net/manual/en/function.cal-days-in-month.
echo cal_days_in_month(CAL_GREGORIAN, 11, 2009);
// = 30
This should work:
$week_start = strtotime('last Sunday', time());
$week_end = strtotime('next Sunday', time());
$month_start = strtotime('first day of this month', time());
$month_end = strtotime('last day of this month', time());
$year_start = strtotime('first day of January', time());
$year_end = strtotime('last day of December', time());
echo date('D, M jS Y', $week_start).'<br/>';
echo date('D, M jS Y', $week_end).'<br/>';
echo date('D, M jS Y', $month_start).'<br/>';
echo date('D, M jS Y', $month_end).'<br/>';
echo date('D, M jS Y', $year_start).'<br/>';
echo date('D, M jS Y', $year_end).'<br/>';
What is wrong -
The most elegant for me is using DateTime
I wonder I do not see DateTime::createFromFormat
, one-liner
$lastDay = \DateTime::createFromFormat("Y-m-d", "2009-11-23")->format("Y-m-t");
You can find last day of the month several ways. But simply you can do this using PHP strtotime() and date() function.I'd imagine your final code would look something like this:
$a_date = "2009-11-23";
echo date('Y-m-t',strtotime($a_date));
But If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$a_date = "2009-11-23";
$date = new DateTime($a_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');
Also, you can solve this using your own function like below:
/**
* Last date of a month of a year
*
* @param[in] $date - Integer. Default = Current Month
*
* @return Last date of the month and year in yyyy-mm-dd format
*/
function last_day_of_the_month($date = '')
{
$month = date('m', strtotime($date));
$year = date('Y', strtotime($date));
$result = strtotime("{$year}-{$month}-01");
$result = strtotime('-1 second', strtotime('+1 month', $result));
return date('Y-m-d', $result);
}
$a_date = "2009-11-23";
echo last_day_of_the_month($a_date);
If you use the Carbon API extension for PHP DateTime, you can get the last day of the month with:
$date = Carbon::now();
$date->addMonth();
$date->day = 0;
echo $date->toDateString(); // use toDateTimeString() to get date and time
Carbon API extension for PHP DateTime
Carbon::parse("2009-11-23")->lastOfMonth()->day;
or
Carbon::createFromDate(2009, 11, 23)->lastOfMonth()->day;
will return
30
There are ways to get last day of month.
//to get last day of current month
echo date("t", strtotime('now'));
//to get last day from specific date
$date = "2014-07-24";
echo date("t", strtotime($date));
//to get last day from specific date by calendar
$date = "2014-07-24";
$dateArr=explode('-',$date);
echo cal_days_in_month(CAL_GREGORIAN, $dateArr[1], $dateArr[0]);
I am late but there are a handful of easy ways to do this as mentioned:
$days = date("t");
$days = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
$days = date("j",mktime (date("H"),date("i"),date("s"),(date("n")+1),0,date("Y")));
Using mktime() is my go to for complete control over all aspects of time... I.E.
echo "<br> ".date("Y-n-j",mktime (date("H"),date("i"),date("s"),(11+1),0,2009));
Setting the day to 0 and moving your month up 1 will give you the last day of the previous month. 0 and negative numbers have the similar affect in the different arguements. PHP: mktime - Manual
As a few have said strtotime isn't the most solid way to go and little if none are as easily versatile.
I have wrapped it in my date time helper class here
https://github.com/normandqq/Date-Time-Helper
using
$dateLastDay = Model_DTHpr::getLastDayOfTheMonth();
And it is done
$startDate = '2011-12-01';
$endDate = date('Y-m');
while (true) {
try {
$startDateTime = new DateTime($startDate);
$startDateTime->add(new DateInterval('P1M'));
$startDate = $startDateTime->format('Y-m-d');
$endTime = $startDateTime->format('Y-m-t');
echo $startDate . ' => ' . $endTime . PHP_EOL;
if ($startDateTime->format('Y-m') == $endDate) {
break;
}
} catch (Exception $exception) {
var_dump($exception->getMessage());
break;
}
}
After testing many solutions, this works best for me.
function first_last_day($string, $first_last, $format) {
$result = strtotime($string);
$year = date('Y',$result);
$month = date('m',$result);
$result = strtotime("{$year}-{$month}-01");
if ($first_last == 'last'){$result = strtotime('-1 second', strtotime('+1 month', $result)); }
if ($format == 'unix'){return $result; }
if ($format == 'standard'){return date('Y-m-d', $result); }
}
You can use "t
" in date function to get the number of day in a particular month.
The code will be something like this:
function lastDateOfMonth($Month, $Year=-1) {
if ($Year < 0) $Year = 0+date("Y");
$aMonth = mktime(0, 0, 0, $Month, 1, $Year);
$NumOfDay = 0+date("t", $aMonth);
$LastDayOfMonth = mktime(0, 0, 0, $Month, $NumOfDay, $Year);
return $LastDayOfMonth;
}
for($Month = 1; $Month <= 12; $Month++)
echo date("Y-n-j", lastDateOfMonth($Month))."\n";
The code is self-explained. So hope it helps.