如何使用时间戳在php中找到日期?

时间:2011-08-24 12:54:12

标签: php

我有一个时间戳,比如$ now,我需要找到时间戳,比如说,现在是$ now之后的45天。我该怎么办?

6 个答案:

答案 0 :(得分:1)

您可以使用strtotime

$the_45_days_later = strtotime('+45 day', $now);

时间戳的精度未四舍五入为00:00:00
但恰好遵循$now所属的时间

其他答案显示使用纯秒计算,
这也是精确的

$the_45_days_later = $now+(86400*45);

答案 1 :(得分:0)

                                 //sec min  hour  day
$fourty_five_days_after = time() + 60 * 60 * 24 * 45;

答案 2 :(得分:0)

1天有86400秒,所以代码为:

$now = time();
$after = $now + 45*86400;

答案 3 :(得分:0)

这应该是你需要的:

$new_ts = $now+45*24*60*60;

答案 4 :(得分:0)

我更喜欢使用(PHP 5> = 5.2.0)DateTime函数:

$iTimeStamp = time();
$oDateTime = DateTime::createFromFormat("U", $iTimeStamp);
$oDateTime->add(new DateInterval('P45D'));
echo $oDateTime->getTimeStamp();

答案 5 :(得分:0)

来自http://www.php.net/manual/en/datetime.add.php

$a = new DateTime("now");
var_dump($a);
date_add($a, date_interval_create_from_date_string("45 days"));
var_dump($a);

object(DateTime)#1 (3) {
  ["date"]=>
  string(19) "2011-08-24 09:00:03"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "America/New_York"
}
object(DateTime)#1 (3) {
  ["date"]=>
  string(19) "2011-10-08 09:00:03"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "America/New_York"
}