Browse Source

Merge pull request #6389 from icaroscherma/patch-1

Timezone option for timeAgoInWords now works.
Mark Story 11 years ago
parent
commit
e98d0e19d8
2 changed files with 27 additions and 3 deletions
  1. 9 2
      src/I18n/Time.php
  2. 18 1
      tests/TestCase/I18n/TimeTest.php

+ 9 - 2
src/I18n/Time.php

@@ -246,6 +246,8 @@ class Time extends Carbon implements JsonSerializable
      */
     public function timeAgoInWords(array $options = [])
     {
+        $time = $this;
+        
         $timezone = null;
         $format = static::$wordFormat;
         $end = static::$wordEnd;
@@ -272,8 +274,13 @@ class Time extends Carbon implements JsonSerializable
             }
         }
 
+        if ($timezone) {
+            $time = clone $this;
+            $time->timezone($timezone);
+        }
+
         $now = $from->format('U');
-        $inSeconds = $this->format('U');
+        $inSeconds = $time->format('U');
         $backwards = ($inSeconds > $now);
 
         $futureTime = $now;
@@ -289,7 +296,7 @@ class Time extends Carbon implements JsonSerializable
         }
 
         if ($diff > abs($now - (new static($end))->format('U'))) {
-            return sprintf($absoluteString, $this->i18nFormat($format));
+            return sprintf($absoluteString, $time->i18nFormat($format));
         }
 
         // If more than a week, then take into account the length of months

+ 18 - 1
tests/TestCase/I18n/TimeTest.php

@@ -174,7 +174,24 @@ class TimeTest extends TestCase
             ],
         ];
     }
-
+    /**
+     * test the timezone option for timeAgoInWords
+     *
+     * @return void
+     */
+    public function testTimeAgoInWordsTimezone()
+    {
+        $time = new Time('1990-07-31 20:33:00 UTC');
+        $result = $time->timeAgoInWords(
+            [
+                'timezone' => 'America/Vancouver',
+                'end' => '+1month',
+                'format' => 'dd-MM-YYYY HH:mm:ss'
+            ]
+        );
+        $this->assertEquals('on 31-07-1990 13:33:00', $result);
+    }
+     
     /**
      * test the end option for timeAgoInWords
      *