Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add day to current date

Tags:

date

php

add a day to date, so I can store tomorrow's date in a variable.

$tomorrow = date("Y-m-d")+86400;

I forgot.

like image 332
Brad Avatar asked Oct 12 '10 20:10

Brad


3 Answers

I'd encourage you to explore the PHP 5.3 DateTime class. It makes dates and times far easier to work with:

$tomorrow = new DateTime('tomorrow');

// e.g. echo 2010-10-13
echo $tomorrow->format('d-m-Y');

Furthermore, you can use the + 1 day syntax with any date:

$xmasDay = new DateTime('2010-12-24 + 1 day');
echo $xmasDay->format('Y-m-d'); // 2010-12-25
like image 191
lonesomeday Avatar answered Oct 23 '22 18:10

lonesomeday


date returns a string, whereas you want to be adding 86400 seconds to the timestamp. I think you're looking for this:

$tomorrow = date("Y-m-d", time() + 86400);
like image 34
casablanca Avatar answered Oct 23 '22 18:10

casablanca


date() returns a string, so adding an integer to it is no good.

First build your tomorrow timestamp, using strtotime to be not only clean but more accurate (see Pekka's comment):

$tomorrow_timestamp = strtotime("+ 1 day");

Then, use it as the second argument for your date call:

$tomorrow_date = date("Y-m-d", $tomorrow_timestamp);

Or, if you're in a super-compact mood, that can all be pushed down into

$tomorrow = date("Y-m-d", strtotime("+ 1 day"));
like image 25
Matchu Avatar answered Oct 23 '22 18:10

Matchu