示例#1
0
文件: Calc.php 项目: ulrikkold/cal
 /**
  * Formats the date in the given format, much like strfmt()
  *
  * This function is used to alleviate the problem with 32-bit numbers for
  * dates pre 1970 or post 2038, as strfmt() has on most systems.
  * Most of the formatting options are compatible.
  *
  * Formatting options:
  * <pre>
  * %a abbreviated weekday name (Sun, Mon, Tue)
  * %A full weekday name (Sunday, Monday, Tuesday)
  * %b abbreviated month name (Jan, Feb, Mar)
  * %B full month name (January, February, March)
  * %d day of month (range 00 to 31)
  * %e day of month, single digit (range 0 to 31)
  * %E number of days since unspecified epoch (integer)
  * (%E is useful for passing a date in a URL as
  * an integer value. Then simply use
  * daysToDate() to convert back to a date.)
  * %j day of year (range 001 to 366)
  * %m month as decimal number (range 1 to 12)
  * %n newline character (\n)
  * %t tab character (\t)
  * %w weekday as decimal (0 = Sunday)
  * %U week number of current year, first sunday as first week
  * %y year as decimal (range 00 to 99)
  * %Y year as decimal including century (range 0000 to 9999)
  * %% literal '%'
  * </pre>
  *
  * @param int $day
  *        	the day of the month
  * @param int $month
  *        	the month
  * @param int $year
  *        	the year. Use the complete year instead of the
  *        	abbreviated version. E.g. use 2005, not 05.
  *        	Do not add leading 0's for years prior to 1000.
  * @param string $format
  *        	the format string
  *        	
  * @return string the date in the desired format
  *        
  * @access public
  * @static
  *
  */
 static function dateFormat($day, $month, $year, $format)
 {
     if (!Calc::isValidDate($day, $month, $year)) {
         $year = Calc::dateNow('%Y');
         $month = Calc::dateNow('%m');
         $day = Calc::dateNow('%d');
     }
     $output = '';
     for ($strpos = 0; $strpos < strlen($format); $strpos++) {
         $char = substr($format, $strpos, 1);
         if ($char == '%') {
             $nextchar = substr($format, $strpos + 1, 1);
             switch ($nextchar) {
                 case 'a':
                     $output .= Calc::getWeekdayAbbrname($day, $month, $year);
                     break;
                 case 'A':
                     $output .= Calc::getWeekdayFullname($day, $month, $year);
                     break;
                 case 'b':
                     $output .= Calc::getMonthAbbrname($month);
                     break;
                 case 'B':
                     $output .= Calc::getMonthFullname($month);
                     break;
                 case 'd':
                     $output .= sprintf('%02d', $day);
                     break;
                 case 'e':
                     $output .= $day;
                     break;
                 case 'E':
                     $output .= Calc::dateToDays($day, $month, $year);
                     break;
                 case 'j':
                     $output .= Calc::julianDate($day, $month, $year);
                     break;
                 case 'm':
                     $output .= sprintf('%02d', $month);
                     break;
                 case 'n':
                     $output .= "\n";
                     break;
                 case 't':
                     $output .= "\t";
                     break;
                 case 'w':
                     $output .= Calc::dayOfWeek($day, $month, $year);
                     break;
                 case 'U':
                     $output .= Calc::weekOfYear($day, $month, $year);
                     break;
                 case 'y':
                     $output .= substr($year, 2, 2);
                     break;
                 case 'Y':
                     $output .= $year;
                     break;
                 case '%':
                     $output .= '%';
                     break;
                 default:
                     $output .= $char . $nextchar;
             }
             $strpos++;
         } else {
             $output .= $char;
         }
     }
     return $output;
 }