Write a PHP script to get the first and last day of a month from a specified date. Sample Solution: PHP Code: Sample Output: Flowchart : PHP Code Editor: Have another way to solve this solution? Contribute your code [and comments] through Disqus. Previous: Write a PHP script to calculate number of days between two dates.PHP date: Exercise-8 with Solution
First day : 2008-02-01 - Last day : 2008-02-29
Next: Write a PHP script to print like : Saturday the 7th
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
PHP: Tips of the Day
PHP: PHP cURL custom headers
curl_setopt[$ch, CURLOPT_HTTPHEADER, array[ 'X-Apple-Tz: 0', 'X-Apple-Store-Front: 143444,12' ]];
Ref : //bit.ly/2HgTwMp
- Exercises: Weekly Top 16 Most Popular Topics
- SQL Exercises, Practice, Solution - JOINS
- SQL Exercises, Practice, Solution - SUBQUERIES
- JavaScript basic - Exercises, Practice, Solution
- Java Array: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : Conditional Statement
- HR Database - SORT FILTER: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : String
- Python Data Types: Dictionary - Exercises, Practice, Solution
- Python Programming Puzzles - Exercises, Practice, Solution
- C++ Array: Exercises, Practice, Solution
- JavaScript conditional statements and loops - Exercises, Practice, Solution
- C# Sharp Basic Algorithm: Exercises, Practice, Solution
- Python Lambda - Exercises, Practice, Solution
- Python Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
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.
Examples:
Input: 2020-04-23 Output: Thursday Input: '2018-09-11' Output: Sunday
Approach:
- 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.
Syntax: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.
Program:
get last date of month in php : Php time timestamp Get the first day and last day of the current month, last month, next month, etc.
- get last date of month in php
- PHP Get last day of month
- Using the DateTime object.
- php get start and end date of month and year
- Last day of the month.
- get last month php
- Related posts
PHP DateTime to Get the Last Date of the Month – How to get last day of a month from date in PHP ? – for example ‘last day of’, Sets the day to the last day of the current month.
PHP: Get last day of month
This month.
The last day of this month: