TAGS :Viewed: 2 - Published at: a few seconds ago

[ Calculating local time with UTC offset ]

This is probably basic, but I can't seem to think straight when it comes to offset.

Currently, I display time by converting datetime from db like this:

date('g:i A', strtotime($given_date));

However, now I get an additional value from db, which UTC shows offset time in minutes, Currently it is: $offsetTime = -300;

So, to get the current local time, in this case I need to subtract, something like this

date('g:i A', strtotime($given_date) - $offsetTime * 60);

But what if offset is a positive number?

Answer 1


You state that $offsetTime already is a negative value

 $offsetTime = -300;

Therefore you must add it to the date

date('g:i A', strtotime($given_date) + ($offsetTime * 60); // use plus-char

Because

+ (-300 * 60) = + -18000 = - 18000

If you have a positive offset of let's say 120 you will get:

+ (120 * 60) = + 7200