Convert PHP date into javascript date format

Convert PHP date into javascript date format

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Convert PHP Date into JavaScript Date Format

When passing dates from a PHP backend to a JavaScript frontend, the most common pitfall is trying to mimic the verbose output of new Date() by formatting the string directly in PHP. The source material highlights a classic confusion: seeing "Fri Jun 01 2012 11:08:48 GMT+0100" and assuming that reproducing this exact string format via date('d-m-Y G:H') (or similar) is the solution. It isn't.

The core issue lies in how JavaScript's internal date parser handles strings versus ISO 8601 standards. When you construct a new Date object in JavaScript, passing a long-form English string like "Fri Jun 01..." works only because the browser has built-in logic to parse that specific locale-based format. However, this behavior is brittle. It relies on the user's system locale, timezone settings, and the specific phrasing of the month/day. If a user switches their browser language from English to French, or if your PHP script outputs "GMT+0100" instead of "GMT+01:00", the JavaScript parser may fail silently or parse the date incorrectly.

The robust solution is to stop trying to replicate the human-readable output and instead format the date as an ISO 8601 string. This standardizes the data regardless of timezone or locale. In PHP, you should output the timestamp in a way that JavaScript can universally accept. The most compatible format is Y-m-d\TH:i:sP, which corresponds to the ISO 8601 standard with timezone offset information.

Here is how you modify your PHP array assignment:

// Correct approach using ISO 8601 format
$newticket['DateCreated'] = date('c', strtotime($addticket));

Alternatively, if you are working with a raw string or need explicit control over the output without relying on strtotime, use the 'Y-m-d\TH:i:sP' format directly:

// Explicit ISO 8601 formatting
$newticket['DateCreated'] = date('Y-m-d\TH:i:sP', strtotime($addticket));

This outputs a string like "2012-06-01T11:08:48+01:00". When this JSON is consumed by JavaScript via JSON.parse(), the resulting value is a standard ISO string. Passing this directly to the Date constructor works flawlessly in all modern browsers:

const date = new Date("2012-06-01T11:08:48+01:00");
console.log(date); // Works correctly regardless of locale

Why does this work where the long-form string fails? The ISO 8601 format is defined specifically for data interchange. It removes ambiguity regarding month names (using numbers instead) and explicitly states the timezone offset using + or -. JavaScript's Date parser has strict rules for this format; if the characters do not match the expected pattern, it throws an error or returns an invalid date. By contrast, the long-form string "Fri Jun 01..." is parsed loosely. It assumes the user knows what "Jun" means in their current language context, which breaks internationalization.

If you are using a framework like Laravel, this becomes even more straightforward. You can utilize Carbon or the native Carbon package to handle the formatting:

use Illuminate\Support\Carbon;

$newticket['DateCreated'] = Carbon::parse($addticket)->format('Y-m-d\TH:i:sP');

Using a library like Carbon ensures that your timestamp includes the correct timezone offset relative to your server's configuration, preventing the "off by one day" errors that often plague naive datetime conversions.

There is a trade-off here: the resulting JSON string will be slightly longer than a simple YYYY-MM-DD format because it includes time and timezone data. However, omitting this information forces JavaScript to assume a default timezone (usually UTC), which can lead to display bugs where dates appear one day earlier or later depending on the user's location. Preserving the offset in the string ensures the date is rendered correctly on every client without requiring complex timezone calculations in the frontend code.

In summary, do not try to replicate the verbose English string output of new Date() using PHP's date() function. Instead, format your data as an ISO 8601 string. This approach eliminates locale dependencies, avoids parsing errors, and ensures that your date handling remains consistent across different environments.

Our network