Exemple #1
0
 /**
  *
  * @param string date representation
  * @param string format that the date should be in
  * @return array date bits keyed on date representations e.g.  m/d/Y
  */
 function str2Time($date, $format)
 {
     static $finalformat;
     /**
      * 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();
     preg_match("/now/i", $date, $matches);
     //eg now
     preg_match("/[+|-][0-9]* (week\\b|year\\b|day\\b|month\\b)/i", $date, $matches2);
     //eg +2 Week
     preg_match("/[next|last]* (\\monday\\b|tuesday\\b|wednesday\\b|thursday\\b|friday\\b|saturday\\b|sunday\\b)/i", $date, $matches3);
     //eg next wednesday
     $matches = array_merge($matches, $matches, $matches2);
     if (!empty($matches)) {
         $d = JFactory::getDate($date);
         //$date = $d->toMySQL();
         //$$$rob set to $format as we expect $date to already be in $format
         $date = $d->toFormat($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 = FabrikWorker::_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 = FabrikWorker::_monthToInt($date, $month == '%B' ? false : true);
         }
     }
     //@TODO: some of these arent right for strftime
     $this->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 - eg 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));
         $this->finalformat = $format;
         return FabrikWorker::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;
 }