1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 297028 $ -->
<refentry xml:id="function.date" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>date</refname>
<refpurpose>Format a local time/date</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>string</type><methodname>date</methodname>
<methodparam><type>string</type><parameter>format</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>timestamp</parameter></methodparam>
</methodsynopsis>
<para>
Returns a string formatted according to the given format string using the
given integer <parameter>timestamp</parameter> or the current time
if no timestamp is given. In other words, <parameter>timestamp</parameter>
is optional and defaults to the value of <function>time</function>.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>format</parameter></term>
<listitem>
<para>
The format of the outputted date <type>string</type>. See the formatting
options below. There are also several
<link linkend="datetime.constants.types">predefined date constants</link>
that may be used instead, so for example <constant>DATE_RSS</constant>
contains the format string <literal>'D, d M Y H:i:s'</literal>.
</para>
<para>
<table>
<title>The following characters are recognized in the
<parameter>format</parameter> parameter string</title>
<tgroup cols="3">
<thead>
<row>
<entry><parameter>format</parameter> character</entry>
<entry>Description</entry>
<entry>Example returned values</entry>
</row>
</thead>
<tbody>
<row>
<entry align="center"><emphasis>Day</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry><literal>d</literal></entry>
<entry>Day of the month, 2 digits with leading zeros</entry>
<entry><literal>01</literal> to <literal>31</literal></entry>
</row>
<row>
<entry><literal>D</literal></entry>
<entry>A textual representation of a day, three letters</entry>
<entry><literal>Mon</literal> through <literal>Sun</literal></entry>
</row>
<row>
<entry><literal>j</literal></entry>
<entry>Day of the month without leading zeros</entry>
<entry><literal>1</literal> to <literal>31</literal></entry>
</row>
<row>
<entry><literal>l</literal> (lowercase 'L')</entry>
<entry>A full textual representation of the day of the week</entry>
<entry><literal>Sunday</literal> through <literal>Saturday</literal></entry>
</row>
<row>
<entry><literal>N</literal></entry>
<entry>ISO-8601 numeric representation of the day of the week (added in
PHP 5.1.0)</entry>
<entry><literal>1</literal> (for Monday) through <literal>7</literal> (for Sunday)</entry>
</row>
<row>
<entry><literal>S</literal></entry>
<entry>English ordinal suffix for the day of the month, 2 characters</entry>
<entry>
<literal>st</literal>, <literal>nd</literal>, <literal>rd</literal> or
<literal>th</literal>. Works well with <literal>j</literal>
</entry>
</row>
<row>
<entry><literal>w</literal></entry>
<entry>Numeric representation of the day of the week</entry>
<entry><literal>0</literal> (for Sunday) through <literal>6</literal> (for Saturday)</entry>
</row>
<row>
<entry><literal>z</literal></entry>
<entry>The day of the year (starting from 0)</entry>
<entry><literal>0</literal> through <literal>365</literal></entry>
</row>
<row>
<entry align="center"><emphasis>Week</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry><literal>W</literal></entry>
<entry>ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)</entry>
<entry>Example: <literal>42</literal> (the 42nd week in the year)</entry>
</row>
<row>
<entry align="center"><emphasis>Month</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry><literal>F</literal></entry>
<entry>A full textual representation of a month, such as January or March</entry>
<entry><literal>January</literal> through <literal>December</literal></entry>
</row>
<row>
<entry><literal>m</literal></entry>
<entry>Numeric representation of a month, with leading zeros</entry>
<entry><literal>01</literal> through <literal>12</literal></entry>
</row>
<row>
<entry><literal>M</literal></entry>
<entry>A short textual representation of a month, three letters</entry>
<entry><literal>Jan</literal> through <literal>Dec</literal></entry>
</row>
<row>
<entry><literal>n</literal></entry>
<entry>Numeric representation of a month, without leading zeros</entry>
<entry><literal>1</literal> through <literal>12</literal></entry>
</row>
<row>
<entry><literal>t</literal></entry>
<entry>Number of days in the given month</entry>
<entry><literal>28</literal> through <literal>31</literal></entry>
</row>
<row>
<entry align="center"><emphasis>Year</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry><literal>L</literal></entry>
<entry>Whether it's a leap year</entry>
<entry><literal>1</literal> if it is a leap year, <literal>0</literal> otherwise.</entry>
</row>
<row>
<entry><literal>o</literal></entry>
<entry>ISO-8601 year number. This has the same value as
<literal>Y</literal>, except that if the ISO week number
(<literal>W</literal>) belongs to the previous or next year, that year
is used instead. (added in PHP 5.1.0)</entry>
<entry>Examples: <literal>1999</literal> or <literal>2003</literal></entry>
</row>
<row>
<entry><literal>Y</literal></entry>
<entry>A full numeric representation of a year, 4 digits</entry>
<entry>Examples: <literal>1999</literal> or <literal>2003</literal></entry>
</row>
<row>
<entry><literal>y</literal></entry>
<entry>A two digit representation of a year</entry>
<entry>Examples: <literal>99</literal> or <literal>03</literal></entry>
</row>
<row>
<entry align="center"><emphasis>Time</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry><literal>a</literal></entry>
<entry>Lowercase Ante meridiem and Post meridiem</entry>
<entry><literal>am</literal> or <literal>pm</literal></entry>
</row>
<row>
<entry><literal>A</literal></entry>
<entry>Uppercase Ante meridiem and Post meridiem</entry>
<entry><literal>AM</literal> or <literal>PM</literal></entry>
</row>
<row>
<entry><literal>B</literal></entry>
<entry>Swatch Internet time</entry>
<entry><literal>000</literal> through <literal>999</literal></entry>
</row>
<row>
<entry><literal>g</literal></entry>
<entry>12-hour format of an hour without leading zeros</entry>
<entry><literal>1</literal> through <literal>12</literal></entry>
</row>
<row>
<entry><literal>G</literal></entry>
<entry>24-hour format of an hour without leading zeros</entry>
<entry><literal>0</literal> through <literal>23</literal></entry>
</row>
<row>
<entry><literal>h</literal></entry>
<entry>12-hour format of an hour with leading zeros</entry>
<entry><literal>01</literal> through <literal>12</literal></entry>
</row>
<row>
<entry><literal>H</literal></entry>
<entry>24-hour format of an hour with leading zeros</entry>
<entry><literal>00</literal> through <literal>23</literal></entry>
</row>
<row>
<entry><literal>i</literal></entry>
<entry>Minutes with leading zeros</entry>
<entry><literal>00</literal> to <literal>59</literal></entry>
</row>
<row>
<entry><literal>s</literal></entry>
<entry>Seconds, with leading zeros</entry>
<entry><literal>00</literal> through <literal>59</literal></entry>
</row>
<row>
<entry><literal>u</literal></entry>
<entry>Microseconds (added in PHP 5.2.2)</entry>
<entry>Example: <literal>654321</literal></entry>
</row>
<row>
<entry align="center"><emphasis>Timezone</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry><literal>e</literal></entry>
<entry>Timezone identifier (added in PHP 5.1.0)</entry>
<entry>Examples: <literal>UTC</literal>, <literal>GMT</literal>, <literal>Atlantic/Azores</literal></entry>
</row>
<row>
<entry><literal>I</literal> (capital i)</entry>
<entry>Whether or not the date is in daylight saving time</entry>
<entry><literal>1</literal> if Daylight Saving Time, <literal>0</literal> otherwise.</entry>
</row>
<row>
<entry><literal>O</literal></entry>
<entry>Difference to Greenwich time (GMT) in hours</entry>
<entry>Example: <literal>+0200</literal></entry>
</row>
<row>
<entry><literal>P</literal></entry>
<entry>Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)</entry>
<entry>Example: <literal>+02:00</literal></entry>
</row>
<row>
<entry><literal>T</literal></entry>
<entry>Timezone abbreviation</entry>
<entry>Examples: <literal>EST</literal>, <literal>MDT</literal> ...</entry>
</row>
<row>
<entry><literal>Z</literal></entry>
<entry>Timezone offset in seconds. The offset for timezones west of UTC is always
negative, and for those east of UTC is always positive.</entry>
<entry><literal>-43200</literal> through <literal>50400</literal></entry>
</row>
<row>
<entry align="center"><emphasis>Full Date/Time</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry><literal>c</literal></entry>
<entry>ISO 8601 date (added in PHP 5)</entry>
<entry>2004-02-12T15:19:21+00:00</entry>
</row>
<row>
<entry><literal>r</literal></entry>
<entry><link xlink:href="&url.rfc;2822">RFC 2822</link> formatted date</entry>
<entry>Example: <literal>Thu, 21 Dec 2000 16:01:07 +0200</literal></entry>
</row>
<row>
<entry><literal>U</literal></entry>
<entry>Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)</entry>
<entry>See also <function>time</function></entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
Unrecognized characters in the format string will be printed
as-is. The <literal>Z</literal> format will always return
<literal>0</literal> when using <function>gmdate</function>.
</para>
<note>
<para>
Since this function only accepts <type>integer</type> timestamps the
<literal>u</literal> format character is only useful when using the
<function>date_format</function> function with user based timestamps
created with <function>date_create</function>.
</para>
</note>
</listitem>
</varlistentry>
&date.timestamp.description;
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns a formatted date string. If a non-numeric value is used for
<parameter>timestamp</parameter>, &false; is returned and an
<constant>E_WARNING</constant> level error is emitted.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
&date.timezone.errors.description;
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>5.1.0</entry>
<entry>
The valid range of a timestamp is typically from Fri, 13 Dec
1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are
the dates that correspond to the minimum and maximum values for
a 32-bit signed integer). However, before PHP 5.1.0 this range was limited
from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).
</entry>
</row>
&date.timezone.errors.changelog;
<row>
<entry>5.1.1</entry>
<entry>
There are useful <link linkend="datetime.constants">constants</link>
of standard date/time formats that can be used to specify the
<parameter>format</parameter> parameter.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>date</function> examples</title>
<programlisting role="php">
<![CDATA[
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// Prints something like: Monday
echo date("l");
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* use the constants in the format parameter */
// prints something like: Mon, 15 Aug 2005 15:12:46 UTC
echo date(DATE_RFC822);
// prints something like: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>
]]>
</programlisting>
</example>
</para>
<para>
You can prevent a recognized character in the format string from being
expanded by escaping it with a preceding backslash. If the character with
a backslash is already a special sequence, you may need to also escape
the backslash.
<example>
<title>Escaping characters in <function>date</function></title>
<programlisting role="php">
<![CDATA[
<?php
// prints something like: Wednesday the 15th
echo date("l \\t\h\e jS");
?>
]]>
</programlisting>
</example>
</para>
<para>
It is possible to use <function>date</function> and
<function>mktime</function> together to find dates in the future
or the past.
<example>
<title><function>date</function> and <function>mktime</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>
]]>
</programlisting>
</example>
<note>
<para>
This can be more reliable than simply adding or subtracting the number
of seconds in a day or month to a timestamp because of daylight saving
time.
</para>
</note>
</para>
<para>
Some examples of <function>date</function> formatting. Note that
you should escape any other characters, as any which currently
have a special meaning will produce undesirable results, and
other characters may be assigned meaning in future PHP versions.
When escaping, be sure to use single quotes to prevent characters
like \n from becoming newlines.
<example>
<title><function>date</function> Formatting</title>
<programlisting role="php">
<![CDATA[
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
?>
]]>
</programlisting>
</example>
</para>
<para>
To format dates in other languages, you should use the
<function>setlocale</function> and <function>strftime</function>
functions instead of <function>date</function>.
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<para>
To generate a timestamp from a string representation of the date, you
may be able to use <function>strtotime</function>. Additionally, some
databases have functions to convert their date formats into timestamps
(such as MySQL's <link xlink:href="&url.mysql.docs.date;">UNIX_TIMESTAMP</link>
function).
</para>
</note>
<tip>
<para>
Timestamp of the start of the request is available in
<varname>$_SERVER['REQUEST_TIME']</varname> since PHP 5.1.
</para>
</tip>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>gmdate</function></member>
<member><function>idate</function></member>
<member><function>getdate</function></member>
<member><function>getlastmod</function></member>
<member><function>mktime</function></member>
<member><function>strftime</function></member>
<member><function>time</function></member>
<member><link linkend="datetime.constants.types">Predefined DateTime Constants</link></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|