Skip to content

The 7-Hour JSON-LD Timestamp Shift and the mysql2date Trap

Adityo Guni Waluyo

mysql2date parses its input as site-local time. Passing it the post_date_gmt column shifts the instant seven hours early on a UTC+07:00 site.

TL;DR

Passing post_date_gmt to mysql2date breaks WordPress timestamps because that function assumes local time, re-labeling the UTC clock with a +07:00 offset and shifting the instant seven hours early. Use get_post_datetime with the gmt source instead, which preserves the instant and just converts the offset label. Rule: pair gmt columns with instant-preserving functions, local columns with local ones.

The JSON-LD payload from a headless WordPress project showed datePublished at 03:30 with a +07:00 label. The article was actually published at 03:30 UTC, which should read 10:30 in Jakarta time. The displayed clock was the UTC clock; only the timezone label was local. The instant had slipped seven hours without a single error in the logs.

The first suspicion was, naturally, the data: maybe a time column in the database was wrong. A check of the wp_posts table showed the opposite. The local-time column and the GMT column both held values matching the publishing records. The bug lived in the PHP function formatting the global time column: mysql2date.

The Overlooked Input Contract of mysql2date

mysql2date has a firm contract: its input is expected to be site-local time in the MySQL Y-m-d H:i:s format. Its implementation creates the DateTime object directly with wp_timezone() as its zone, so every incoming string is parsed as local time [5].

The post_date_gmt column is not local time; it is absolute UTC. Handing it to mysql2date tells WordPress to treat the UTC clock as Jakarta time. The result is a string carrying the GMT clock wearing a +07:00 label. Once a consumer normalizes it to UTC, the instant lands seven hours earlier. For posts stored between 00:00 and 06:59 UTC, the shift crosses midnight, so the article reads as published on the previous calendar day.

Consumers Read the Instant, Not the Text

This is not a display bug. The datePublished property of schema.org expects a Date or DateTime, the "Date of first publication or broadcast", and crawlers read it as an absolute instant [6]. An offset label attached to the wrong clock produces genuinely wrong data, not a different notation. The chronological order between articles and the calendar date a search engine sees both shift with the incorrect value.

The Fix: Read the GMT Column, Format W3C

The right function has existed since WordPress 5.3: get_post_datetime. Its third argument selects the time source, local or gmt [4]. With the gmt source, the function reads the UTC column into an explicit UTC zone, then converts the object to the site timezone via setTimezone. The instant stays identical; only the offset label changes [4].

// Wrong: the GMT clock re-labeled as site-local time.
'datePublished' => mysql2date( DATE_W3C, $post->post_date_gmt ),

// Correct: the instant is preserved, the offset label follows the site timezone.
'datePublished' => get_post_datetime( $post, 'date', 'gmt' )->format( DATE_W3C ),
'dateModified'  => get_post_datetime( $post, 'modified', 'gmt' )->format( DATE_W3C ),
ApproachOutput for a 03:30 GMT inputMeaning
mysql2date2026-12-05T03:30:00+07:00The instant lands 7 hours early (2026-12-04T20:30Z)
get_post_datetime gmt2026-12-05T10:30:00+07:00The instant is preserved

The fix was verified live in an environment with the Asia/Jakarta timezone: a post_date_gmt input of 2026-12-05 03:30:00 produced datePublished 2026-12-05T10:30:00+07:00 at the endpoint, exactly the same instant. The test environment was restored to its original state afterwards.

DATE_W3C itself carries the offset in ISO 8601 notation, so a parser on the frontend or crawler side just normalizes it without assuming any particular timezone. While the server-side instant is correct, the +07:00 label is mere annotation; once the instant is wrong, the same label masks the error because the format still looks valid.

This bug class does not hide in JSON-LD alone. Every formatting pass that touches the post_date_gmt column, from feeds to sitemaps to custom API responses, can meet the same fate while it flows through a function contracted for local time. The prevention rule is one sentence: if the consumer reads an instant, read the *_gmt column through a function that preserves the instant; if the consumer reads the site wall clock, the local column with a local function is the right pair.

There is one legacy note about mysql2date: the 'U' and 'G' formats return a timestamp summed with the timezone offset, and the documentation itself says that pattern should ideally never be used [5]. For machine-readable output, the DateTime object from the core function is the safer path.

Two clock columns in wp_posts feel redundant until a function receives the column that fights its contract. post_date stores the site wall clock; post_date_gmt stores the absolute instant. WordPress date functions do not guess which one they were given; they trust the contract. Choosing the matching column, then formatting with a function that preserves the instant, kills this problem at the root.

Sources

  1. WordPress Developer Resources, get_post_datetime(): developer.wordpress.org/reference/functions/get_post_datetime
  2. WordPress Developer Resources, mysql2date(): developer.wordpress.org/reference/functions/mysql2date
  3. Schema.org, datePublished: schema.org/datePublished

Related articles