为时间戳添加时间


add time for timestamp

向时间戳添加时间的最佳方法是什么?我在数据库中有

2011-10-09 10:29:23

,我想加上20天。我该怎么做呢?我需要一个例子:

    <
  1. 20小时/gh>20天

你可以像这样使用Date::add:

<?php
$date = new DateTime('2011-10-09 10:29:23');
$date->add(new DateInterval('P20D')); //use PT20H for 20 hours
echo $date->format('Y-m-d H:i:s');
?>

http://www.php.net/manual/en/function.strtotime.php

使用PHP的strtotime函数:

$newtimestamp = strtotime("+20 days", $yourtimestamp);

另一个与Frame配合良好的方法是

$time = date('Y-m-d H:i:s', time($foo->time_stamp + (24*60*60))); 

,其中末尾的数字是旧的php方法(*days*hours*mins*secs)

我建议您使用unix_timestamps,使用这些可以更快地工作,如果要将时间显示给用户,则可以使用gmdate()。