Opened 11 months ago
Last modified 7 weeks ago
#20973 new defect (bug)
date_i18n() produces invalid output for shorthand formats
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Date/Time | Version: | 3.4 |
| Severity: | normal | Keywords: | |
| Cc: | info@…, stephen@…, contact@…, gabriel.koen@… |
Description
date_i18n() function relies on parsing passed format to make adjustments. However shorthand formats are not handled and produce invalid output.
Example:
var_dump( date_i18n( 'Y-m-d\TH:i:sP', time() ) ); // 2012-06-15T13:34:03+03:00 << ok var_dump( date_i18n( DATE_W3C, time() ) ); // 2012-06-15T13:34:03+03:00 << ok var_dump( date_i18n( 'c', time() ) ); // 2012-06-15T13:34:03+00:00 << broken time zone!
Hook-level fix:
add_filter( 'date_i18n', 'fix_c_time_format', 10, 3 );
function fix_c_time_format( $date, $format, $timestamp ) {
if ( 'c' == $format )
$date = date_i18n( DATE_W3C, $timestamp );
return $date;
}
See Why time functions show invalid time zone when using 'c' time format?
Possibly related (can't say for sure from description) #13538
Change History (5)
comment:3
stephenh1988 — 11 months ago
- Cc stephen@… added
- Cc contact@… added
Small correction - proper constant for c replacement is DATE_ISO8601, not DATE_W3C (different time zone formatting).
[update] Hm, nope DATE_W3C weirdly seems to be more consistent with c than DATE_ISO8601. While either time zone format is actually ok for standard.
comment:5
mintindeed — 7 weeks ago
- Cc gabriel.koen@… added
Note: See
TracTickets for help on using
tickets.

Actually scratch time() from example for more accurate: