users to view in local timezone

后端 未结 2 950
温柔的废话
温柔的废话 2021-01-20 20:01

I\'ve asked a very similar question before but got no answers that helped.

I have a site that allows users to post notes. There will be a time stamp on those notes.

相关标签:
2条回答
  • 2021-01-20 20:30

    What you can do is save the UTC time when saving and when viewing show the UTC time + offset. This can all be done using JS on client side.

    JS method used when saving= UTC()

    JS method used when displaying =

    <html>
    <head>
    <script type="text/javascript">
            function showDateInClientSideFormat(dValue)
            {
                var d = new Date()
                var n = d.getTimezoneOffset();
                var dateClientSide = new Date(dValue +n);
                return dateClientSide;
            }
        </script>
    </head>
    <body>
    <?php
    echo '<script> showDateInClientSideFormat($dateSaved); </script>';
    ?>
    </body>
    </html>
    

    PS: UTC time is the same as GMT time

    0 讨论(0)
  • 2021-01-20 20:34

    You could use PHPs DateTime and DateTimeZone to convert the datetime into the users current timezone:

    http://www.php.net/manual/de/class.datetime.php
    http://www.php.net/manual/de/class.datetimezone.php

    $date = new DateTime($dateTimeFromDB);
    $date->setTimeZone( new DateTimeZone('User/Timezone') );
    
    echo $date->format('d.m.Y H:i');
    
    0 讨论(0)
提交回复
热议问题