本文整理汇总了PHP中Datetime::setTimeZone方法的典型用法代码示例。如果您正苦于以下问题:PHP Datetime::setTimeZone方法的具体用法?PHP Datetime::setTimeZone怎么用?PHP Datetime::setTimeZone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Datetime
的用法示例。
在下文中一共展示了Datetime::setTimeZone方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_create_new_blank_datetime
/**
* @since 4.6.x
*/
public function test_create_new_blank_datetime()
{
//if timezone is empty string then the setUp didn't work correctly. For the purpose of this test
//we want a high positive timezone, so let's force that if necessary
if (get_option('timezone_string') != 'Australia/Sydney') {
update_option('timezone_string', 'Australia/Sydney');
EEM_Datetime::reset();
EEM_Datetime::instance();
}
EE_Registry::instance()->load_helper('DTT_Helper');
//make sure now is in the timezone we want to test with.
$now = new Datetime('@' . (time() + DAY_IN_SECONDS * 30));
$now->setTimeZone(new DateTimeZone(EEH_DTT_Helper::get_timezone()));
$now->setTime('8', '0', '0');
$now->setTimeZone(new DateTimeZone('America/Toronto'));
//get the default datetime
$default_date = EEM_Datetime::instance()->create_new_blank_datetime();
$default_date = reset($default_date);
//assert instance
$this->assertInstanceOf('EE_Datetime', $default_date);
//set its timezone to match our expected timezone
$default_date->set_timezone('America/Toronto');
$actual = $default_date->get_DateTime_object('DTT_EVT_start');
$this->assertInstanceOf('DateTime', $actual);
//assert timezones are the same
$this->assertEquals($now->getTimezone(), $actual->getTimeZone());
//assert that we have the correct values on the date... we'll do each part separately to verify.
$this->assertEquals($now->format('Y'), $actual->format('Y'));
$this->assertEquals($now->format('m'), $actual->format('m'));
$this->assertEquals($now->format('d'), $actual->format('d'));
$this->assertEquals($now->format('H'), $actual->format('H'));
$this->assertEquals($now->format('i'), $actual->format('i'));
}
示例2: date
static function convert_datetime($timestamp, $to_tz, $from_tz = 'UTC')
{
// Has a specific timestamp been given?
if (NULL === $timestamp) {
$timestamp = date('Y-m-d H:i:s');
} else {
// Are we dealing with a UNIX timestamp or a datetime?
if (!is_numeric($timestamp)) {
if (!$timestamp || $timestamp == '0000-00-00') {
return '';
}
$timestamp = date('Y-m-d H:i:s', strtotime($timestamp));
} else {
if (!$timestamp) {
return '';
}
$timestamp = date('Y-m-d H:i:s', $timestamp);
}
}
// --------------------------------------------------------------------------
// Perform the conversion
$_from_tz = new DateTimeZone($from_tz);
$_out = new Datetime($timestamp, $_from_tz);
// Set the output timezone
$_to_tz = new DateTimeZone($to_tz);
$_out->setTimeZone($_to_tz);
return $_out->format('Y-m-d H:i:s');
}