source

PHP에서 서버 시간대 가져오기

nicesource 2023. 2. 14. 21:30
반응형

PHP에서 서버 시간대 가져오기

내 클라이언트는 플러그인 실행 중인 서버 세부 정보를 시스템 보고해야 합니다. 즉, SERVER 타임존을 취득해야 합니다.워드프레스로 작업합니다.시도해보았지만 모든 서버에서 작동되는 것은 아닙니다.

$date = new DateTime();
$date->setTimezone(new DateTimeZone(ini_get('date.timezone')));

이로 인해 일부 서버에서 치명적인 오류가 발생합니다. 서버 시간대를 가져오는 공통 함수가 있습니까?
어떤 제안이라도 대단히 감사합니다.

이것을 시험해 보세요.

$date = new DateTime();
$timeZone = $date->getTimezone();
echo $timeZone->getName();

다음 링크를 참조하십시오.https://www.w3schools.com/php/func_date_default_timezone_get.asp

    <?php
      echo date_default_timezone_get();
    ?> 

date_default_timezone_get() 함수는 스크립트의 모든 날짜/시간 함수에 사용되는 기본 시간대를 반환합니다.

<?php 
date_default_timezone_set();
if (date_default_timezone_get()) 
{
    echo 'date_default_timezone_set: ' . date_default_timezone_get() . '<br />';
}
if (ini_get('date.timezone')) 
{
    echo 'date.timezone: ' . ini_get('date.timezone');
}?>

이 코드가 도움이 될 거야

지금까지 설명한 모든 내용은 PHP 기본 시간대를 제공합니다.이 시간대는 php.ini로 설정되거나 코드의 어딘가에서 덮어쓸 수 있습니다.SERVER 타임존을 찾고 있는 경우는, 다음과 같이 해결합니다.

$timezone = $_SERVER['TZ'] ??
    (file_get_contents('/etc/timezone') ?: file_get_contents('/etc/localtime'));

Python에게 도움을 청합니다.python으로 서버 타임존 오프셋을 얻을 수 있습니다.

from time import gmtime, strftime
print strftime("%z", gmtime())

exec을 사용하여 php에서 python 코드를 실행합니다.

$tz_offset = exec("python tz.py");

언급URL : https://stackoverflow.com/questions/37429837/get-server-timezone-in-php

반응형