You are here

function date_calc_format in Date 6.2

Same name and namespace in other branches
  1. 5.2 date_php4/date_php4_calc.inc \date_calc_format()
  2. 6 date_php4/date_php4_calc.inc \date_calc_format()

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 date_calc_days_to_date() 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>

Parameters

int $day: The day of the month.

int $month: The month.

int $year: The 4 digit year. Do not add leading 0's for years prior to 1000.

string $format: The format string.

Return value

string The date in the desired format.

4 calls to date_calc_format()
date_calc_begin_of_month_by_span in date_php4/date_php4_calc.inc
Returns date of the first day of the month in the number of months from the given date
date_calc_days_to_date in date_php4/date_php4_calc.inc
Converts number of days to a distant unspecified epoch
date_calc_end_of_month_by_span in date_php4/date_php4_calc.inc
Returns date of the last day of the month in the number of months from the given date.
date_calc_n_weekday_of_month in date_php4/date_php4_calc.inc
Calculates the date of the Nth weekday of the month, such as the second Saturday of January 2000

File

date_php4/date_php4_calc.inc, line 87

Code

function date_calc_format($day, $month, $year, $format = DATE_CALC_FORMAT) {
  if (!date_calc_is_valid($day, $month, $year)) {
    $year = date_calc_date_now('%Y');
    $month = date_calc_date_now('%m');
    $day = date_calc_date_now('%d');
  }
  $output = '';
  for ($strpos = 0; $strpos < strlen($format); $strpos++) {
    $char = drupal_substr($format, $strpos, 1);
    if ($char == '%') {
      $nextchar = drupal_substr($format, $strpos + 1, 1);
      switch ($nextchar) {
        case 'a':
          $output .= date_calc_get_weekday_abbrname($day, $month, $year);
          break;
        case 'A':
          $output .= date_calc_get_weekday_fullname($day, $month, $year);
          break;
        case 'b':
          $output .= date_calc_get_month_abbrname($month);
          break;
        case 'B':
          $output .= date_calc_get_month_fullname($month);
          break;
        case 'd':
          $output .= date_pad($day);
          break;
        case 'e':
          $output .= $day;
          break;
        case 'E':
          $output .= date_calc_date_to_days($day, $month, $year);
          break;
        case 'j':
          $output .= date_calc_julian_date($day, $month, $year);
          break;
        case 'm':
          $output .= date_pad($month);
          break;
        case 'n':
          $output .= "\n";
          break;
        case 't':
          $output .= "\t";
          break;
        case 'w':
          $output .= date_dow($day, $month, $year);
          break;
        case 'U':
          $output .= date_calc_week_of_year($day, $month, $year);
          break;
        case 'y':
          $output .= drupal_substr(date_pad($year, 4), 2, 2);
          break;
        case 'Y':
          $output .= date_pad($year, 4);
          break;
        case '%':
          $output .= '%';
          break;
        default:
          $output .= $char . $nextchar;
      }
      $strpos++;
    }
    else {
      $output .= $char;
    }
  }
  return $output;
}