How to convert a date YYYY-MM-DD to epoch in PHP

How to convert a date YYYY-MM-DD to epoch in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Converting YYYY-MM-DD Strings to Unix Timestamps in PHP

Working with dates in PHP often requires bridging the gap between human-readable strings and machine-processable integers. The Unix timestamp, representing the number of seconds since January 1, 1970, is a standard for sorting, calculating intervals, and interfacing with external APIs. When you need to convert a date string formatted as YYYY-MM-DD into this epoch value, the most direct approach relies on PHP's built-in time manipulation functions.

The core mechanism involves parsing the string into components that the system can interpret as a specific moment in time. The strtotime() function is the utility designed for exactly this purpose. It accepts a date string and returns the corresponding Unix timestamp. If you pass it "2023-10-05", it understands that format, strips away any ambiguity regarding timezones (defaulting to UTC), and returns an integer representing seconds since the epoch.

$dateString = '2023-10-05';
$timestamp = strtotime($dateString);

// Verify the result
var_dump($timestamp); // int(1696473600)

This method is robust because strtotime handles a wide array of date formats, not just ISO 8601. However, relying solely on strtotime can introduce subtle bugs if your application environment has specific timezone configurations or if the input string contains unexpected whitespace. For strict validation where you only expect dates and nothing else, passing the string directly is generally safe, but being mindful of how strtotime interprets relative strings like "next Monday" is crucial if that logic ever bleeds into your parsing code.

If you are working in a Laravel project or need more predictable behavior across different server environments, consider using the Carbon library or Laravel's native Carbon facade. These tools abstract away the complexities of timezone handling and provide a fluent interface for date manipulation. While strtotime is sufficient for simple conversions, Carbon offers explicit parsing that prevents silent failures if the input format deviates slightly from expectations.

use Carbon\Carbon;

$dateString = '2023-10-05';
$carbonDate = Carbon::parse($dateString);
$timestamp = $carbonDate->getTimestamp();

var_dump($timestamp); // int(1696473600)

Using Carbon::parse() explicitly tells the system to interpret the string as a date without requiring additional time information. The resulting object holds the parsed data, and calling getTimestamp() retrieves the epoch value. This approach is often preferred in enterprise applications because it makes the intent of the code clearer: you are parsing a date, not just converting a string to an integer via a heuristic function.

A critical pitfall to avoid is assuming that all clients or external systems send dates in UTC. If your application stores timestamps but receives YYYY-MM-DD strings from a frontend without timezone context, the resulting epoch will be calculated based on the server's default timezone unless you explicitly set it. In many cases, this means the string "2023-10-05" is treated as midnight in the local server timezone (e.g., UTC+2), which results in a timestamp 7200 seconds ahead of UTC midnight.

To ensure consistency regardless of server location, you should parse the date in UTC or explicitly define the timezone for the input. If the string represents a calendar day without a specific time, adding T00:00:00 to the string and setting the timezone to UTC guarantees that the epoch calculation starts at 00:00:00 UTC on that day.

$utcDate = '2023-10-05T00:00:00Z';
$timestamp = strtotime($utcDate);

This pattern eliminates ambiguity. The Z suffix denotes UTC, ensuring the calculation is independent of server configuration. When integrating with APIs or databases that expect Unix timestamps, this precision prevents off-by-one-day errors that can accumulate over time. Always validate the resulting timestamp against known boundaries if the data represents historical events to ensure no timezone drift has occurred during parsing.

Our network