the red penguin
HOME ABOUT SITEMAP BLOG LOGIN

Finding the first and last day of a month in PHP

Some code to find the first and last day of this current month, and then pushing that on month by month (obviously using a loop and arrays would be neater).

<?

$start = date('Y-m-01');
$end = date('Y-m-t');

echo $start . " to " . $end . "<br />";

$start2 = date('Y-m-d', strtotime("+1 day", strtotime($end)));
$d = new DateTime($start2);
$end2 = $d->format('Y-m-t');

echo $start2 . " to " . $end2 . "<br />";

$start3 = date('Y-m-d', strtotime("+1 day", strtotime($end2)));
$d = new DateTime($start3);
$end3 = $d->format('Y-m-t');

echo $start3 . " to " . $end3 . "<br />";

?>

Output (assuming today’s date of 2022-02-01):

2022-02-01 to 2022-02-28
2022-03-01 to 2022-03-31
2022-04-01 to 2022-04-30
Tuesday 1 February 2022, 542 views


Leave a Reply

Your email address will not be published. Required fields are marked *