+ Reply to Thread
Results 1 to 4 of 4

Thread: PHP server time to local time

  1. #1
    denzil is offline x10 Sophmore denzil is an unknown quantity at this point
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    134

    Question PHP server time to local time

    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)

  2. #2
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: PHP server time to local time

    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  3. #3
    denzil is offline x10 Sophmore denzil is an unknown quantity at this point
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    134

    Re: PHP server time to local time

    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 by denzil; 02-27-2011 at 01:50 PM. Reason: logic error in code fixed

  4. #4
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: PHP server time to local time

    MyBB will have a timezone helper built in already, I imagine

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

+ Reply to Thread

Similar Threads

  1. Service dont work from time to time
    By lassiter in forum Free Hosting
    Replies: 1
    Last Post: 06-30-2009, 12:07 AM
  2. Server Time-Zone To Local Time-Zone
    By rockee in forum Tutorials
    Replies: 4
    Last Post: 06-02-2008, 09:58 AM
  3. Best way to get clients local time.
    By oracle in forum Programming Help
    Replies: 7
    Last Post: 05-29-2008, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers