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']))
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'])))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With