如何在 php 中为 unix 时间戳添加 24 小时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2515047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add 24 hours to a unix timestamp in php?
提问by zeckdude
I would like to add 24 hours to the timestamp for now. How do I find the unix timestamp number for 24 hours so I can add it to the timestamp for right now?
我现在想在时间戳中添加 24 小时。如何找到 24 小时的 Unix 时间戳编号,以便我现在可以将其添加到时间戳?
I also would like to know how to add 48 hours or multiple days to the current timestamp.
我还想知道如何将 48 小时或多天添加到当前时间戳。
How can I go best about doing this?
我怎样才能最好地做到这一点?
回答by álvaro González
You probably want to add one day rather than 24 hours. Not all days have 24 hours due to (among other circumstances) daylight saving time:
您可能想要添加一天而不是 24 小时。由于(在其他情况下)夏令时,并非所有日子都有 24 小时:
strtotime('+1 day', $timestamp);
回答by Yacoby
A Unix timestamp is simply the number of seconds since January the first 1970, so to add 24 hours to a Unix timestamp we just add the number of seconds in 24 hours. (24 * 60 *60)
Unix 时间戳只是自 1970 年 1 月以来的秒数,因此要向 Unix 时间戳添加 24 小时,我们只需添加 24 小时内的秒数。(24*60*60)
time() + 24*60*60;
回答by Soufiane Hassou
Add 24*3600which is the number of seconds in 24Hours
添加24*360024 小时内的秒数
回答by reko_t
Unix timestamp is in seconds, so simply add the corresponding number of seconds to the timestamp:
Unix 时间戳以秒为单位,因此只需将相应的秒数添加到时间戳中:
$timeInFuture = time() + (60 * 60 * 24);
回答by SeanJA
You could use the DateTimeclass as well:
您也可以使用DateTime类:
$timestamp = mktime(15, 30, 00, 3, 28, 2015);
$d = new DateTime();
$d->setTimestamp($timestamp);
Add a Period of 1Day:
添加P的eriod 1个dAY:
$d->add(new DateInterval('P1D'));
echo $d->format('c');
See DateIntervalfor more details.
有关更多详细信息,请参阅DateInterval。
回答by Haritsinh Gohil
As you have said if you want to add 24 hours to the timestamp for right now then simply you can do:
正如您所说,如果您现在想为时间戳添加 24 小时,那么您只需执行以下操作:
<?php echo strtotime('+1 day'); ?>
Above code will add 1 day or 24 hours to your current timestamp.
上面的代码将为您当前的时间戳添加 1 天或 24 小时。
in place of +1 dayyou can take whatever you want, As php manualsays strtotimecan Parse about any English textual datetime description into a Unix timestamp.
代替+1 day你可以随心所欲,正如php 手册所说,strtotime可以将任何英文文本日期时间描述解析为 Unix 时间戳。
examples from the manual are as below:
手册中的示例如下:
<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>
回答by SARADA PRASAD BISWAL
$time = date("H:i", strtotime($today . " +5 hours +30 minutes"));
//+5 hours +30 minutes Time Zone +5:30 (Asia/Kolkata)

