mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
Added function to convert time from user timezone to server timezone.
This commit is contained in:
parent
a96de99388
commit
058d57a85a
2 changed files with 38 additions and 0 deletions
|
@ -394,6 +394,27 @@ class CakeTimeTest extends CakeTestCase {
|
|||
$this->assertEquals(false, $this->Time->toUnix(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* testToServer method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testToServer() {
|
||||
$tzBackup = date_default_timezone_get();
|
||||
|
||||
date_default_timezone_set('UTC');
|
||||
$serverTime = new DateTime('now');
|
||||
|
||||
$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
|
||||
foreach ($timezones as $timezone) {
|
||||
$result = $this->Time->toServer($serverTime->format('Y-m-d H:i:s'), $timezone, 'U');
|
||||
$tz = new DateTimeZone($timezone);
|
||||
$this->assertEquals($serverTime->format('U'), $result + $tz->getOffset($serverTime));
|
||||
}
|
||||
|
||||
date_default_timezone_set($tzBackup);
|
||||
}
|
||||
|
||||
/**
|
||||
* testToAtom method
|
||||
*
|
||||
|
|
|
@ -471,6 +471,23 @@ class CakeTime {
|
|||
return self::fromString($dateString, $timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a formatted date in server's timezone.
|
||||
*
|
||||
* @param string $dateString Datetime string
|
||||
* @param mixed $timezone Timezone string or DateTimeZone object
|
||||
* @param string $format date format string
|
||||
* @return mixed Formatted date
|
||||
*/
|
||||
public static function toServer($dateString, $timezone = null, $format = 'Y-m-d H:i:s') {
|
||||
if ($timezone === null) {
|
||||
$timezone = Configure::read('Config.timezone');
|
||||
}
|
||||
$time = new DateTime($dateString, new DateTimeZone($timezone));
|
||||
$time->setTimezone(new DateTimeZone(date_default_timezone_get()));
|
||||
return $time->format($format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a date formatted for Atom RSS feeds.
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue