server/src/app/Libraries/Utils.php
changeset 19 eadaf0b8f02e
parent 18 f2a40bbc27f6
child 20 a9b98b16b053
--- a/server/src/app/Libraries/Utils.php	Tue Nov 17 13:11:55 2015 +0100
+++ b/server/src/app/Libraries/Utils.php	Fri Nov 27 17:59:36 2015 +0100
@@ -6,6 +6,42 @@
  */
 class Utils {
 
+    /**
+     * convert DateIntervals to milliseconds
+     * Months and year calculated by approximation based on average number
+     * of days over 4 years (365*4+1)
+     * @param \DateInterval $di the date interval
+     * @return the number of milliseconds or 0 if $di is null
+     */
+    public static function dateIntervalToMillis(\DateInterval $di) {
+        if(is_null($di)) {
+            return 0;
+        }
+        $seconds = ($di->s)
+         + ($di->i * 60)
+         + ($di->h * 60 * 60)
+         + ($di->d * 60 * 60 * 24)
+         + ($di->m * (365*4+1)/48*60*60*24)
+         + ($di->y * (365*4+1)/4*60*60*24);
+        return $seconds*1000;
+    }
+
+    /**
+     * convert iso8601 strings to milliseconds
+     * Months and year calculated by approximation based on average number
+     * of days over 4 years (365*4+1)
+     *
+     * @param str iso8601 string
+     * @return the number of milliseconds or 0 if $str is null
+     */
+    public static function iso8601IntervalToMillis($str) {
+        if(is_null($str) || $str === '') {
+            return 0;
+        }
+        $di = new \DateInterval($str);
+        return self::dateIntervalToMillis($di);
+    }
+
     /*
      * From http://www.thecave.info/php-get-mime-type-from-file-extension/
      */