I am pulling a datetime from a mysql db and i would like to add X hours to it then compare it to the current time. So far i got
$dateNow = strtotime(date('Y-m-d H:i:s'));
$dbTime = strtotime($row[0]);
then i tried $dbTime + strtotime("4 hours"); but 4 hours seem to add 4hrs to the current time instead of raw 4hours. How do i add X hours to dbTime?
NOTE: I am using php 5.1.2 so date_add doesnt work (5.3.0)
I tend to use the time() function, and this page from the manual shows them displaying the date a week in the future: http://us3.php.net/manual/en/function.time.php
strtotime
has an optional second argument. Provide a Unix timestamp there and the output will be relative to that date instead of the current date.You can also use the fact that Unix timestamps are seconds-based - if you know what four hours are in seconds, you can just add that to the time integer value.
Here's how I'd do it:
Pull the time from the database using the
UNIX_TIMESTAMP()
function.The UNIX timestamp is in seconds, so add
4*60*60
to it.Convert the modified UNIX timestamp to a date using PHP's localtime() or strftime() function.
You have quite a few options here:
1.
2.
3.
4.
5.
Assuming that the timestamp returned by the DB is in SQL format, the following should work fine:
time() and strtotime() result in unix timestamps in seconds, so you can do something like the following, provided your db and do your comparison: