Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, Get tomorrows date from date

I have a PHP date in the form of 2013-01-22 and I want to get tomorrows date in the same format, so for example 2013-01-23.

How is this possible with PHP?

like image 726
Justin Avatar asked Jan 22 '13 14:01

Justin


People also ask

How can I get the day of a specific date with PHP?

You can use the date function. I'm using strtotime to get the timestamp to that day ; there are other solutions, like mktime , for instance.

What is Strtotime PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

How can I get yesterday Date in PHP?

To get yesterday's date in PHP simply use the DateTime class and instantiate it with the constructor parameter 'yesterday' Then you can format the DateTime object with the format method.

How can I get the difference between two dates in PHP?

PHP date_diff() Function $date2=date_create("2013-12-12"); $diff=date_diff($date1,$date2);


1 Answers

Use DateTime

$datetime = new DateTime('tomorrow'); echo $datetime->format('Y-m-d H:i:s'); 

Or:

$datetime = new DateTime('2013-01-22'); $datetime->modify('+1 day'); echo $datetime->format('Y-m-d H:i:s'); 

Or:

$datetime = new DateTime('2013-01-22'); $datetime->add(new DateInterval("P1D")); echo $datetime->format('Y-m-d H:i:s'); 

Or in PHP 5.4+:

echo (new DateTime('2013-01-22'))->add(new DateInterval("P1D"))                                  ->format('Y-m-d H:i:s'); 
like image 64
John Conde Avatar answered Sep 21 '22 14:09

John Conde