How to Parse Date String in PHP: Syntax and Examples
In PHP, you can parse a date string using
DateTime::createFromFormat to specify the exact format or strtotime for flexible parsing. These functions convert date strings into DateTime objects or timestamps for easy date manipulation.Syntax
DateTime::createFromFormat lets you parse a date string by specifying its exact format. The syntax is:
DateTime::createFromFormat(string $format, string $time, ?DateTimeZone $timezone = null): DateTime|false- $format: The pattern describing the date string (like Y-m-d for 2024-06-01).
- $time: The date string to parse.
- $timezone: Optional timezone object.
strtotime parses many date formats into a Unix timestamp:
int|false strtotime(string $datetime, int $baseTimestamp = time())- $datetime: The date string.
- $baseTimestamp: Optional base time for relative dates.
php
DateTime::createFromFormat('Y-m-d', '2024-06-01'); strtotime('next Monday');
Example
This example shows how to parse a date string with a known format using DateTime::createFromFormat and how to parse a flexible date string with strtotime. It then formats the date to a readable string.
php
<?php // Parse date with known format $dateString = '2024-06-01'; $date = DateTime::createFromFormat('Y-m-d', $dateString); if ($date === false) { echo "Failed to parse date."; } else { echo "Parsed date: " . $date->format('l, F j, Y') . "\n"; } // Parse flexible date string $timestamp = strtotime('next Monday'); if ($timestamp === false) { echo "Failed to parse date string."; } else { echo "Next Monday is: " . date('l, F j, Y', $timestamp) . "\n"; } ?>
Output
Parsed date: Saturday, June 1, 2024
Next Monday is: Monday, June 3, 2024
Common Pitfalls
Common mistakes include:
- Using
DateTime::createFromFormatwith a format that does not match the date string exactly, causing parsing to fail. - Not checking if the parsing returned
false, which means the date string was invalid. - Using
strtotimewith ambiguous or unsupported date formats, which can lead to unexpected results.
Always validate the result before using the parsed date.
php
<?php // Wrong: format does not match string $date = DateTime::createFromFormat('d/m/Y', '2024-06-01'); var_dump($date); // bool(false) // Right: matching format $date = DateTime::createFromFormat('Y-m-d', '2024-06-01'); var_dump($date instanceof DateTime); // bool(true) ?>
Output
bool(false)
bool(true)
Quick Reference
| Function | Purpose | Example Format |
|---|---|---|
| DateTime::createFromFormat | Parse date string with exact format | 'Y-m-d', '2024-06-01' |
| strtotime | Parse flexible date strings to timestamp | 'next Monday', 'last year' |
| DateTime::format | Format DateTime object to string | 'l, F j, Y' |
Key Takeaways
Use DateTime::createFromFormat to parse date strings with a known format precisely.
Use strtotime for flexible, natural language date strings but verify the result.
Always check if parsing returns false to handle invalid date strings safely.
Format parsed dates with DateTime::format for readable output.
Matching the format exactly is crucial for successful parsing with createFromFormat.