Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to increment a date in PHP?

Say I have a string coming in, "2007-02-28", what's the simplest code I could write to turn that into "2007-03-01"? Right now I'm just using strtotime(), then adding 24*60*60, then using date(), but just wondering if there is a cleaner, simpler, or more clever way of doing it.

like image 474
davr Avatar asked Mar 18 '09 23:03

davr


People also ask

How can increase day in date in PHP?

You can use strtotime. $your_date = strtotime("1 day", strtotime("2016-08-24")); $new_date = date("Y-m-d", $your_date);

How can I get 30 Day date in PHP?

php $next_due_date = date('05/06/2016', strtotime("+30 days")); echo $next_due_date; ?> But it is returning to "05/06/2016" only!

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.


1 Answers

A clean way is to use strtotime()

$date = strtotime("+1 day", strtotime("2007-02-28")); echo date("Y-m-d", $date); 

Will give you the 2007-03-01

like image 105
Ólafur Waage Avatar answered Sep 21 '22 17:09

Ólafur Waage