PHP 时区只改变部分时间
PHP timezone only changing part of the times
我试图在三个不同的时区显示事件时间,所以我在文本字段中设置时间(“2:00 pm”),默认情况下是美国东部时间,输出应该是:
2:00 pm EDT / 1:00 pm CDT / 11:00 am PDT
但它只是显示为:
2:00 pm EDT / 2:00 pm CDT / 2:00 pm PDT
因此,时间不会转换,但时区会。这是我的代码:
// Get the value from the text field
$value = '2:00 pm';
// Let's first check to see if the value is a valid time
if( strtotime( $value ) ){
// Let's convert the time into an actual time, using today's date as filler
date_default_timezone_set( 'America/New_York' );
$time = date( 'Y-m-d g:i:s a', strtotime( 'today '.$value ) );
// List the timezones we want to return
$timezones = [
'US/Eastern',
'America/Chicago',
'America/Los_Angeles',
];
// Empty array
$display = [];
// Cycle through each timezone
foreach( $timezones as $timezone ) {
// Let's set the timezone
date_default_timezone_set( $timezone );
// Get the time in the new timezone
$new_time = date( 'g:i a T', strtotime( $time ) );
// Make the initials lowercase for the class
$ini = strtolower( date( 'T', strtotime( $value ) ) );
// Add the time to the array
$display[] = '<span class="'.$ini.'-time">'.$new_time.'</span>';
}
// Return all of the times from the array
return implode(' <span class="sep-time">/</span> ', $display );
} else {
// Else state it's not valid
return '<strong>INVALID TIME FORMAT - PLEASE USE "H:MM AM/PM"</strong>';
}
您可以看到我从 date()
函数中获取了时区缩写(EDT、CDT、PDT),这些都很好地改变了,但实际时间却没有。我尝试切换到 'H:i'
而不是 'g:i'
,但它只是更改为晚上 7 点、下午 6 点和下午 4 点,这些时间都提前了 5 小时。
我改变时区的方法:
$time = new DateTime("now", new DateTimeZone("UTC");
$time->setTimezone(new DateTimeZone("US/Eastern")); // now it's US/Eastern
$time->setTimezone(new DateTimeZone("America/Chicago")); // now it's America/Chicago
确保输入提示:
use DateTime, DateTimeZone;
我试图在三个不同的时区显示事件时间,所以我在文本字段中设置时间(“2:00 pm”),默认情况下是美国东部时间,输出应该是:
2:00 pm EDT / 1:00 pm CDT / 11:00 am PDT
但它只是显示为:
2:00 pm EDT / 2:00 pm CDT / 2:00 pm PDT
因此,时间不会转换,但时区会。这是我的代码:
// Get the value from the text field
$value = '2:00 pm';
// Let's first check to see if the value is a valid time
if( strtotime( $value ) ){
// Let's convert the time into an actual time, using today's date as filler
date_default_timezone_set( 'America/New_York' );
$time = date( 'Y-m-d g:i:s a', strtotime( 'today '.$value ) );
// List the timezones we want to return
$timezones = [
'US/Eastern',
'America/Chicago',
'America/Los_Angeles',
];
// Empty array
$display = [];
// Cycle through each timezone
foreach( $timezones as $timezone ) {
// Let's set the timezone
date_default_timezone_set( $timezone );
// Get the time in the new timezone
$new_time = date( 'g:i a T', strtotime( $time ) );
// Make the initials lowercase for the class
$ini = strtolower( date( 'T', strtotime( $value ) ) );
// Add the time to the array
$display[] = '<span class="'.$ini.'-time">'.$new_time.'</span>';
}
// Return all of the times from the array
return implode(' <span class="sep-time">/</span> ', $display );
} else {
// Else state it's not valid
return '<strong>INVALID TIME FORMAT - PLEASE USE "H:MM AM/PM"</strong>';
}
您可以看到我从 date()
函数中获取了时区缩写(EDT、CDT、PDT),这些都很好地改变了,但实际时间却没有。我尝试切换到 'H:i'
而不是 'g:i'
,但它只是更改为晚上 7 点、下午 6 点和下午 4 点,这些时间都提前了 5 小时。
我改变时区的方法:
$time = new DateTime("now", new DateTimeZone("UTC");
$time->setTimezone(new DateTimeZone("US/Eastern")); // now it's US/Eastern
$time->setTimezone(new DateTimeZone("America/Chicago")); // now it's America/Chicago
确保输入提示:
use DateTime, DateTimeZone;