Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added function to convert time from user timezone to server timezone.

  • Loading branch information...
commit 058d57a85a51213727b98ed411cbe52c3c40ad1d 1 parent a96de99
@ADmad ADmad authored
View
21 lib/Cake/Test/Case/Utility/CakeTimeTest.php
@@ -395,6 +395,27 @@ public function testToUnix() {
}
/**
+ * 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
*
* @return void
View
17 lib/Cake/Utility/CakeTime.php
@@ -472,6 +472,23 @@ public static function toUnix($dateString, $timezone = null) {
}
/**
+ * 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.
*
* @param string $dateString Datetime string or Unix timestamp
Please sign in to comment.
Something went wrong with that request. Please try again.