YogeshChauhan . com

How to get previous days or next days in PHP?

in PHP on 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"));
amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 7 How to disable right click and drag and drop of images using jQuery? May 7 How to render Lists in React? May 7 What’s the difference between variables in CSS and SCSS (Sass)? May 7 How to define variables in SCSS (Sass)? May 7 How to show and hide an element on click in React? May 5 Use inline if to make a shorter conditional syntax in React

You might also like these

How to set the permissions on all files and folders in Mac?MiscHow to scroll contents of HTML body horizontally and vertically using JavaScript?JavaScriptHow to create a Random Hex Color generator using JavaScript?JavaScriptaddcslashes() and addslashes() String Functions in PHPPHPHow to convert an object from API to JSON array in Angular 9?AngularUnderstand Inheritance in PHP in this Basic Example (For Beginners)PHP