How to get Unix timestamp in php based on timezone

后端 未结 5 581
温柔的废话
温柔的废话 2020-11-30 07:54

Code first

    echo time() . \'
\'; echo date(\'Y-m-d H:i:s\') . \'
\'; date_default_timezone_set(\'America/New_York\'); echo time() .
5条回答
  •  半阙折子戏
    2020-11-30 08:14

    The answer provided by Volkerk (that says timestamps are meant to be always UTC based) is correct, but if you really need a workaround (to make timezone based timestamps) look at my example.

    getTimestamp().'
    '."\r\n"; //America/New_York $date = new DateTime(null, new DateTimeZone('America/New_York')); echo 'America/New_York: '.$date->getTimestamp().'
    '."\r\n"; //Europe/Amsterdam $date = new DateTime(null, new DateTimeZone('Europe/Amsterdam')); echo 'Europe/Amsterdam: '.$date->getTimestamp().'
    '."\r\n"; echo 'WORK AROUND
    '."\r\n"; // WORK AROUND //default timezone $date = new DateTime(null); echo 'Default timezone: '.($date->getTimestamp() + $date->getOffset()).'
    '."\r\n"; //America/New_York $date = new DateTime(null, new DateTimeZone('America/New_York')); echo 'America/New_York: '.($date->getTimestamp() + $date->getOffset()).'
    '."\r\n"; //Europe/Amsterdam $date = new DateTime(null, new DateTimeZone('Europe/Amsterdam')); echo 'Europe/Amsterdam: '.($date->getTimestamp() + $date->getOffset()).'
    '."\r\n"; ?>

    Get the regular timestamp and add the UTC offset

提交回复
热议问题