How to get the last day of the month of a given date in PHP
- Table of Content
- Unix Timestamp
- The strtotime() function
- The date() function
- The DateTime class
- Getting the last day of the month from a given date in PHP
Given a date and the task to get the last day of the month in that date, this article explains how to achieve that in different methods and with examples in PHP.
Let's say for instance you have:
$date = "2022-01-13"
Since the month in this date is January, which has 31 days, the last day of the month should be 2022-01-31.
Let's briefly understand some important concepts here before going far.
Unix timestamp refers to the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC.
UTC (Coordinated Universal Time), also known as GMT(Greenwich Meridian Time) is the time at 0° longitude. Time at January 1, 1970, 00:00:00 UTC is referred to as Unix epoch.
Computers count time using Unix timestamp (ie the number of seconds that have elapsed since Unix epoch).
The strtotime() function
The strtotime() function is an in-built function in PHP used to parse a date or date-time into a Unix timestamp.
The time is a mandatory parameter that specifies the date/time string that we want to convert into timestamp.
The now is an optional parameter that specifies the timestamp used as a base for the calculation of relative dates. In most cases, you won't need it.
Below is an example of date conversion to a Unix timestamp:
<?php $date = "2022-01-13"; $timestamp = strtotime($date); echo $timestamp;
The above is the timestamp for the date "2022-01-13".
To get the timestamp for the current time now, you don't have to use the strtotime() function. You use the time() function, which is also known as the timestamp function.
<?php $timestamp = time(); echo $timestamp;
The date() function
This is an in-built PHP function that returns a string formatted from a given timestamp according to the specified date format string.
The format is a mandatory parameter, which is a string that specifies the format for the date to be returned.
The timestamp is an optional parameter, which is the integer Unix timestamp value of a date/time. It is only optional for the current date and time right now. In such a case, the function defaults to the value of time(). Else, you must specify it.
For the date format strings, refer to the table below:
|Y||4-digit year eg. 2022|
|y||2-digit year eg. 22|
|F||Name of the Month eg. January|
|M||Short-form name of the month eg. Jan|
|m||The number of the month eg. 01 for January|
|n||A numeric representation of a month, without leading zeros (1 to 12)|
|t||The number of days in the given month|
|l||(Lower case L) Long-form day of the week eg. Monday|
|W||ISO-8601 Week number of the year (Weeks starting on Monday)|
|N||ISO-8601 numeric number of the day of the week (1 for Mon, 7 for Sun)|
|w||Numeric representation for a day of the week (0 for Mon, 6 for Sat)|
|D||Short-form(3-letter) day of the week eg. Mon|
|d||Day of the month (01 - 31)|
|j||Day of the month without leading zeros (1 - 31)|
|z||The day of the year (from 0 through 365)|
|S||The English ordinal suffix for the day of the month eg. st, nd, rd, th. Works best when used in combination with j.|
|H||The hour in 24hr system (00 - 23)|
|h||The hour in 12hr system (01 - 12)|
|G||24-hour format of an hour without leading zeros (0 to 23)|
|g||12-hour format of an hour without leading zeros (1 to 12)|
|i||Minutes with leading zeros (00 to 59)|
|s||Seconds with leading zeros (00 to 59)|
|A||Sets uppercase AM or PM|
|a||Sets lowercase am or pm|
|L||Returns whether it's a leap year (1 if it is a leap year, 0 otherwise)|
<?php $date = "2022-01-13"; echo date("l d M, Y", strtotime($date));
Thursday 13 Jan, 2022
From the example above, we have converted the date "2022-01-13" to a timestamp using the strtotime() function. Then we have passed that timestamp to the date() function which has a specified string format as "l d M, Y". Refer to the table above and play around with the values to come up with your own formats.
The DateTime class
DateTime is an in-built class in PHP that allows you to work with the date and time in an object-oriented way.
You can refer to its documentation here -> PHP: DateTime - Manual.
Now, having understood the concepts above, let's deal with the elephant in the room.
Getting the last day of the month from a given date in PHP
There are many ways of doing it. Here in this article, we will cover two different methods which include:
- Using the date() and strtotime() functions
- Using the DateTime class
Method 1: Using the date() and strtotime() functions
Our approach here involves converting the date into a timestamp using the strtotime() function, then we pass the timestamp to the date() function with a string format that returns the last date of the month.
<?php $date = "2022-01-13"; echo date("Y-m-t", strtotime($date));
As you can see in the output, 31st is the last date in January. "Y" returns a 4-digit year, "m" returns a numeric representation of the month, and "t" gives the number of days in that given month which is equal to the date of the last day in that month.
Now if you want to get the day of the week for the last day of the month, you will need to pass the timestamp of the returned date in the date() function and format it as you wish, this time containing the day.
<?php $date = "2022-02-08"; $lastday = date("Y-m-t", strtotime($date)); echo date("l jS F, Y", strtotime($lastday));
Monday 28th February, 2022
Method 2: Using the DateTime class
This class has many methods but we will only need to use two of them: The modify() and the format() methods.
The modify() method alters the timestamp of a DateTime object by incrementing or decrementing in an accepted date/time string format. It returns the DateTime object for method chaining or false on failure.
The format() method accepts strings formatted in the same way just as described in the date() function above (refer to the table). It returns a date formatted according to the given format.
We need to first create an object of the class and pass the date we are working with to it, then use the class methods to get our desired results.
Specifying the last date in the format() method, including "t" in the format string.
<?php $date = new DateTime("2022-01-13"); echo $date->format("Y-m-t");
To format the date in a way that includes the day of the week, you will need to either use the modify() method.
Using the modify() method to specify the last day of the month. In this case, you can specify the date format including the day of the week.
<?php $date = new DateTime("2022-01-13"); $date->modify('last day of'); echo $date->format("l jS F, Y");
Monday 31st January, 2022