问题描述
|
我有这段代码来显示数据库中的日期和时间。我如何修改它以从时间戳中减去6个小时。
date(\'m-d g:Ga\',strtotime($row[\'time_stamp\']))
解决方法
UNIX时间戳是自大纪元以来的秒数。只需减去六个小时内的秒数即可:
date(\'m-d g:Ga\',strtotime($row[\'time_stamp\'])-21600)
您也可以再次使用strtotime
:
date(\'m-d g:Ga\',strtotime(\'-6 hours\',strtotime($row[\'time_stamp\'])))
, 由于它在MySQL中,因此您可以让MySQL为您进行计算:
SELECT DATE_FORMAT(\'...\',DATE_SUB(time_stamp,INTERVAL 6 HOUR)) ...
这样可以节省MySQL必须将其内部表示转换为完整字符串的开销,以及PHP \的strtotime()解析该字符串以将其转换为另一个字符串的开销。尽管strtotime有时是神奇的,但绝对没有效率。
, $ vindate = date(\'H:i:s \',strtotime($ row [\'ms_attend_time \'])+ 19800);
回声$ vindate;