I am trying to assign tomorrow date to a variable in my program call $tomorrow, but i just cant seem to work out how to do it. Have done today's one by doing

$date = date('Y-m-d');

after reseraching I thought it will make sense to do this

$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
but this produced a long number (1141689600). making no sense.

I want the output to be as the same format as the date function used above.

Please please please please can someone help me. Thanks in advance

Dani AI

Generated

You saw a long number (1141689600) because that’s a Unix timestamp (seconds since 1970). The fix is to convert that timestamp into a formatted date instead of printing it raw. Several answers here used timestamps directly; below are clearer, more robust alternatives that avoid common pitfalls.

Simple, readable approach using a relative string with strtotime:

$tomorrow = date('Y-m-d', strtotime('+1 day'));

A more robust, timezone-aware method uses DateTime and DateInterval (preferred for production):

$dt = new DateTime('now', new DateTimeZone('America/New_York'));
$dt->add(new DateInterval('P1D'));
$tomorrow = $dt->format('Y-m-d');

You can also construct from the relative keyword in DateTime:

$tomorrow = (new DateTime('tomorrow', new DateTimeZone('UTC')))->format('Y-m-d');

Notes and caveats: , and were on the right track by generating a timestamp and formatting it, and ’s shell approach is fine for scripts run on Unix. Avoid simply adding 86400 seconds when incrementing a day — daylight saving transitions can make a day 23 or 25 hours. Also be aware of 32-bit timestamp limits on old systems; DateTime avoids that. Explicitly set the timezone (date_default_timezone_set or DateTimeZone) to prevent surprising results.

Recommended Answers

All 5 Replies

$tomorrow = date('Y-m-d',mktime(0,0,0,date('m'), date('d')+1, date('Y')));

Troy's code will work just fine, here's another way of doing it:

$tomorrow = date('Y-m-d',mktime()+86400);

;)

Troy's code will work just fine, here's another way of doing it:

$tomorrow = date('Y-m-d',mktime()+86400);

Hi,

I think is better to use time() instead of mktime() in this case ;)

And a little explanation:

date() function get's two arguments. first one is date format, second one is function time() which get the current time in UNIX format.
We can manipulate time() function with - or + time.
so 86400 is the sum for the seconds in 24hours.
24*60*60 (24 hours * 60 mins * 60 sec).
Well date function will convert UNIX time time stamp to your date format. That's is...

excuse my english it's not my native language and i hope this will help you to understand how the things work out

Thanks alot guys. Those codes works.

hi,


try this

TOMORROW=`expr \`date +%s\` + 86400`
date -r $TOMORROW

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.