CHAPTER 9 – DATE HANDLING – Formatting Date and Time

Making a GMT date with gmmktime() and then showing it in the current time zone with the date() function doesn't make much sense. Thus, we also have two functions for formatting date/time: date() to format a local date/time, and gmdate() to format a GMT date/time. Both functions accept exactly the same parameters. The first parameter is a format string (more about that in a bit), and the second is an optional timestamp. If the timestamp parameter is not included, the current time is used in formatting the output. gmdate() and date() always format the date in English, not in the current "locale" that is set on your system. Two functions are provided to format local time/date according to locale settings: strftime() for local time and gmstrftime() for GMT times. Table 9.6 describes formatting string characters for both functions. Note that the (gm)strftime() prefix to the formatting string options with a %. Table 9.6 Date Formatting Modifiers date / strftime / Description gmdate gmstrftime Remarks AM/PM A am/pm a %p Either am or pm for the English locale. Other locales might have their replace- ments (for example, nl_NL has an empty string here). Century, numeric %C Returns the century number 20 for 2004, two digits and so on. Character, literal % %% Use this to place a literal character % inside the formatting string. Character, newline %n Use this to place a newline character inside the formatting string. Character, tab %t Use this to place a tab character inside the formatting string. Day count in month t Number of days in the month defined by the timestamp. Day of month, lead- %e Current day in this month defined by the ing spaces timestamp. A space is prepended when the day number is less than 10. Day of month, lead- d %D Current day in this month defined by the ing zeros timestamp. A zero is prepended when the day number is less than 10. Day of month, with- j Current day in this month defined by the out leading zeros timestamp. Table 9.6 Date Formatting Modifiers date / strftime / Description gmdate gmstrftime Remarks Day of week, full l %A For strftime(), the day is shown accord- textual ing to the names of the current locale. <?php setlocale(LC_ALL, 'C'); echo strftime('%A '); setlocale(LC_ALL, 'no_NO'); echo strftime('%A'); ?> shows Monday mandag Day of week, w %w The range is 0­6 with 0 being Sunday numeric and 6 being Saturday. (0 = Sunday) Day of week, %u The range is 1­7 with 1 being Monday numeric and 7 being Sunday. (1= Monday) Day of week, short D %a For the (gm)strftime() function, the textual name is shown according to the locale; for (gm)date() it is the normal three let- ter abbreviation: Sun, Sat, Wed, and so on. Day of year, %j The day number in a year, starting with numeric with lead- 001 for January 1 to 365 or 366. ing zeros Day of year, z The day number in a year, starting with numeric without 0 for January 1 to 364 or 365. leading zeros DST active I Returns 1 if DST is active and 0 if DST is not active for the given timestamp. Formatted, %D Gives the same result as using %d/%m/%y. %d/%m/%y Formatted, %T Gives the same result as using %H:%M:%S. %H:%M:%S Formatted, %R The time in 24-hour notation without in 24-hour notation seconds. <?php echo strftime("%Rn"); // shows 23:53 ?> Formatted, %r The time in 12-hour notation including in a.m./p.m. seconds. notation <?php echo strftime("%rn"); // shows 11:53:47 ?>

Table 9.6 Date Formatting Modifiers date / strftime / Description gmdate gmstrftime Remarks Formatted, locale %x The date in preferred locale format. preferred date <?php setlocale(LC_ALL, 'iw_IL'); echo strftime("%xn"); // shows 29/02/04 ?> Formatted, locale %c The date and time in preferred locale preferred date and format. time <?php setlocale(LC_ALL, 'nl_NL'); // shows zo 29 feb 2004 23:56:12 CET echo strftime("%cn"); ?> Formatted, locale %X The date in preferred locale format. preferred time <?php setlocale(LC_ALL, 'nl_NL'); echo strftime("%xn"); // shows 29-02-04 ?> Hour, h %I 12-hour format, leading zeros Hour, g 12-hour format, no leading zeros Hour, H %H 24-hour format, leading zeros Hour, G 24-hour format, no leading zeros Internet time B The swatch Internet time in which a day is divided into 1,000 units: <?php echo date('B'). "n"; // shows 005 ?> ISO 8601 c Shows the date in ISO 8601 format: 2004-03-01T00:08:37+01:00 Leap year L Returns 1 if the year represented by the timestamp is a leap year, or 0 otherwise. Minutes, leading i %M zeros Table 9.6 Date Formatting Modifiers date / strftime / Description gmdate gmstrftime Remarks Month, F %B For (gm)strftime(), the month name is full textual the name in the language of the current locale. <?php setlocale(LC_ALL, 'iw_IL'); echo strftime("%Bn"); // shows

?> Month, numeric M %m with leading zeros Month, numeric N without leading zeros Month, M %b, %h short textual RFC 2822 R Returns a RFC 2822 (mail) formatted text (Mon, 1 Mar 2004 00:13:34 +0100). Seconds since U UNIX epoch Seconds, numeric s %S with leading zeros Suffix for day of S Returns an English ordinal suffix for use month, English with the j formatting option. ordinal <?php echo date("jSn"); // returns 1st ?> Time zone, numeric Z Returns the offset to GMT in seconds. (in seconds) For CET, this is 3600; for EST, this is ­18000, for example. Time zone, numeric O Returns a formatted offset to GMT. For formatted CET, this is +0100; for EST, this is -0500, for example. Time zone, textual T %Z Returns the current time zone name: CET, EST, and so on. Week number, ISO W %V In ISO 8601, week #1 is the first week in 8601 the year having four or more days. The range is 01 to 53, and you can use this in combination with %g or %G for the accom- panying year.

Table 9.6 Date Formatting Modifiers date / strftime / Description gmdate gmstrftime Remarks Week number, the %W <?php first Monday in a // shows 01 year is the start of echo strftime("%W", week 1 strtotime("2001-01- 01")),"n";// shows 53 echo strftime("%W", strtotime("2001-12- 31")),"n"; ?> Week number, the %U <?php first Sunday in a // shows 00 year is the start of echo strftime("%U", week 1 strtotime("2001-01- 01")),"n"; // shows 52 echo strftime("%U", strtotime("2001-12- 31")),"n"; ?> Year, numeric y %y two digits with leading zeroes Year, numeric %g This number might differ from the "real two digits; year year," as in ISO 8601; January 1 might component for %W still belong to week 53 of the year before. In that case, the year returned with this formatting option will be the one of the previous year, too. Year, numeric Y %Y four digits Year, numeric %G This number might differ from the "real four digits; year year," as in ISO 8601; January 1 might component for %W still belong to week 53 of the year before. In that case, the year returned with this formatting option will be the one of the previous year, too.

Post Comment
Login to post comments