Given a date and the task is to print the last day of the month. We will use date() and strtotime() function to get the last day of the month.
Used PHP functions:
- date() function: Format a local time/date.
- strtotime() function: Parse about any English textual datetime description into a Unix timestamp.
Input: 2020-04-23 Output: Thursday Input: '2018-09-11' Output: Sunday
- Given a date stored in a variable as a string, the step is to convert it into date format using strtotime() function.
- Once we get the date, we will employ date() method.
date( $format, $timestamp )
- Within $format, we will pass “Y-m-t” and within $timestamp the date taken above. In terms of date “Y” gives a full numeric representation of a year in 4 digits, “m” provides numeric representation of a month and “t” gives the number of days in the given month.
- By using “Y-m-t”, we will get the number of days in a month because of “t”, “m” and “Y” will provide the month and the year.
- The last step is to use the date() again with “l” for format and the date achieved above as $timestamp.
- Display the day.
- How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
- How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
- How to get first day of week in PHP?
- How to convert number to month name in PHP?
- How to get the last n characters of a PHP string?
- Last-child and last-of-type selector in SASS
- How to get the current date and time in seconds?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.