Jeff Thomas
Jeff Thomas

Reputation: 4816

Subtract 6 Hours From timestamp Using PHP

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

Answers (3)

Vincy Oommen
Vincy Oommen

Reputation: 75

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

Upvotes: 0

icktoofay
icktoofay

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

Marc B
Marc B

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

Related Questions