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, 648 views
Next post: How to create a simple Twitter bot Previous post: Javascript countdown timer
Programming index
- How to post text and images to Bluesky using PHP
- General PHP code snippets
- Date and time utilities in PHP
- How to get rid of “episodes and shows” from Spotify:
- Example of character counter using Bootstrap and jQuery
- How to create a simple Twitter bot
- Finding the first and last day of a month in PHP
- Javascript countdown timer
- Creating a WordPress theme from scratch
Leave a Reply