How to get first and last day of the month with php
Using PHP’s mktime function , the last day of any given month can be expressed as the “0″ day of the next month , therefore :
$lastDayOftheMonthTimestamp = mktime(0,0,0,$currentMonthNumeric + 1,0,$currentFullYear);
Full example :
$currentTime = time(); $dateTime = getdate($currentTime); $currentFullYear = $dateTime["year"]; $currentMonthNumeric = $dateTime["mon"]; $firstDayOfTheMonth = date('Y-m-d',mktime(0,0,0,$currentMonthNumeric,1,$currentFullYear)); $lastDayOfTheMonth = date('Y-m-d',mktime(0,0,0,$currentMonthNumeric + 1,0,$currentFullYear)); echo $firstDayOfTheMonth.PHP_EOL; echo $lastDayOfTheMonth .PHP_EOL;
Result :
2009-02-01 2009-02-28
Comments
One Response to “How to get first and last day of the month with php”
Leave a Reply
I was looking for this, thanks!!