You are here

function _date_date in Date 5.2

Same name and namespace in other branches
  1. 6.2 date_php4/date_php4_lib.inc \_date_date()
  2. 6 date_php4/date_php4_lib.inc \_date_date()

Low level function to create date() for pre-1970 and post-2038 dates.

Parameters

$format a format string for the result:

$timestamp a unix timestamp:

$timezone name: Use 'UTC' to avoid timezone conversion.

1 call to _date_date()
date_date in date_php4/date_php4.inc
Return formatted date based on timestamp $timestamp.

File

date_php4/date_php4_lib.inc, line 223

Code

function _date_date($format, $timestamp = false, $timezone = false) {
  if ($timezone === FALSE) {
    $timezone = date_default_timezone_name();
  }
  $_day_power = 86400;
  $arr = _date_getdate($timestamp, $timezone);
  $year = $arr['year'];
  $month = $arr['month'];
  $day = $arr['day'];
  $hour = $arr['hour'];
  $min = $arr['minute'];
  $secs = $arr['second'];
  $max = strlen($format);
  $dates = '';

  /*
    at this point, we have the following integer vars to manipulate:
    $year, $month, $day, $hour, $min, $secs
  */
  for ($i = 0; $i < $max; $i++) {
    switch ($format[$i]) {
      case 'T':
        $dates .= date('T');
        break;

      // YEAR
      case 'L':
        $dates .= $arr['leap'] ? '1' : '0';
        break;
      case 'r':

        // Thu, 21 Dec 2000 16:01:07 +0200
        // 4.3.11 uses '04 Jun 2004'
        // 4.3.8 uses  ' 4 Jun 2004'
        $dates .= gmdate('D', $_day_power * (3 + date_dow($day, $month, $year))) . ', ' . ($day < 10 ? '0' . $day : $day) . ' ' . date('M', mktime(0, 0, 0, $month, 2, 1971)) . ' ' . $year . ' ';
        if ($hour < 10) {
          $dates .= '0' . $hour;
        }
        else {
          $dates .= $hour;
        }
        if ($min < 10) {
          $dates .= ':0' . $min;
        }
        else {
          $dates .= ':' . $min;
        }
        if ($secs < 10) {
          $dates .= ':0' . $secs;
        }
        else {
          $dates .= ':' . $secs;
        }
        $gmt = date_get_gmt_diff_ts($timestamp, $timezone);
        $dates .= sprintf(' %s%04d', $gmt >= 0 ? '+' : '-', abs($gmt) / 36);
        break;
      case 'Y':
        $dates .= date_pad($year, 4);
        break;
      case 'y':
        $dates .= substr($year, strlen($year) - 2, 2);
        break;

      // MONTH
      case 'm':
        if ($month < 10) {
          $dates .= '0' . $month;
        }
        else {
          $dates .= $month;
        }
        break;
      case 'Q':
        $dates .= $month + 3 >> 2;
        break;
      case 'n':
        $dates .= $month;
        break;
      case 'M':
        $dates .= date('M', mktime(0, 0, 0, $month, 2, 1971));
        break;
      case 'F':
        $dates .= date('F', mktime(0, 0, 0, $month, 2, 1971));
        break;

      // DAY
      case 't':
        $dates .= $arr['ndays'];
        break;
      case 'z':
        $dates .= $arr['yday'];
        break;
      case 'w':
        $dates .= date_dow($day, $month, $year);
        break;
      case 'l':
        $dates .= gmdate('l', $_day_power * (3 + date_dow($day, $month, $year)));
        break;
      case 'D':
        $dates .= gmdate('D', $_day_power * (3 + date_dow($day, $month, $year)));
        break;
      case 'j':
        $dates .= $day;
        break;
      case 'd':
        if ($day < 10) {
          $dates .= '0' . $day;
        }
        else {
          $dates .= $day;
        }
        break;
      case 'S':
        $d10 = $day % 10;
        if ($d10 == 1) {
          $dates .= 'st';
        }
        else {
          if ($d10 == 2 && $day != 12) {
            $dates .= 'nd';
          }
          else {
            if ($d10 == 3) {
              $dates .= 'rd';
            }
            else {
              $dates .= 'th';
            }
          }
        }
        break;

      // HOUR
      case 'Z':
        $dates .= -date_get_gmt_diff_ts($timestamp, $timezone);
        break;
      case 'O':
        $gmt = date_get_gmt_diff_ts($timestamp, $timezone);
        $dates .= sprintf('%s%04d', $gmt < 0 ? '+' : '-', abs($gmt) / 36);
        break;
      case 'H':
        if ($hour < 10) {
          $dates .= '0' . $hour;
        }
        else {
          $dates .= $hour;
        }
        break;
      case 'h':
        if ($hour > 12) {
          $hh = $hour - 12;
        }
        else {
          if ($hour == 0) {
            $hh = '12';
          }
          else {
            $hh = $hour;
          }
        }
        if ($hh < 10) {
          $dates .= '0' . $hh;
        }
        else {
          $dates .= $hh;
        }
        break;
      case 'G':
        $dates .= $hour;
        break;
      case 'g':
        if ($hour > 12) {
          $hh = $hour - 12;
        }
        else {
          if ($hour == 0) {
            $hh = '12';
          }
          else {
            $hh = $hour;
          }
        }
        $dates .= $hh;
        break;

      // MINUTES
      case 'i':
        if ($min < 10) {
          $dates .= '0' . $min;
        }
        else {
          $dates .= $min;
        }
        break;

      // SECONDS
      case 'U':
        $dates .= $timestamp;
        break;
      case 's':
        if ($secs < 10) {
          $dates .= '0' . $secs;
        }
        else {
          $dates .= $secs;
        }
        break;

      // AM/PM
      // Note 00:00 to 11:59 is AM, while 12:00 to 23:59 is PM
      case 'a':
        if ($hour >= 12) {
          $dates .= 'pm';
        }
        else {
          $dates .= 'am';
        }
        break;
      case 'A':
        if ($hour >= 12) {
          $dates .= 'PM';
        }
        else {
          $dates .= 'AM';
        }
        break;
      default:
        $dates .= $format[$i];
        break;

      // ESCAPE
      case "\\":
        $i++;
        if ($i < $max) {
          $dates .= $format[$i];
        }
        break;
    }
  }
  return $dates;
}