add days and hours in date/time field and get the updated date/time in php using mysql

ぃ、小莉子 提交于 2019-12-11 04:58:49

问题


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.


回答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'



回答2:


to add an interval (a day or several hours) to datetime, you can use the date_add function.




回答3:


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);


来源:https://stackoverflow.com/questions/2242601/add-days-and-hours-in-date-time-field-and-get-the-updated-date-time-in-php-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!