Convert this string to timestamp PHP

Convert this string to timestamp PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Parsing Ambiguous Date Strings in PHP

When you call strtotime() on a string like "13/10 15:00", you might expect it to return a Unix timestamp immediately. However, as your specific example shows, this function returns an empty value (or false). This isn't a bug; it is a deliberate behavior resulting from how PHP interprets date formats based on the server's locale settings.

The core issue lies in the ambiguity of the slashes and the month order. The string "13/10" could logically represent October 13th or the 13th day of the 10th month (which is invalid, but some parsers might get confused). More critically, strtotime() relies heavily on the default_timezone and the current locale to decide whether a format like DD/MM or MM/DD is being used.

If your server's locale assumes a US format (Month/Day), PHP attempts to parse "13" as the month. Since there is no 13th month, the parser fails silently and returns false. Conversely, if the locale expects Day/Month, it parses October 13th successfully. Because this behavior varies unpredictably between servers—especially in shared hosting environments where you cannot always control the global PHP configuration—you cannot rely on strtotime() for non-standard or ambiguous date strings.

To ensure your application works consistently across all environments, you must explicitly define the format using DateTime::createFromFormat(). This method bypasses locale guessing and forces PHP to interpret the string according to a strict pattern you provide.

Here is how you should handle the conversion:

$format = 'd/m/Y H:i'; // Day/Month Year Hour:Minute
$dateString = "13/10 15:00";

// The format must match exactly:
// d = two-digit day (01-31)
// m = two-digit month (01-12)
// Y = four-digit year (assumes current year if omitted, or you can add 'Y' to the string)
// H = hour in 24-hour format (00-23)
// i = minutes

if ($dateObj = DateTime::createFromFormat($format, $dateString)) {
    $timestamp = $dateObj->getTimestamp();
    
    // Handle potential timezone offset issues if needed
    echo $timestamp;
} else {
    // Log the error or handle the failure case
    // Note: Check for specific format errors using DateTime::getLastErrors()
    echo "Date parsing failed.";
}

Handling Missing Years

In your original example, the string "13/10 15:00" lacks a year. DateTime::createFromFormat() will default to the current year if you do not specify it in the format string. If your application requires strict validation where missing years should be rejected, you can modify the parsing logic to throw an exception or return null if the format does not match a four-digit year pattern.

// Strictly requiring a 4-digit year
$format = 'd/m/Y H:i'; 
// String must be "13/10/2023 15:00" for this to work

Timezone Considerations

A common pitfall when converting to timestamps is ignoring the timezone. The DateTime::createFromFormat() method creates an object in the default timezone of your server (e.g., UTC). If you need a timestamp representing that specific moment in a user's local time (e.g., Europe/London), you must set the timezone explicitly before getting the timestamp.

$dateObj = new DateTimeZone('Europe/London'); // Or any IANA timezone
$dateObj->setTimezone(new DateTimeZone('Europe/London'));

$timestamp = $dateObj->getTimestamp();
// This now includes the UTC offset for that specific timezone at that time

Why Not Use DateTimeImmutable?

While DateTime::createFromFormat() returns a mutable object, using DateTimeImmutable is often preferred in modern PHP applications to prevent accidental side effects. If you plan to modify the date later (e.g., adding days), creating an immutable instance first ensures that the original parsed value remains unchanged.

$dateObj = DateTimeImmutable::createFromFormat('d/m/Y H:i', "13/10 2023 15:00");
$timestamp = $dateObj->getTimestamp();

Best Practices for Date Handling in PHP

Relying on strtotime() for anything other than standard calendar expressions (like "next Monday" or "last month") is a fragile strategy. It introduces hidden dependencies on server configuration that lead to inconsistent behavior in production environments. By adopting DateTime::createFromFormat(), you gain explicit control over parsing logic.

If your project requires robust date manipulation, validation, and timezone handling without reinventing the wheel, consider integrating Laravel. Its built-in Carbon library simplifies these complex date operations significantly, providing a fluent interface that handles edge cases like leap years and DST transitions automatically. For more details on leveraging these tools within the ecosystem, you can explore resources at https://laravelcompany.com.

Ultimately, always prioritize explicit format strings over implicit parsing. The slight increase in code verbosity pays off by eliminating cryptic false returns and ensuring your date logic remains consistent regardless of the server's locale settings.

Our network