Send PHP date to JavaScript Date format
Passing a server-generated timestamp from PHP to a client-side script often trips up developers who expect the data to simply "just work." The core issue usually isn't the language barrier between PHP and JavaScript; it is how each environment interprets time zones. In your specific scenario, you are generating a formatted date string in PHP using date_default_timezone_set('Australia/Perth') and expecting JavaScript to parse it directly.
The PHP code echo date("r") outputs Sun, 18 Mar 2012 12:01:23 +0800. This is a valid RFC 2822 formatted string that includes the time offset (+0800). When JavaScript's new Date() constructor receives this string, it successfully parses the date components. However, if your application logic relies on the resulting object representing midnight UTC or if the parsing fails due to malformed strings in other contexts, you will encounter unexpected behavior.
The confusion often stems from mixing "local time" with "UTC time." If you attempt to convert the PHP string to a timestamp using strtotime and then output it, you might see a discrepancy if date_default_timezone_set is not configured correctly on the server relative to your expectations. For instance, converting the Perth time to a Unix timestamp (seconds since 1970-01-01 00:00:00 UTC) requires ensuring that PHP treats the input as local first before converting.
echo strtotime(date("r"));
// This returns a Unix timestamp representing the exact moment in UTC.
When you output this timestamp as a string, JavaScript can parse it easily because it is a numeric value represented as text. However, if you use gmstrftime('%s'), you are explicitly generating the GMT timestamp. If your server's default timezone is not configured to match your application logic, this function might output a timestamp that corresponds to a different calendar day or time than intended, leading to the "invalid date" feeling or a 8-hour offset error in your JavaScript today variable.
To ensure reliability when passing dates from PHP to JavaScript, the most robust approach is to send the timestamp as an integer (Unix epoch) rather than a formatted string like D, d M Y H:i:s T. JavaScript's Date constructor handles numeric strings and ISO 8601 formats very well.
Here is a pattern that guarantees consistency regardless of the server's timezone configuration:
<?php
// Always use UTC for data transmission to avoid ambiguity
$timestamp = time();
echo $timestamp;
?>
In your JavaScript handler, you can then reconstruct the date in the desired timezone if needed:
$.get('time.php', function(data) {
// Assuming PHP returns a numeric timestamp like "1332069683"
var epoch = Number(data);
var today = new Date(epoch * 1000);
// You can now manipulate 'today' freely
console.log(today);
});
The trade-off here is that you lose the immediate timezone context sent by PHP, but you gain absolute accuracy. If you must send a formatted string, ensure it adheres to RFC 3339 or ISO 8601 standards (e.g., 2012-03-18T12:01:23+08:00). Avoid relying on the browser's implicit timezone guessing when passing complex date strings.
If you are building a larger application where this synchronization is critical, platforms like LaravelCompany often provide utilities that handle timezones seamlessly at the framework level, abstracting away the manual date() and strtotime() conversions so you don't have to manage these offsets manually.