PHP converts the gmt+8 string of the database to the user's local time zone

Source: Internet
Author: User
Tags php server php framework
    1. Write User time zone to cookie
    2. PHP reads the user time zone and processes

The moment the user opens the browser, uses JS to get the time zone and write the cookie, which can be manipulated using an open source library on GitHub .

https://GitHub. Com/js-cookie/js-cookie

The specific code is as follows

        Write the time zone to a cookie in minutes, but note that offset here is a negative number, indicating the difference between local time and GMT        var d = new Date ();        

On the PHP server side, if the time value stored in the database field is timestamp, then it is good to handle a lot, but it is important to note that if your server defaults to the timestamp of the gmt+8 time zone instead of the GMT timestamp, then you need to process the code as follows (here I use think PHP Framework )

            $tmp = Strtotime ($strTime);  This converts a string of y-m-d h:i format into a timestamp            $tmp = $tmp-(8 * 3600);//The string stored by the server is gmt+8, so here I subtract 8 hours of milliseconds, which is the time stamp of GMT            $timeZone = -cookie (' Localzone '); Because of the difference, need to add, so turn into a positive            $tmp = $tmp + ($timeZone * 60);//plus the time zone of the browser's location            $logInfo [$i] [' access_time '] = Date (' y-m-d h:i ', $TMP); Format timestamp as y-m-d h:i

The above describes the PHP conversion database gmt+8 string for the user's local time zone, including the contents of the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.