Browse Source

HtmlHelper::time(): support \DatePeriod instances

Fabian Peter Hammerle 6 years ago
parent
commit
6557dee812
3 changed files with 14 additions and 4 deletions
  1. 1 1
      DateTimeHelper.php
  2. 1 3
      HtmlHelper.php
  3. 12 0
      tests/HtmlHelperTest.php

+ 1 - 1
DateTimeHelper.php

@@ -145,7 +145,7 @@ class DateTimeHelper
                 // printf("%d. %s\n", $repetitions, $dt->format(\DateTime::ATOM));
             }
             // \DatePeriod::getStartDate() is available from php 5.6.5.
-            $start_iso = $i->start->format(\DateTime::ATOM);
+            $start_iso = self::iso($i->start);
             // \DatePeriod::getDateInterval() is available from php 5.6.5.
             // \DatePeriod::$interval returned an invalid \DatePeriod instance
             // in php 7.0.8

+ 1 - 3
HtmlHelper.php

@@ -84,9 +84,7 @@ class HtmlHelper
 
     public static function time($dt, $content, array $attributes = [])
     {
-        if($dt instanceof \DateTime) {
-            $attr = $dt->format(\DateTime::W3C);
-        } elseif($dt instanceof \DateInterval) {
+        if(is_object($dt)) {
             $attr = DateTimeHelper::iso($dt);
         } else {
             $attr = $dt;

+ 12 - 0
tests/HtmlHelperTest.php

@@ -229,6 +229,18 @@ class HtmlHelperTest extends \PHPUnit_Framework_TestCase
                 ['title' => function($i) { return $i->format('%mm'); }],
                 '<time datetime="P1Y0M0DT0H0M15S" title="0m">1y, 15s</time>',
                 ],
+            [
+                \fphammerle\helpers\DateTimeHelper::parse('2012-07-01'),
+                function($p) { return $p->recurrences; },
+                [],
+                '<time datetime="2012-07-01T00:00:00+00:00/P0Y0M1DT0H0M0S">1</time>',
+                ],
+            [
+                new \DatePeriod('R4/2012-07-01T00:00:00Z/P7D'),
+                function($p) { return $p->recurrences; },
+                [],
+                '<time datetime="R4/2012-07-01T00:00:00+00:00/P0Y0M7DT0H0M0S">5</time>',
+                ],
             ];
     }