Reputation: 4816
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.
date('m-d g:Ga', strtotime($row['time_stamp']))
Upvotes: 5
Views: 14508
Reputation: 75
$vindate=date('H:i:s', strtotime($row['ms_attend_time'])+19800); echo $vindate;
Upvotes: 0
Reputation: 129109
UNIX time stamps are seconds since the Epoch. Simply subtract the number of seconds in six hours:
date('m-d g:Ga', strtotime($row['time_stamp'])-21600)
You could alternatively use strtotime
again:
date('m-d g:Ga', strtotime('-6 hours', strtotime($row['time_stamp'])))
Upvotes: 15
Reputation: 360782
Since it's in MySQL, you can have MySQL do the calculation for you:
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.
Upvotes: 4