YogeshChauhan.com
How to get previous days or next days in PHP?
March 8, 2020


Using strtotime function, we can achieve this. Let’s take a look at the function first.

strtotime

Parses an English textual datetime description into a Unix timestamp.

Syntax


strtotime ( string $time [, int $now = time() ] ) : int

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now(), or the current time if now() is not supplied.

Each parameter of this function uses the default time zone unless a time zone is specified in that parameter. Be careful not to use different time zones in each parameter unless that is intended. 

Parameters

time: A date/time string.

now: The timestamp which is used as a base for the calculation of relative dates.

Let's come to the main question:

Use this to get previous date:


date('Y/m/d',strtotime("-1 days"));

Just increase or decrease the days to get the desired date.

To get next date, use this:


date('Y/m/d',strtotime("+1 days"));

OR even simple:


echo date('Y-m-d',strtotime("yesterday"));
echo date('Y-m-d',strtotime("tomorrow"));
dreamhost

Leave a Reply

Most Read

#1 How to set opacity or transparency using CSS? #2 Pagination in CSS with multiple examples #3 How to check if radio button is checked or not using JavaScript? #4 How to make HTML form interactive and using CSS? #5 How to uninstall Cocoapods from the Mac OS? #6 How to add Read More Read Less Button using JavaScript?



Recently Posted

Feb 24 WordPress get_posts Examples Feb 24 How to modify the latest post array using get_posts() in WordPress? Feb 22 WordPress: How to get ACF field values from another post? Feb 22 WordPress: How to print ACF repeater field values? Feb 22 WordPress: How to print ACF array field values? Feb 21 WordPress: How to get field values in Advanced Custom Fields?



You might also like these

Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software”MiscHow Do You Make a Private VPN?MiscSolution to pod install fails with json error on Mac OS X 10.15 (or Mac OS Catalina)MiscHow to show slider value in HTML5 range input using JavaScript?HTMLThe difference between isFinite() method and isFinite() function in JavaScriptJavaScriptHow to apply style only to first child and/or only to children other than the first child?CSS