Permalink
Browse files

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

  • Loading branch information...
1 parent a96de99 commit 058d57a85a51213727b98ed411cbe52c3c40ad1d @ADmad ADmad committed Apr 18, 2012
Showing with 38 additions and 0 deletions.
  1. +21 −0 lib/Cake/Test/Case/Utility/CakeTimeTest.php
  2. +17 −0 lib/Cake/Utility/CakeTime.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
@@ -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

0 comments on commit 058d57a

Please sign in to comment.