Core PHP

Formatting Times and Dates

The date() function provides various options for fomatting the display of times and dates. In this example, we print out the members of the date object in the first section. In the second section, we display all of the various available options for formatting times and dates. In the third sect, all of the available standard time formats are shown. In the fourth section, a few examples of date and time formatting are shown.

DateTimeFormatting.php

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
<?php
  // Set the Timezone
  date_default_timezone_set('America/Chicago');
  // Get the date object
  $qaDate = getdate();

  echo"<hr />".PHP_EOL;
  echo "<h3>Date Members</h3>".PHP_EOL;
  echo "<ul>".PHP_EOL;
  foreach($qaDate as $sKey => $sValue) {
    echo "  <li> &dollar;qaDate[&apos;".$sKey."&apos;] = ".$sValue."</li>".PHP_EOL;
  }
  echo "</ul>".PHP_EOL;

  echo"<hr />".PHP_EOL;
  echo "<h3>Format Options</h3>".PHP_EOL;
  echo "<ul>".PHP_EOL;
  echo "  <li> date(<q>a</q>) = ".date("a")." - Lowercase am or pm</li>".PHP_EOL;
  echo "  <li> date(<q>A</q>) = ".date("A")." - Uppercase AM or PM</li>".PHP_EOL;
  echo "  <li> date(<q>B</q>) = ".date("B")." - Swatch internet time (000 to 999)</li>".PHP_EOL;
  echo "  <li> date(<q>c</q>) = ".date("c")." - The ISO-8601 date (e.g. 2022-10-29T16:42:24-05:00)</li>".PHP_EOL;
  echo "  <li> date(<q>d</q>) = ".date("d")." - The day of the month (01 to 31)</li>".PHP_EOL;
  echo "  <li> date(<q>D</q>) = ".date("D")." - Thrree letter representation of the day of the week (e.g. Sat)</li>".PHP_EOL;
  echo "  <li> date(<q>e</q>) = ".date("e")." - The timezone identifier (e.g. America/Chicago)</li>".PHP_EOL;
  echo "  <li> date(<q>F</q>) = ".date("F")." - The name of the month (e.g. October)</li>".PHP_EOL;
  echo "  <li> date(<q>g</q>) = ".date("g")." - 12-hour format of an hour (1 to 12)</li>".PHP_EOL;
  echo "  <li> date(<q>G</q>) = ".date("G")." - 24-hour format of an hour (0 to 23)</li>".PHP_EOL;
  echo "  <li> date(<q>h</q>) = ".date("h")." - 12-hour format of an hour (01 to 12)</li>".PHP_EOL;
  echo "  <li> date(<q>H</q>) = ".date("H")." - 24-hour format of an hour (00 to 23)</li>".PHP_EOL;
  echo "  <li> date(<q>i</q>) = ".date("i")." - Minutes with leading zeros (00 to 59)</li>".PHP_EOL;
  echo "  <li> date(<q>I</q>) = ".date("I")." - Daylights Savings Time (1 = Daylight Savings Time, 0 = Standard Time)</li>".PHP_EOL;
  echo "  <li> date(<q>j</q>) = ".date("j")." - The day of the month without leading zeros (1 to 31)</li>".PHP_EOL;
  echo "  <li> date(<q>l</q>) = ".date("l")." - The name of the of a day of the week</li>".PHP_EOL;
  echo "  <li> date(<q>L</q>) = ".date("L")." - Leap year (1 if it is a leap year, 0 otherwise)</li>".PHP_EOL;
  echo "  <li> date(<q>m</q>) = ".date("m")." - Two digit month number with a leading zero (from 01 to 12)</li>".PHP_EOL;
  echo "  <li> date(<q>M</q>) = ".date("M")." - Abbreviation of the month name (three letters)</li>".PHP_EOL;
  echo "  <li> date(<q>n</q>) = ".date("n")." - A numeric representation of a month, without leading zeros (1 to 12)</li>".PHP_EOL;
  echo "  <li> date(<q>N</q>) = ".date("N")." - The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)</li>".PHP_EOL;
  echo "  <li> date(<q>o</q>) = ".date("o")." - The ISO-8601 year number</li>".PHP_EOL;
  echo "  <li> date(<q>O</q>) = ".date("O")." - Difference to Greenwich time (GMT) in hours (Example: +0100)</li>".PHP_EOL;
  echo "  <li> date(<q>p</q>) = ".date("p")." - Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)</li>".PHP_EOL;
  echo "  <li> date(<q>P</q>) = ".date("P")." - Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)</li>".PHP_EOL;
  echo "  <li> date(<q>o</q>) = ".date("o")." - The ISO-8601 year number</li>".PHP_EOL;
  echo "  <li> date(<q>O</q>) = ".date("O")." - Difference to Greenwich time (GMT) in hours (Example: +0100)</li>".PHP_EOL;
  echo "  <li> date(<q>r</q>) = ".date("r")." - The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)</li>".PHP_EOL;
  echo "  <li> date(<q>s</q>) = ".date("s")." - Seconds, with leading zeros (00 to 59)</li>".PHP_EOL;
  echo "  <li> date(<q>S</q>) = ".date("S")." - The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)</li>".PHP_EOL;
  echo "  <li> date(<q>t</q>) = ".date("t")." - The number of days in the given month</li>".PHP_EOL;
  echo "  <li> date(<q>T</q>) = ".date("T")." - Timezone abbreviations (Examples: EST, MDT)</li>".PHP_EOL;
  echo "  <li> date(<q>u</q>) = ".date("u")." - Microseconds (added in PHP 5.2.2)</li>".PHP_EOL;
  echo "  <li> date(<q>U</q>) = ".date("U")." - The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)</li>".PHP_EOL;
  echo "  <li> date(<q>v</q>) = ".date("v")." ???</li>".PHP_EOL;
  echo "  <li> date(<q>w</q>) = ".date("w")." - A numeric representation of the day (0 for Sunday, 6 for Saturday)</li>".PHP_EOL;
  echo "  <li> date(<q>W</q>) = ".date("W")." - The ISO-8601 week number of year (weeks starting on Monday)</li>".PHP_EOL;
  echo "  <li> date(<q>y</q>) = ".date("y")." - A two digit representation of a year</li>".PHP_EOL;
  echo "  <li> date(<q>Y</q>) = ".date("Y")." - A four digit representation of a year</li>".PHP_EOL;
  echo "  <li> date(<q>z</q>) = ".date("z")." - The day of the year (from 0 through 365)</li>".PHP_EOL;
  echo "  <li> date(<q>Z</q>) = ".date("Z")." - Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)</li>".PHP_EOL;
  echo "</ul>".PHP_EOL;

  echo"<hr />".PHP_EOL;
  echo "<h3>Standard Formats</h3>".PHP_EOL;
  echo "<ul>".PHP_EOL;
  echo "  <li> date(DATE_ATOM) = ".date(DATE_ATOM)." - Atom</li>".PHP_EOL;
  echo "  <li> date(DATE_COOKIE) = ".date(DATE_COOKIE)." - HTTP Cookies</li>".PHP_EOL;
  echo "  <li> date(DATE_ISO8601) = ".date(DATE_ISO8601)." - ISO-8601</li>".PHP_EOL;
  echo "  <li> date(DATE_RFC822) = ".date(DATE_RFC822)." - RFC 822</li>".PHP_EOL;
  echo "  <li> date(DATE_RFC850) = ".date(DATE_RFC850)." - RFC 850</li>".PHP_EOL;
  echo "  <li> date(DATE_RFC1036) = ".date(DATE_RFC1036)." - RFC 1036</li>".PHP_EOL;
  echo "  <li> date(DATE_RFC1123) = ".date(DATE_RFC1123)." - RFC 1123</li>".PHP_EOL;
  echo "  <li> date(DATE_RFC2822) = ".date(DATE_RFC2822)." - RFC 2822</li>".PHP_EOL;
  echo "  <li> date(DATE_RFC3339) = ".date(DATE_RFC3339)." - Same as DATE_ATOM</li>".PHP_EOL;
  echo "  <li> date(DATE_RSS) = ".date(DATE_RSS)." - RSS</li>".PHP_EOL;
  echo "  <li> date(DATE_W3C) = ".date(DATE_W3C)." - World Wide Web Consortium</li>".PHP_EOL;
  echo "</ul>".PHP_EOL;

  echo"<hr />".PHP_EOL;
  echo "<h3>Example Format Usages</h3>".PHP_EOL;
  echo "<ul>".PHP_EOL;
  // A example with several fields.
  echo "  <li> date(<q>l jS F Y g:i:s a</q>) = ".date("l jS F Y g:i:s a")."</li>".PHP_EOL;
  // An example with multiple parts concatenated
  echo "  <li> ".date("l")." the ".date("jS")." of ".date("F Y")." at ".date("g:i:s a")."</li>".PHP_EOL;
  // An example with a set time
  echo "  <li> date(<q>F jS, Y</q>, mktime(0,0,0,12,25,1054)) = ".date("F jS, Y", mktime(0,0,0,12,25,1054))."</li>".PHP_EOL;
  // An example of a standard format with a specified timestamp given by a date object
  echo "  <li> date(DATE_ATOM, &dollar;qaDate[0]) = ".date(DATE_ATOM, $qaDate[0])."</li>".PHP_EOL;
  echo "</ul>".PHP_EOL;
?>
  </body>
</html>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.