How to get Unix timestamp in php based on timezone

后端 未结 5 580
温柔的废话
温柔的废话 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:16

    Had the same issue myself, and this is the easiest approach I could do fairly quickly.

    $dateTimeZone = new DateTimeZone("America/New_York");
    $dateTime = new DateTime("now", $dateTimeZone);
    $timeOffset = $dateTimeZone->getOffset($dateTime);
    
    // New time since epoch according to timezone
    $newTime = time() + $timeOffset;
    
    echo date("H:i:s", $newTime);
    

提交回复
热议问题