Adding a day to a date

Please help!

How do I add 1 day - just 1 day to a date variable?

Is it too much to ask? I've searched all over, I cant seem to find a straight answer.

zicheAsked:
Who is Participating?
 
ldbkuttyCommented:
If the date is in Y-m-d or m/d/Y format, you can use this:

<?php
 $my_date = '2005-05-11';  // or: $my_date = '05/11/2005';
 $new_date = date("Y-m-d", strtotime($my_date . ' +1 day'));
 echo $new_date;
?>

For other formats, split the date and use mktime function: http://www.php.net/mktime
0
 
ldbkuttyCommented:
Example:

<?php
 $my_date = '11-05-2005';
 $date_parts = split('-', $my_date);
 $new_date = date("d-m-Y", mktime(0, 0, 0, $date_parts[1], $date_parts[0]+1, $date_parts[2]));
 echo $new_date;
?>
0
 
jdpipeCommented:
Or, using lowerlevel functions,

// add 24 hours x 3600 seconds per hour
$tomorrow = time() + 24 * 3600;

// print it out
print(date('r',$tomorrow));
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
jdpipeCommented:
NB if you're using this with dates from a database etc, then always use UNIX_TIMESTAMP when getting your dates etc from the database.

SELECT UNIX_TIMESTAMP(dateofsomething) FROM sometable;
...
$nextday = $res['dateofsomething'] + 24 * 3600;
...

JP
0
 
merwetta1Commented:
ldbkutty's solution with strtotime() will handle daylight savings time changes better. Adding 24x3600 seconds will lose or gain an hour if it happens to cross a time change.
0
 
jdpipeCommented:
Yes, you're right there.  I was thinking that this wasn't going to be a problem because ziche was only looking for a date variable. But it is a problem, you're right, merwetta.

Worth pointing out that if you are using a MySQL database you could therefore also try

DATE_ADD(datefield,INTERVAL 1 DAY)

JP
0
 
zicheAuthor Commented:
Thanks everybody
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.