Page 1 of 1

Dates

Posted: Wed Jun 22, 2005 3:13 pm
by blazer21
How could I take a unix timestamp in php and add 4hours to it?

Posted: Wed Jun 22, 2005 3:17 pm
by pickle
Look into strtotime().

Code: Select all

$modified = strototime('+4 hours',$original);
This is better than adding 4 * 60 * 60 seconds, as this takes care of daylight saving time as well (and is just generally cleaner)

Thanks

Posted: Wed Jun 22, 2005 3:20 pm
by blazer21
Thanks for the help...

Posted: Wed Jun 22, 2005 4:38 pm
by John Cartwright
blazer21 wrote:PHP v5.0.2

When running the following code date always returns 04:00:00 for the time and not 4 hours past the current time?

Code: Select all

echo $date('m/d/Y h:i:s', strtotime('+4 hours', time()));
In reply to his cross post...

Aside that you should change $date to date it worked fine for me.
Consider trying this

Code: Select all

echo date('m/d/Y h:i:s', strtotime('+4 hours'));
an alternative

Code: Select all

echo date('m/d/Y h:i:s', (time()+14400));

Posted: Wed Jun 22, 2005 4:46 pm
by blazer21
Funny I can't seem to get strtotime('+4 hours') to work correctly even using your posted code. So, I'm going to use the following.

Code: Select all

echo date('h:i:s', (time()+14400));

Posted: Wed Jun 22, 2005 4:52 pm
by John Cartwright
Try this custom function found at http://ca3.php.net/strtotime who have reported strtotime having some problems with php5

Code: Select all

function strtotimefix($strtotime)
{
   return time() + (strtotime($strtotime) - strtotime('now'));
}

Posted: Wed Jun 22, 2005 4:54 pm
by pickle
Ya, if you're having problems, use ~Jcart's suggestion rather than adding seconds - that always causes problems!