Check if date is empty

Check if date is empty

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

Stefan Bogdanescu

Founder & Senior Architect

Handling Empty Dates in PHP Loops

When processing records in a while loop, displaying data conditionally is a common requirement. You might encounter scenarios where a date field contains 0000-00-00, a legacy marker often used when no actual date was provided. The goal is to render the formatted date only if it represents a valid timestamp, effectively hiding the placeholder value.

A frequent stumbling block occurs when using PHP's date() function with strtotime(). Consider this snippet:

date('d-m-Y', strtotime($row['date']))

If $row['date'] holds the string 0000-00-00, strtotime() fails to parse it as a valid Unix timestamp. Instead of returning false, it often defaults to the Unix epoch: 01-01-1970. This behavior stems from how strtotime handles invalid inputs; when it cannot determine a specific time, it falls back to the start of the epoch (January 1, 1970, 00:00:00 UTC). Consequently, your conditionals might pass even when no real date exists, leading to unwanted output like "01-01-1970" instead of nothing.

To resolve this, you must explicitly check whether the parsed timestamp is valid before formatting it. The most robust approach involves comparing the result against false or checking if the year matches the default epoch value when an invalid string is provided. Here is a reliable pattern for filtering out empty dates:

$dateString = $row['date'];
$formattedDate = '';

if ($dateString && strtotime($dateString) !== false) {
    $formattedDate = date('d-m-Y', strtotime($dateString));
}

echo $formattedDate;

In this implementation, we first ensure $dateString is not empty. Then, we pass it to strtotime(). If the function returns false, it indicates an invalid date string (or a failure to parse). Only if the timestamp is valid do we proceed to format it using date('d-m-Y', ...) and store the result in $formattedDate. By echoing only $formattedDate rather than calling date() directly inside the echo statement, you avoid accidentally printing the default epoch if the check were omitted.

However, relying solely on strtotime() can be risky if your application depends on strict date validation. A more explicit method is to convert the string directly to a timestamp using DateTime::createFromFormat or checking the year component if you know the invalid marker is specifically 0000-00-00. For instance:

if ($row['date'] !== '0000-00-00' && strtotime($row['date']) !== false) {
    echo date('d-m-Y', strtotime($row['date']));
}

This conditional explicitly ignores the common database placeholder 0000-00-00. If your data quality is inconsistent and you encounter other malformed dates, checking for false returns from strtotime() becomes mandatory.

In a Laravel environment, you might prefer using Carbon for date manipulation, which handles these edge cases more gracefully. However, the core logic remains the same: validate the input before formatting. Whether you are iterating through a database result set or processing API responses, always guard against invalid timestamps that default to the epoch. This prevents silent data corruption in your user-facing output and ensures a cleaner experience for your visitors.

For further insights on date handling best practices within the PHP ecosystem, you can explore resources like https://laravelcompany.com.

Our network