Find PHP date Yesterday with Examples
During development, we met with some scenarios where we require to display the previous date or require to find the n days before the date from now.
This date manipulation requires the use of some PHP inbuilt to get the previous dates in a specific date format. We are covering all possible ways to retrieve minus 1 day in detail below. Stay tuned!
In this article, we are going to learn multiple ways to get a date minus 1 day with demonstration example code. There are very useful PHP inbuilt date/time functions out there to manipulate date queries. Let’s understand some of the most widely used PHP date functions.
Inbuilt PHP function date is used to get a formatted date string for a given timestamp.
Syntax: date(format, timestamp)
format: Required param. Date format such as "Y-m-d", "Y-m-d H:i:s" etc
Timestamp: Optional param. Default is current time if not provided.
Formatted Date string If none or integer timestamp provided
False. if the noninteger timestamp is provided. Also, it will raise PHP warnings.
var_dump(date("Y-m-d H:i:s", "TimeStamp"));
Definition:strtotime() is a PHP inbuilt function used to parse English textual date-time to a UNIX timestamp.
Syntax: strtotime(datetime, baseTimestamp)
- Datetime: Required String param. It should be an English textual date-time. Example: "yesterday", "now", ":next Monday" etc.
- baseTimestamp: Optional parameter. Its base time is used to calculate the return value of strtotime. Default is based on the Current timestamp if not mentioned.
- Return: TimeStamp when successful. False on failed to return timestamp.
There are more other vital date/time functions that can be checked here. Now let’s explore the most commonly asked queries for finding yesterday’s date in PHP.
Table of contents
- Get yesterday's date using strtotime function in PHP
- Find the Previous Date for any Given date using strtotime function
- Get Next Day date in PHP
- To brush up on your knowledge on this subject, take the MCQ at the End of this Article.
- Feedback: Your input is valuable to us. Please provide feedback on this article.
1. Get yesterday's date using strtotime function in PHP
We can create yesterday's date format using PHP inbuilt function date & strtotime in the following manner.
Note: Current Server Date: 23 November 2021
Example 1: With English text
echo date('j F, Y',strtotime("-1 days"));
22 November, 2021
Explanation: Here function strtotime() is used to parse an English text
-1 days and return the corresponding Unix timestamp.
The output of
strtotime("-1 days") is passed to date function as the second argument to evaluate string date based on specified date format (j F, Y).
Note. There are multiple ways to define date format depending on requirements.
What if we don’t pass
strtotime("-1 days")) to the date function?
Well, As mentioned above default is the current timestamp if not mentioned. Therefore the code
date('Y-m-d'); will return the current date.
Example 2: With English text yesterday
Explanation: It will return yesterday's date.
strtotime parse text yesterday and return a respected timestamp which further passed to date to evaluate date with the specified format.
2. Find the Previous Date for any Given date using strtotime function
In such cases, we can use the same date & strtotime function to get the previous date for any given date.
Let’s understand with the following program:
$givenDate = "2021-09-01"; $timestampForGivenDate = strtotime ( $givenDate ); $englishText = '-1 day'; $requireDateFormat = "Y-m-d"; echo date($requireDateFormat,strtotime ( $englishText , $timestampForGivenDate )) ;
Let’s understand in following steps
- $timestampForGivenDate = strtotime ( $givenDate ); used to get a unix timestamp for a given date $givenDate.
- Next required English date text for our requirement to get the previous date. Therefore $englishText = '-1 day' serves this purpose.
- Let's suppose we need a date string in “Y-m-d” format. Therefore variable $requireDateFormat = "Y-m-d" used for this purpose.
- strtotime ( $englishText , $timestampForGivenDate ) returns a timestamp and passed to date function to find the previous date of a given date.
3. Get Next Day date in PHP
We have published a detailed article to
- Get the next date for given date
- Find tomorrow date
- Find next Nth day date
With examples. Click Here to learn more.