本文整理匯總了PHP中Drupal\Core\Datetime\DrupalDateTime::getTimezone方法的典型用法代碼示例。如果您正苦於以下問題:PHP DrupalDateTime::getTimezone方法的具體用法?PHP DrupalDateTime::getTimezone怎麽用?PHP DrupalDateTime::getTimezone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Drupal\Core\Datetime\DrupalDateTime
的用法示例。
在下文中一共展示了DrupalDateTime::getTimezone方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: testDateTimezone
/**
* Test that DrupalDateTime can detect the right timezone to use.
* Test with a variety of less commonly used timezone names to
* help ensure that the system timezone will be different than the
* stated timezones.
*/
public function testDateTimezone()
{
global $user;
$date_string = '2007-01-31 21:00:00';
// Make sure no site timezone has been set.
\Drupal::config('system.date')->set('timezone.user.configurable', 0)->set('timezone.default', NULL)->save();
// Detect the system timezone.
$system_timezone = date_default_timezone_get();
// Create a date object with an unspecified timezone, which should
// end up using the system timezone.
$date = new DrupalDateTime($date_string);
$timezone = $date->getTimezone()->getName();
$this->assertTrue($timezone == $system_timezone, 'DrupalDateTime uses the system timezone when there is no site timezone.');
// Create a date object with a specified timezone.
$date = new DrupalDateTime($date_string, 'America/Yellowknife');
$timezone = $date->getTimezone()->getName();
$this->assertTrue($timezone == 'America/Yellowknife', 'DrupalDateTime uses the specified timezone if provided.');
// Set a site timezone.
\Drupal::config('system.date')->set('timezone.default', 'Europe/Warsaw')->save();
// Create a date object with an unspecified timezone, which should
// end up using the site timezone.
$date = new DrupalDateTime($date_string);
$timezone = $date->getTimezone()->getName();
$this->assertTrue($timezone == 'Europe/Warsaw', 'DrupalDateTime uses the site timezone if provided.');
// Create user.
\Drupal::config('system.date')->set('timezone.user.configurable', 1)->save();
$test_user = $this->drupalCreateUser(array());
$this->drupalLogin($test_user);
// Set up the user with a different timezone than the site.
$edit = array('mail' => $test_user->getEmail(), 'timezone' => 'Asia/Manila');
$this->drupalPostForm('user/' . $test_user->id() . '/edit', $edit, t('Save'));
// Disable session saving as we are about to modify the global $user.
\Drupal::service('session_manager')->disable();
// Save the original user and then replace it with the test user.
$real_user = $user;
$user = user_load($test_user->id(), TRUE);
// Simulate a Drupal bootstrap with the logged-in user.
date_default_timezone_set(drupal_get_user_timezone());
// Create a date object with an unspecified timezone, which should
// end up using the user timezone.
$date = new DrupalDateTime($date_string);
$timezone = $date->getTimezone()->getName();
$this->assertTrue($timezone == 'Asia/Manila', 'DrupalDateTime uses the user timezone, if configurable timezones are used and it is set.');
// Restore the original user, and enable session saving.
$user = $real_user;
// Restore default time zone.
date_default_timezone_set(drupal_get_user_timezone());
\Drupal::service('session_manager')->enable();
}
示例2: testDateTimezone
/**
* Test that DrupalDateTime can detect the right timezone to use.
* Test with a variety of less commonly used timezone names to
* help ensure that the system timezone will be different than the
* stated timezones.
*/
public function testDateTimezone()
{
$date_string = '2007-01-31 21:00:00';
// Make sure no site timezone has been set.
$this->config('system.date')->set('timezone.user.configurable', 0)->set('timezone.default', NULL)->save();
// Detect the system timezone.
$system_timezone = date_default_timezone_get();
// Create a date object with an unspecified timezone, which should
// end up using the system timezone.
$date = new DrupalDateTime($date_string);
$timezone = $date->getTimezone()->getName();
$this->assertTrue($timezone == $system_timezone, 'DrupalDateTime uses the system timezone when there is no site timezone.');
// Create a date object with a specified timezone.
$date = new DrupalDateTime($date_string, 'America/Yellowknife');
$timezone = $date->getTimezone()->getName();
$this->assertTrue($timezone == 'America/Yellowknife', 'DrupalDateTime uses the specified timezone if provided.');
// Set a site timezone.
$this->config('system.date')->set('timezone.default', 'Europe/Warsaw')->save();
// Create a date object with an unspecified timezone, which should
// end up using the site timezone.
$date = new DrupalDateTime($date_string);
$timezone = $date->getTimezone()->getName();
$this->assertTrue($timezone == 'Europe/Warsaw', 'DrupalDateTime uses the site timezone if provided.');
// Create user.
$this->config('system.date')->set('timezone.user.configurable', 1)->save();
$test_user = $this->drupalCreateUser(array());
$this->drupalLogin($test_user);
// Set up the user with a different timezone than the site.
$edit = array('mail' => $test_user->getEmail(), 'timezone' => 'Asia/Manila');
$this->drupalPostForm('user/' . $test_user->id() . '/edit', $edit, t('Save'));
// Reload the user and reset the timezone in AccountProxy::setAccount().
\Drupal::entityManager()->getStorage('user')->resetCache();
$this->container->get('current_user')->setAccount(User::load($test_user->id()));
// Create a date object with an unspecified timezone, which should
// end up using the user timezone.
$date = new DrupalDateTime($date_string);
$timezone = $date->getTimezone()->getName();
$this->assertTrue($timezone == 'Asia/Manila', 'DrupalDateTime uses the user timezone, if configurable timezones are used and it is set.');
}