使用PHP从时间戳中减去6小时

时间:2021-12-10 13:43:02

I have this code to display a date and time from the database. How would I modify it to subtract 6 hours from the timestamp.

我有这个代码来显示数据库中的日期和时间。我怎么修改它,从时间戳中减去6小时。

date('m-d g:Ga', strtotime($row['time_stamp']))

3 个解决方案

#1


14  

UNIX time stamps are seconds since the Epoch. Simply subtract the number of seconds in six hours:

UNIX时间戳是自纪元以来的秒数。只需在六小时内减去秒数:

date('m-d g:Ga', strtotime($row['time_stamp'])-21600)

You could alternatively use strtotime again:

您也可以再次使用strtotime:

date('m-d g:Ga', strtotime('-6 hours', strtotime($row['time_stamp'])))

#2


4  

Since it's in MySQL, you can have MySQL do the calculation for you:

因为它在MySQL中,你可以让MySQL为你做计算:

SELECT DATE_FORMAT('...', DATE_SUB(time_stamp, INTERVAL 6 HOUR)) ...

This would save you the overhead of MySQL having to conver its internal representation into a full string, and the overhead of PHP's strtotime() parseing that string just to turn it back into yet another string. While strtotime is magical sometimes, it's definitely not efficient.

这将节省MySQL必须将其内部表示转换为完整字符串的开销,以及PHP的strtotime()解析该字符串以将其转换为另一个字符串的开销。虽然strtotime有时很神奇,但绝对不是有效的。

#3


0  

$vindate=date('H:i:s', strtotime($row['ms_attend_time'])+19800); echo $vindate;

$ vindate =日期(H:我:年代”,strtotime($ row[' ms_attend_time '])+ 19800);echo $ vindate;

#1


14  

UNIX time stamps are seconds since the Epoch. Simply subtract the number of seconds in six hours:

UNIX时间戳是自纪元以来的秒数。只需在六小时内减去秒数:

date('m-d g:Ga', strtotime($row['time_stamp'])-21600)

You could alternatively use strtotime again:

您也可以再次使用strtotime:

date('m-d g:Ga', strtotime('-6 hours', strtotime($row['time_stamp'])))

#2


4  

Since it's in MySQL, you can have MySQL do the calculation for you:

因为它在MySQL中,你可以让MySQL为你做计算:

SELECT DATE_FORMAT('...', DATE_SUB(time_stamp, INTERVAL 6 HOUR)) ...

This would save you the overhead of MySQL having to conver its internal representation into a full string, and the overhead of PHP's strtotime() parseing that string just to turn it back into yet another string. While strtotime is magical sometimes, it's definitely not efficient.

这将节省MySQL必须将其内部表示转换为完整字符串的开销,以及PHP的strtotime()解析该字符串以将其转换为另一个字符串的开销。虽然strtotime有时很神奇,但绝对不是有效的。

#3


0  

$vindate=date('H:i:s', strtotime($row['ms_attend_time'])+19800); echo $vindate;

$ vindate =日期(H:我:年代”,strtotime($ row[' ms_attend_time '])+ 19800);echo $ vindate;