Пример #1
0
 /**
  * String to time
  *
  * @param   string $date   Date representation
  * @param   string $format Date format
  *
  * @return    array    date bits keyed on date representations e.g.  m/d/Y
  */
 public static function str2Time($date, $format)
 {
     /**
      * lets check if we have some special text as per :
      * http://php.net/strtotime - this means we can use "+2 week" as a url filter
      * do this before we urldecode the date otherwise the + is replaced with ' ';
      */
     $matches = array();
     $matches2 = array();
     $matches3 = array();
     // E.g. now
     preg_match("/[now|ago|midnight|yesterday|today]/i", $date, $matches);
     // E.g. +2 Week
     preg_match("/[+|-][0-9]* (week\\b|year\\b|day\\b|month\\b)/i", $date, $matches2);
     // E.g. next Wednesday
     preg_match("/[next|last]* (\\monday\\b|tuesday\\b|wednesday\\b|thursday\\b|friday\\b|saturday\\b|sunday\\b)/i", $date, $matches3);
     $matches = array_merge($matches, $matches2, $matches3);
     if (!empty($matches)) {
         $d = JFactory::getDate($date);
         $date = $d->format($format);
     }
     /* $$$ - hugh : urldecode (useful when ajax calls, may need better fix)
      * as per http://fabrikar.com/forums/showthread.php?p=43314#post43314
      */
     $date = urldecode($date);
     // Strip any textual date representations from the string
     $days = array('%A', '%a');
     foreach ($days as $day) {
         if (strstr($format, $day)) {
             $format = str_replace($day, '', $format);
             $date = self::stripDay($date, $day == '%a' ? true : false);
         }
     }
     $months = array('%B', '%b', '%h');
     foreach ($months as $month) {
         if (strstr($format, $month)) {
             $format = str_replace($month, '%m', $format);
             $date = self::monthToInt($date, $month == '%B' ? false : true);
         }
     }
     // @TODO: some of these aren't right for strftime
     self::$finalFormat = $format;
     $search = array('%d', '%e', '%D', '%j', '%m', '%b', '%Y', '%y', '%g', '%H', '%h', '%i', '%s', '%S', '%M');
     $replace = array('(\\d{2})', '(\\d{1,2})', '(\\w{3})', '(\\d{1,2})', '(\\d{2})', '(\\w{3})', '(\\d{4})', '(\\d{2})', '(\\d{1,2})', '(\\d{2})', '(\\d{2})', '(\\d{2})', '(\\d{2})', '(\\d{2})', '(\\d{2})');
     $pattern = str_replace($search, $replace, $format);
     if (!preg_match("#{$pattern}#", $date, $matches)) {
         // Lets allow for partial date formats - e.g. just the date and ignore the time
         $format = explode('%', $format);
         if (empty($format)) {
             // No format left to test so return false
             return false;
         }
         array_pop($format);
         $format = trim(implode('%', $format));
         self::$finalFormat = $format;
         return self::str2Time($date, $format);
     }
     $dp = $matches;
     if (!preg_match_all('#%(\\w)#', $format, $matches)) {
         return false;
     }
     $id = $matches['1'];
     if (count($dp) != count($id) + 1) {
         return false;
     }
     $ret = array();
     for ($i = 0, $j = count($id); $i < $j; $i++) {
         $ret[$id[$i]] = $dp[$i + 1];
     }
     return $ret;
 }