Get the Previous Dates in PHP
Dates and timestamps are crucial in many software systems and web applications. They allow us to track events, analyze patterns, and provide users with pertinent information based on time. In this post we’ll explore how to handle dates in PHP, specifically on how to get the date for the last day, or last month or even last year.
Get the Previous Month in PHP date
Imagine you’re working on a PHP project and you need to fetch last month’s date. The PHP date()
and strtotime()
functions are here to help. However, these functions can lead to misunderstandings if not used correctly.
At first, you might be tempted to use this code:
$prevmonth = date('M Y');
However, this will give you the current month/year, not the last month. To modify this, you can use the PHP
strtotime()
function, which parses English textual datetime descriptions into Unix timestamps. This leads us to this solution:
echo date("Y-n-j", strtotime("first day of previous month"));
echo date("Y-n-j", strtotime("last day of previous month"));
When running this on November 3, you’ll get 2014-10-1 and 2014-10-31 - the first and last day of the previous month. This method is simple and highly recommended.
A word of caution: Using strtotime('-1 month', $time)
can cause issues with the output. This is because -1 month
is the same as -30 days
, which can lead to errors if the current month has 31 days or February is the last month.
So, be cautious while using this method.
One “incorrect” method that is often used is:
$lastMonth = date('M Y', strtotime("-1 month"));
This method is prone to errors. If the current month has 30 or more days but the previous month has less than 30
days, this method will return the same date as the current month. You can test the behaviour with the following
example that will print Fri, 03 Mar 2023 00:00:00 +0000
and not Tue, 28 Feb 2023 00:00:00 +0000
:
$the_date = strtotime('31.03.2023');
echo date('r', strtotime('-1 month', $the_date));
The correct way to fetch last month’s date in this case would be:
echo date("Y-m", strtotime("first day of previous month"));
The above method will accurately return the first day of the previous month. For example, considering you are in
2023-03-30
, than the result would be 2023-02
. Note that use only previous month
would result in a
behaviour similar to -1 month
.
Another elegant and reliable method of fetching the previous month’s date is to use PHP’s DateTime()
and
DateInterval()
functions. Here is an example function:
$now = new DateTime();
$lastMonth = $now->sub(new DateInterval('P1M'));
echo $lastMonth->format('Y-m');
This function uses DateTime
to get the current date and time and DateInterval
to subtract one month from the current date.
Get the Previous Day and Year in PHP date
To get the previous day in PHP date has a more direct use, You can simply pass -1 day as a parameter without risking unexpected behavior:
Get previous day in PHP using strtotime
The strtotime
function is used to convert a date/time description to a Unix timestamp.
$yesterday = date("Y-m-d", strtotime("-1 day"));
echo $yesterday;
Get previous day in PHP using DateTime and modify method
The DateTime
class represents date and time and the modify
method alters the timestamp.
$date = new DateTime();
$date->modify('-1 day');
$yesterday = $date->format('Y-m-d');
echo $yesterday;
Get previous day in PHP using DateTime and sub method with DateInterval
The DateInterval
class represents a time interval and can be used with the sub
method of DateTime
to subtract a day.
$date = new DateTime();
$date->sub(new DateInterval('P1D'));
$yesterday = $date->format('Y-m-d');
echo $yesterday;
Get the Previous Year in PHP
To get the previous year with dates in PHP is quite similar to getting the previous day. All you need to do is
replace the day
parameter with year
.
$lastYear = date("Y", strtotime("-1 year"));
echo $lastYear;
$date = new DateTime();
$date->modify('-1 year');
$lastYear = $date->format('Y');
echo $lastYear;
$date = new DateTime();
$date->sub(new DateInterval('P1Y'));
$lastYear = $date->format('Y');
echo $lastYear;
Final considerations
Remember, dates can be tricky to work with due to variations in days per month, leap years, and different time zones. Therefore, it’s always a good idea to verify your output by running your code with different scenarios.
Now that you have the correct information and the knowledge to implement it, it’s time to put this into action!
Comments