PHP server time to local time

denzil

New Member
Messages
134
Reaction score
3
Points
0
I'm having an issue converting server time to local time. On my website I have a board where members can post messages. Nothing fancy, but it also displays the time the user posted the message. The time is saved as a timestamp value in my mysql database (I just use the field's default value feature set to current timestamp). Now when I read the messages and timestamp from my database, it displays the correct time as server time.

However, I'd like that time to be converted to the user's local time. I've been looking around but haven't yet come up with a working solution. Any ideas?

(Note, I'm using PHP)
 

denzil

New Member
Messages
134
Reaction score
3
Points
0
hmm. thanx. This solution requires the user to select his own timezone from a list of options. I suppose I have the other's time zone saved in my MyBB forums database. So I can just use that to correct the time zone. But I would've liked it all to be automated (without any user input). I suppose it's bot very easy to implement though.

It looks like it can be done using some javascript. I don't know enough to do it yet.

*I have decided to just use the settings saved in my mybb forums database. These values are timezone offset and dst (for day light savings). The timezone offset, for example, is 2 for GMT+2, or -5 for GMT -5 and so on.

How can use this to adjust my timestamp I'm reading from my database? I tried taking the hour offset and multiply it by 60*60 to give me seconds and subtract that from my timestamp. But it didn't work. My timestamp is a date format. And if I subtract anything from it it just subtracts from the year, and not from the total seconds since UNIX epoch.

Any solutions? Or perhaps a method to convert timestamp to seconds?

---------- Post added at 01:14 PM ---------- Previous post was at 12:01 PM ----------

I'll just add this here for anyone possibly interested. I found that
strtotime($timestamp)
where timestamp is the timestamp read from mysql database, will convert it to a timestamp (seconds from UNIX epoch).
I can then subtract the timezone offset read from mybb (as seconds) to adjust the time. Then convert back to a string.
Code:
 $time = date("Y-m-d H:i:s",strtotime($timestamp) + ($mybb->user["timezone"]+6)*60*60-4*60) ;
The "$mybb->user["timezone"]+6)*60*60" bit takes the timezone offset from mybb. I'm adding 6 to account for the server location. And the "-4*60" bit at the end (which relates to 4 minutes) I just added to fix a discrepancy I picked up between my forums time and my websites time (as saved in the database).
 
Last edited:

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
MyBB will have a timezone helper built in already, I imagine

~Callum
 
Top