How to handle datetime between php (Laravel api) and javascript (AngularJS)
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Date Synchronization: Handling Datetime Between PHP (Laravel API) and AngularJS
Dealing with datetime synchronization between a backend API (like Laravel/PHP) and a frontend framework (like AngularJS/JavaScript) is one of the most common sources of frustration for developers. You’ve correctly identified the core conflict: ensuring that timezones, formatting standards, and data types translate seamlessly across the network boundary.
The experience you described—where PHP handles dates beautifully using Carbon's ISO 8601 format, but JavaScript struggles when sending complex Date objects back—is a textbook example of serialization issues. As senior developers, our goal is to establish a single source of truth for date handling and enforce strict serialization protocols on both ends.
Here is a comprehensive guide on navigating this challenge and establishing the most robust solution.
The Foundation: Why ISO 8601 is Your Best Friend
Your initial success using Carbon\Carbon::setToStringFormat('c') to output dates in the ISO 8601 format (e.g., 2015-02-04T00:53:51+02:00) was the correct starting point. ISO 8601 is an international standard for representing dates and times, including timezone information. This string format is unambiguous, machine-readable, and universally understood by both PHP and JavaScript environments.
The problem arises not in the server-side formatting, but in how the client attempts to serialize its local time back into a format the server expects, especially concerning milliseconds and UTC offsets.
The Solution: Standardizing on UTC Timestamps
The most automatic and reliable solution is to never rely on sending complex JavaScript Date objects directly across the wire. Instead, you should treat all date exchange as pure numerical timestamps, which are inherently timezone-agnostic when stored in UTC.
1. Server-Side Best Practice (PHP/Laravel)
When retrieving data or preparing data for transmission from your Laravel API, ensure that any time stored in the database is consistently stored in UTC. Carbon excels at this:
// Ensure you are working with UTC internally
$dateTime = Carbon::now();
$isoString = $dateTime->toIso8601String(); // e.g., 2024-01-01T10:00:00+00:00
return response()->json(['timestamp' => $isoString]);
2. Client-Side Best Practice (AngularJS/JavaScript)
The AngularJS frontend should focus on sending the raw timestamp—the number of milliseconds since the Unix epoch—which is what modern APIs prefer for data exchange. This bypasses all timezone interpretation issues on the network layer.
When you need to display or manipulate this date in the client, use JavaScript's Date object, but be mindful of its inherent local time context.
Sending Data to PHP:
Instead of sending a formatted string, send the raw timestamp:
// AngularJS Service or Controller logic
const now = new Date();
// Use .getTime() to get the UTC milliseconds timestamp
const unixTimestamp = now.getTime();
// Sending via HTTP request (e.g., using $http service)
$http.post('/api/update-time', { timestamp: unixTimestamp });
Receiving Data in PHP:
When Laravel receives this integer, you can use it directly to reconstruct the date object, ensuring it is interpreted correctly as UTC if that was your intention:
// In your Laravel Controller method receiving the request
$requestData = $request->json()->all();
$timestampMs = $requestData['timestamp'];
// Convert milliseconds back to a Carbon instance (which handles timezones well)
$carbonDate = Carbon::createFromTimestamp($timestampMs);
return response()->json(['message' => $carbonDate->toDateTimeString()]);
Conclusion: Consistency is Key
The friction you experienced stems from trying to force string formatting where pure numerical serialization is more robust. By adhering to the principle of "send raw UTC timestamps, and format on the client or server as needed," you eliminate ambiguity.
When building APIs with Laravel, understanding how Carbon handles these underlying timestamps is crucial for consistency across your entire application architecture. Whether you are working with Eloquent models or defining API responses, maintaining strict adherence to UTC-based numerical exchange ensures that your application remains reliable and scalable. For more advanced insights into structuring robust data handling in the Laravel ecosystem, exploring official documentation from laravelcompany.com is highly recommended.