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 a 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.
Date function
Inbuilt PHP function date is used to get a formatted date string for a given timestamp.
Syntax: date(format, timestamp)
Input Param
format: Required param. Date format such as "Y-m-d", "Y-m-d H:i:s" etc
Timestamp: Optional param. The default is current time if not provided.
Return:
Formatted Date string If none or integer timestamp provided
False. if the noninteger timestamp is provided. Also, it will raise PHP warnings.
Example :
var_dump(date("Y-m-d H:i:s", "TimeStamp"));
Result: False
strtotime function
Definition:strtotime() is a PHP inbuilt function used to parse English textual date-time to a UNIX timestamp.
Syntax: strtotime(datetime, baseTimestamp)
Input Params:
- 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 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.
Scroll for More Useful Information and Relevant FAQs
- 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.
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 -1 days
echo date('j F, Y',strtotime("-1 days"));
Result:
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
echo date('Y-m-d',strtotime("yesterday"));
Explanation: It will return yesterday's date. strtotime
parse text yesterday and return a respected timestamp which further passed to date to evaluate the date with the specified format.
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 )) ;
Result: 2021-08-31
Explanation:
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" is used for this purpose.
- strtotime ( $englishText , $timestampForGivenDate ) returns a timestamp and passed to date function to find the previous date of a given date.
Get Next Day date in PHP
Brain Exercise
Was this post helpful?
- ?
Which code might NOT return yesterday's date?
Options are: - ?
Which date code will yield a textual month in abbreviated form? ( three letters)?
Try your hand at more Multiple-choice ExercisesOptions are: