T
T
Tsarev Vadim2019-02-03 12:27:40
PHP
Tsarev Vadim, 2019-02-03 12:27:40

How to display the time on the site regardless of the time zone of the client and its system time?

Good afternoon. How to display the time on the site regardless of the time zone of the client and its system time? rummaged through a bunch of information in a clear language, no one wrote. It seems that all the authors write somewhere in the wrong place (((
Who can know an adequate solution, I want to display the organization's working time on the site in local time! SOS!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DanKud, 2019-02-03
@DanKud

Display the time in UTC and add the desired time zone. For example, for Moscow +3 hours:

$offset = 3; /* смещение часово пояса */
$date = gmdate('d-m-Y - H:i:s', (time() + ($offset * 3600))); /* прибавляем к time() три часа */
echo $date;
Or it's even easier to set the default time zone in the script, because due to winter/summer time translations, there may be an incorrect offset:
date_default_timezone_set('Europe/Moscow'); /* устанавливаем временную зону */
$date = date('d-m-Y - H:i:s');
echo $date;
list of time zones

T
Tsarev Vadim, 2019-02-03
@zava75

Might be useful to someone

$(function () {
     setTimeout(function() {
            $.ajax({
                url: '/clock.php',
                type: 'POST',
            }).done(function(dataSite) {
                $('#time').html(dataSite);
            })
        }, 0);
     setInterval(function() {
            $.ajax({
                url: '/clock.php',
                type: 'POST',
            }).done(function(dataSite) {
                $('#time').html(dataSite);
            })
        }, 60000);
    var h = parseInt ($('#time').text(), 10);
        var imeWork = (h >= 9 && h <= 18) ? '<span class="green";>Мы работаем</span>' : '<span class="red";>Мы закрыты</span>';
        $('#work').html(imeWork);
    });

clock.php
<?php
date_default_timezone_set('Asia/Vladivostok'); /* устанавливаем временную зону */
$dateSite = date('H:i');
echo $dateSite;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question