Countdown to the e-timer date

I use e-timer on the website. you need to make a countdown every day to 20-00 if this time has come, then make a countdown to the next day 20-00. We need to tie this whole thing to Moscow time. So that the timer on the site for everyone works according to Moscow time without being tied to local time. As I understand it, you need to connect the counter to the server time and not to the user's local time.

Here in this format the counter accepts the date to which it will count etDate: "07.09.2018.0.0"

I made a small squeak in php (of course it's crooked).

    $hour = date('H');

    if ($hour >= 19 && $hour <= 24  ) {
        $date = new DateTime('+1 days');
    } else {
        $date = new DateTime('+0 days');
    }

It actually passes the data to the counter

 jQuery(document).ready(function() {
    jQuery(".eTimer").eTimer({
        etType: 0,
        etDate: "<?php echo $date->format('d.m.Y');?>.19.00",
    });
});

It all works... But it is tied to the user's local time. I repeat, it is necessary that the timer counts only in Moscow time for everyone. You also need to use only this timer.

Link to the timer https://e-timer.ru/

Author: Kromster, 2018-09-06

2 answers

We transmit the local time of the server to the client and count the difference between the time of the client and the server already on the client. Then we add this difference to the time in the timer. Defining the target date:

$hour = date('H');
if ($hour >= 19 && $hour <= 24  ) {
    $date = new DateTime('tomorrow 19:00');
} else {
    $date = new DateTime('today 19:00');
}

Code for the starnitsa:

jQuery(document).ready(function() {
    var serverTime = new Date(<?=(new DateTime())->getTimestamp()?>*1000);
    var localTime = new Date();
    var diff = serverTime - localTime;
    var targetTime = new Date(<?=$date->getTimestamp()?>*1000));
    targetTime.setTime(targetTime.getTime() + diff);

    jQuery(".eTimer").eTimer({
        etType: 0,
        etDate: targetTime.getDate() + '.' + (targetTime.getMonth() + 1) + '.'+  targetTime.getFullYear() + '.' +  targetTime.getHours()+ '.' + targetTime.getMinutes() ,
    });
});
 0
Author: Yuriy Prokopets, 2018-09-14 11:54:50

This option is bound to the +3 offset, meaning it doesn't switch to daylight saving time and winter time itself. A good option is to link the date to the time zone using the format Europe/Moscow to do this, you can use moment-timezone

// в примере используется текущая дата + 24 часа
// дату можно указывать просто '2018-10-20 20:00'
const end = Date.now() + 1000 * 60 * 60 * 24 
const endDate = calcTime(new Date(end))

setInterval(() => {
  const result = getTimeRemaining(endDate)
  let formated = ''
  
  for (let part in result)
    formated += `${part}: ${result[part]} | `
  // const formated = Object.values(result).join(':')

  display.innerHTML = formated.slice(0, -2)
}, 500)

function getTimeRemaining(endtime){
  const t = endtime - calcTime()
  let obj = {
    days: t / (1000 * 60 * 60 * 24) | 0,
    hours: t / (1000 * 60 * 60) % 24 | 0,
    minutes: t / 1000 / 60 % 60 | 0,
    seconds: t / 1000 % 60 | 0
  }

  for (let key in obj)
    obj[key] = ('0' + obj[key]).slice(-2)
  
  return obj
}

// дата с нужным смещением
function calcTime(d = new Date(), offset = 3) {
    utc = d.getTime() + (d.getTimezoneOffset() * 60000)

    nd = new Date(utc + (3600000 * offset))

    return nd
}
<span id="display"></span>
 0
Author: Илья Зеленько, 2018-09-24 13:32:34