- Using Text Within date()
- Automatically Localizing Dates
- Manually Localizing Dates
- Using the Current Date the U.S./U.K./European Way
- Formatting a Specific Date
- Validating a Date
- Calculating a Relative Date
- Creating a Sortable Time Stamp
- Converting a String into a Date
- Determining Sunrise and Sunset
- Using Date and Time for Benchmarks
- Using Form Fields for Date Selection
- Create Self-updating Form Fields for Date Selection
- Calculating the Difference Between Two Dates
- Using GMT Date/Time Information
Automatically Localizing Dates
setlocale(LC_TIME, ‘en_US‘); echo strftime(‘In (American) English: %c<br />‘);
The PHP function strftime() formats a date/time value according to the sytem’s locale, for example, to the web server’s local settings. Generally, the language of the system is automatically used. However, this can be overridden using setlocale().
<?php setlocale(LC_TIME, ‘en_US‘); echo strftime(‘In (American) English: %c<br />‘); setlocale(LC_TIME, ‘en_gb‘); echo strftime(‘In (British) English: %c<br />‘); setlocale(LC_TIME, ‘de_DE‘); echo strftime(‘Auf Deutsch: %c<br />‘); setlocale(LC_TIME, ‘fr_FR‘); echo strftime(‘En Français: %c‘); ?>
Localizing Dates Using strftime() (strftime.php)
The function strftime() expects a format string (as does date()) in which it accepts a large number of special symbols. Table 3.2 contains a full list.
Table 3.2 Formatting Symbols for strftime()
Symbol |
Description |
%a |
Day of week (abbreviated) |
%A |
Day of week |
%b or %h |
Month (abbreviated) |
%B |
Month |
%c |
Date and time in standard format |
%C |
Century |
%d |
Day of month (from 01 to 31) |
%D |
Date in abbreviated format (mm/dd/yy) |
%e |
Day of month as a two-character string (from ‘ 1’ to ‘31’) |
%g |
Year according to the week number,two digits |
%G |
Year according to the week number,four digits |
%H |
Hour (from 00 to 23) |
%I |
Hour (from 01 to 12) |
%j |
Day of year (from 001 to 366) |
%m |
Month (from 01 to 12) |
%M |
Minute (from 00 to 59) |
%n |
Newline (\n) |
%p |
am or pm (or local equivalent) |
%r |
Time using a.m./p.m. notation |
%R |
Time using 24 hours notation |
%S |
Second (from 00 to 59) |
%t |
Tab (\t) |
%T |
Time in hh:ss:mm format |
%u |
Day of week (from 1—Monday—to 7—Sunday) |
%U |
Week number (Rule: The first Sunday is the first day of the first week.) |
%V |
Week number (Rule: The first week in the year with at least four days counts as week number 1.) |
%w |
Day of week (from 0—Sunday to 6—Saturday) |
%W |
Week number (Rule: The first Monday is the first day of the first week.) |
%x |
Date in standard format (without the time) |
%X |
Time in standard format (without the date) |
%y |
Year (two digits) |
%Y |
Year (four digits) |
%z or %Z |
Time zone |
Whenever it says standard format in Table 3.2, the formatting symbol gets replaced by the associated value according to the local setting. The preceding code changes the locale several times using setlocale() and then calls strftime(). Note the differences that can be seen in Figure 3.1. Also take a look at Figure 3.2, in which the same script was executed on a Windows machine. According to the documentation, most of strftime() also works on Windows, but on some configurations changing the locale just does not seem to work. Therefore, it is very important to test first whether the system supports localized dates.
Figure 3.1 The current date in different locales.
Figure 3.2 This particular system does not seem to support locales.