Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Adding a day to a date

Posted on 2005-05-10
7
Medium Priority
?
231 Views
Last Modified: 2009-07-29
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.

0
Comment
Question by:ziche
7 Comments
 
LVL 32

Accepted Solution

by:
ldbkutty earned 200 total points
ID: 13968590
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13968633
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
 
LVL 7

Expert Comment

by:jdpipe
ID: 13969143
Or, using lowerlevel functions,

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

// print it out
print(date('r',$tomorrow));
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:jdpipe
ID: 13969180
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
 
LVL 6

Expert Comment

by:merwetta1
ID: 13974128
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
 
LVL 7

Expert Comment

by:jdpipe
ID: 13974169
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
 

Author Comment

by:ziche
ID: 13975437
Thanks everybody
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question