如何在php中添加24小时的unix时间戳?(How do I add 24 hours to a unix timestamp in php?)
现在我想添加24小时的时间戳。 如何找到24小时的unix时间戳数,以便现在可以将其添加到时间戳?
我也想知道如何在当前时间戳中添加48小时或多天。
我该怎么做最好呢?
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?
I also would like to know how to add 48 hours or multiple days to the current timestamp.
How can I go best about doing this?
原文:https://stackoverflow.com/questions/2515047
更新时间:2019-07-24 23:03
最满意答案
你可能想添加一天而不是24小时。 夏季时间不是全天都有24小时:
strtotime('+1 day', $timestamp);
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:
strtotime('+1 day', $timestamp);
2018-01-25
相关问答
Sam的解决方案很好,但是因为你使用yyyyMMddHHmmss我会做类似的事情: bool addDay = false;
DateTime result;
string dtToParse = "20120911240000";
if (dtToParse[8] == '2' && dtToParse[9] == '4')
{
dtToParse = dtToParse.Substring(0, 8) + "00" + dtToParse.Substring(10);
add
...
实际问题似乎围绕着使用DST - 因为有关日期是在DST结束时,时间计算变得大约一个小时。 这应该是什么原因应该是美国东部标准时间3点到美国东部标准时间1点我不确定(也许我以前用过快速转换的网站是关闭的),但它也解释了为什么我的数学都出错了无论我有多少次检查了它。 The actual issue appears to have revolved around the use of DST -- because the date in question was at the end of DST,
...
你可能想添加一天而不是24小时。 夏季时间不是全天都有24小时: strtotime('+1 day', $timestamp);
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: strtotime('+1 day', $timestamp);
我猜你可以使用DateTime , DateInterval和DatePeriod : $date1 = new DateTime();
$date2 = DateTime::createFromFormat('U', $post_timestamp); # I assume a unix timestamp here
//determine what interval should be used - 1 minute
$interval = new \DateInterval('PT1M');
...
Unix时间戳以秒为单位。 这适用于MySQL: SELECT * FROM NODE WHERE Date < (UNIX_TIMESTAMP(NOW()) - 24*60*60)
Unix timestamp is in seconds. This works with MySQL: SELECT * FROM NODE WHERE Date < (UNIX_TIMESTAMP(NOW()) - 24*60*60)
SELECT
timestampdiff(HOUR, yourtimestampcolumn, now() ) as hours_since,
*
FROM
Your_table
WHERE
timestampdiff(HOUR, yourtimestampcolumn, now() ) < 24
请参阅https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_time
...
你有秒,所以只需要做点什么 SELECT secondsField / 3600 as 'hours' FROM tableName
You have the seconds, so just do something like SELECT secondsField / 3600 as 'hours' FROM tableName
+00表示它是带时区的时间戳,您的客户端时区是UTC。 如果您不想在屏幕上显示这些+00 ,请将其投射到timestamp without timezone ,例如: t=# select now();
now
-------------------------------
2017-05-23 09:04:46.105322+00
(1 row)
Time: 0.690 ms
t=# select now()::timestamp;
now
...
要获得最近24小时,请将1天前的时间转换为unix时间戳: UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))
要将时间戳转换回一小时,请先将其转换为时间,然后使用HOUR函数: HOUR(FROM_UNIXTIME(time))
结合这些,你可以做到: SELECT HOUR(FROM_UNIXTIME(time)) AS hour, COUNT(*)
FROM service_data
WHERE time >= UNIX_TIMESTAMP(D
...
在计算差异之前,计算两个时间戳的完整天数: floor(1284985360 / 86400) - floor(1233925200 / 86400)
您的结果始终是整数。 由于您使用strtotime来获取这些时间戳,请指定时间00:00:00+0000以始终获得86400的倍数: strtotime($str.' 00:00:00+0000')
Calculate the number of full days for both timestamps before calculating
...