시간대, 언어 및 WordPress 옵션에서 형식으로 UTC를 문자열로 변환
UTC의 날짜-시간 문자열을 올바른 언어, 형식 및 시간대의 예쁜 날짜-시간 문자열로 변환하는 잘 문서화 된 함수를 만들었습니다. 자유롭게 복사하십시오.
예를 들어 "2019-05-30 18:06:01"
(UTC로) 전달 하면을 반환 "Maggio 30, 2019 10:06 am"
합니다.
/**
* Given a string with the date and time in UTC, returns a pretty string in the
* configured language, format and timezone in WordPress' options.
*
* @param string $utc_date_and_time
* e.g: "2019-05-30 18:06:01"
* This argument must be in UTC.
* @return string
* e.g: "Maggio 30, 2019 10:06 am"
* This returns a pretty datetime string in the correct language and
* following the admin's settings.
*/
function pretty_utc_date( string $utc_date ): string {
if (! preg_match( '/^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$/', $utc_date ) ) {
/* I have not tested other formats, so only this one allowed. */
throw new InvalidArgumentException( "Expected argument to be in YYYY-MM-DD hh:mm:ss format" );
}
$date_in_local_timezone = get_date_from_gmt( $utc_date );
/* $date_in_local_timezone is now something like "2019-05-30 10:06:01"
* in the timezone of get_option( 'timezone_string' ), configured in
* WordPress' general settings in the backend user interface.
*/
/* Unfortunately, we can't just pass this to WordPress' date_i18n, as that
* expects the second argument to be the number of seconds since 1/Jan/1970
* 00:00:00 in the timezone of get_option( 'timezone_string' ), which is not the
* same as a UNIX epoch timestamp, which is the number of seconds since
* 1/Jan/1970 00:00:00 GMT. */
$seconds_since_local_1_jan_1970 =
(new DateTime( $date_in_local_timezone, new DateTimeZone( 'UTC' ) ))
->getTimestamp();
// e.g: 1559210761
/* Administrators can set a preferred date format and a preferred time
* format in WordPress' general settings in the backend user interface, we
* need to retrieve that. */
$settings_format = get_option( 'date_format' ) . ' '. get_option( 'time_format' );
// $settings_format is in this example "F j, Y g:i a"
/* In this example, the installation of WordPress has been set to Italian,
* and the final result is "Maggio 30, 2019 10:06 am" */
return date_i18n( $settings_format, $seconds_since_local_1_jan_1970 );
}
참고 문헌 :