I would like to provide the ability for users to submit a datetime
value, along with values representing additional days/hours, and then store the datetime
that results from adding the two in a mysql database.
Asked
Active
Viewed 3,560 times
3 Answers
1
If you want to do it in PHP, this should help:
$datetime = '2010-02-11 12:00:00';
$days = '5';
$hours = '3';
$new_datetime = date('Y-m-d H:i:s', strtotime("+$days days $hours hours", strtotime($datetime)));
echo $new_datetime; # Will output '2010-02-16 15:00:00'

Tatu Ulmanen
- 123,288
- 34
- 187
- 185
0
If you want to do this in PHP, you can use mktime, adding the additional time / date values as you require:
$time = mktime($hours + $extra_hours, $minutes, $seconds, $months + $extra_months, $days, $years);
$date = date('Y-m-d H:i:s', $time);

Sampson
- 265,109
- 74
- 539
- 565
-
What if $hours + $extra_hours adds up to more than 24 - does mktime deal with that correctly? – Vince Bowdren Feb 11 '10 at 22:17
-
Yes, it does. It will also handle rollover for the other parameters. – Dan Ruscoe Feb 12 '10 at 06:15