How to transfer time from php to js?

  • 0
    Something is not clear at all I get the time with php getdate () I take it 1602079775 multiply it by 1000 and output it to js.
    let d = new Date(1602083213 * 1000);
    alert(d );


    But every time the time zone is changed on the computer, it gives a different time, how can you set a specific time so that the time displayed from php coincides with the one on js?
    JavaScript Anonymous, Dec 6, 2020

  • 1 Answers
  • 0
    On the php side, you should not transfer the timestamp in seconds, but specifically the date and time!

    <?php

    // Устанавливаем тип документа и его кодировку:
    header('Content-Type: text/html; charset=utf-8');

    // Включаем показ ошибок:

    ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);

    // Записываем в переменную текущую дату и время:

    $data = date('d-m-Y H:i:s');

    // Выводим скрипт:

    echo '<script type="text/javascript">
    let d = "'.$data.'";
    alert(d);
    </script>';

    ?>
    Adam Collier

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!