예제 #1
0
 /**
  * Returns a new DateTimeEx object representing the first day of the week.
  * 
  * If `$date` is given that date's first day of week will be returned.
  * @param mixed $date Starting point for the calculation
  * @return DateTimeEx The created instance
  */
 public static function FirstDayOfWeek($date = false)
 {
     $dt = new DateTimeEx(date('Y-m-d 0:00:00', intval(DateTimeEx::Make($date)->format('U'))));
     $dt->sub(new DateInterval('P' . ($dt->format('w') > 0 ? $dt->format('w') - 1 : 7) . 'D'));
     return $dt;
 }
예제 #2
0
 /**
  * Static tool method to ensure $value is of type <DateTimeEx>.
  * 
  * @param mixed $value Some value representing a datetime
  * @param bool $convert_now_to_value if true will check if `$value=='now()'` and if so return `new DateTimeEx` instead of `'now()'`
  * @return mixed <DateTimeEx> value or 'now()' if $convert_now_to_value is fale and value is 'now()'
  */
 public static function EnsureDateTime($value, $convert_now_to_value = false)
 {
     if ($value === null) {
         return null;
     }
     if ($value instanceof DateTimeEx) {
         return $value;
     }
     if ($value instanceof DateTime) {
         return DateTimeEx::Make($value);
     }
     if (is_string($value)) {
         // special handling for NOW() argument
         if (strtolower($value) == "now()") {
             if ($convert_now_to_value) {
                 return new DateTimeEx();
             }
             return "now()";
         }
         // check if we have a timestamp as string
         if (is_numeric($value)) {
             $res = new DateTimeEx();
             $res->setTimestamp(intval($value));
             return $res;
         } else {
             // add eventually missing time part
             $value = preg_replace('/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/', '$1-$2-$3 00:00:00', $value);
             // add eventually missing fractional seconds part to ISO 8601 format
             $value = preg_replace('/([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})([+-]{1})([0-9]{2}):([0-9]{2})/', '$1-$2-$3T$4:$5:$6.00$7$8:$9', $value);
         }
     } elseif (is_integer($value)) {
         $res = new DateTimeEx();
         $res->setTimestamp($value);
         return $res;
     }
     return new DateTimeEx($value);
 }