Home PHP Date/Time strptime() PHP strptime() Function Definition In PHP, strptime() parses a date and time string using C-style % format codes and returns the parts as an array. It is deprecated and no longer available in modern PHP.
This function is deprecated as of PHP 8.1 (removal planned for a future PHP major). It also never worked on Windows. For new code, use date_parse_from_format() or DateTimeImmutable::createFromFormat().
Syntax Call strptime() with the values you need. Start with this line.
$parts = strptime('2023-11-14 22:13:20', '%Y-%m-%d %H:%M:%S'); Call What it does strptime('2023-11-14 22:13:20', '%Y-%m-%d %H:%M:%S')Returns an array with tm_year, tm_mon, tm_mday and more (on PHP versions that still have it). date_parse_from_format('Y-m-d H:i:s', '2023-11-14 22:13:20')Returns an array with year, month, day, hour, minute and second. DateTimeImmutable::createFromFormat('Y-m-d', '2023-11-14')Returns a DateTimeImmutable object, or false when the text does not match.
Do not use this function in new code. The two modern replacements use date() style letters (Y, m, d, H, i, s) instead of % codes. Always check the result for errors, because user input is often invalid.
Library declarations strptime(string $timestamp, string $format): array|false Parameters Parameter Description timestamp Required. The date and time string to parse. format Required. The format of that string, written with % codes such as %Y, %m, %d, %H, %M and %S.
Return Value Return Value Description array An array with tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday and unparsed (the leftover text). false Returned when the string cannot be parsed.
PHP Examples 1. Legacy Call with a Safe Fallback 2. Check Errors and Warnings 3. Modern Replacement with DateTimeImmutable Example 1 — Legacy Call with a Safe Fallback Check that strptime() exists, and fall back to date_parse_from_format() when it does not. Both paths print the same text.
<?php
error_reporting(E_ALL & ~E_DEPRECATED);
$input = '2023-11-14 22:13:20';
if (function_exists('strptime')) {
$t = strptime($input, '%Y-%m-%d %H:%M:%S');
echo $t['tm_year'] + 1900, '-', $t['tm_mon'] + 1, '-', $t['tm_mday'], PHP_EOL;
} else {
$p = date_parse_from_format('Y-m-d H:i:s', $input);
echo $p['year'], '-', $p['month'], '-', $p['day'], PHP_EOL;
} How It Works function_exists() avoids a fatal error on versions where strptime() is gone. The legacy path fixes tm_year (+1900) and tm_mon (+1) before printing. date_parse_from_format() already returns normal values for year, month and day. Both branches print the same date. Example 2 — Check Errors and Warnings date_parse_from_format() reports problems in the result instead of throwing, so always check the counts.
<?php
$good = date_parse_from_format('Y-m-d', '2023-11-14');
$bad = date_parse_from_format('Y-m-d', 'not-a-date');
$invalid = date_parse_from_format('Y-m-d', '2023-02-30');
echo 'Good errors: ', $good['error_count'], PHP_EOL;
echo $bad['error_count'] > 0 ? 'Bad input has errors' : 'Bad input is fine', PHP_EOL;
echo 'Invalid date warnings: ', $invalid['warning_count'], PHP_EOL; Good errors: 0
Bad input has errors
Invalid date warnings: 1 How It Works A valid string gives error_count 0. 'not-a-date' does not match the format, so error_count is above 0. '2023-02-30' matches the format but is not a real date, so PHP adds a warning. Check both error_count and warning_count before trusting the parsed values. Frequently Asked Questions Is strptime() still available in PHP? No. It was Deprecated in PHP 8.1 (removal planned for a future PHP major). It was also never available on Windows.
What should I use instead of strptime()? Use DateTimeImmutable::createFromFormat() to get an object, or date_parse_from_format() to get an array of parts.
How are strptime() formats different from createFromFormat() formats? strptime() used % codes like %Y-%m-%d %H:%M:%S. createFromFormat() uses letters like Y-m-d H:i:s, the same ones as date().
Why was strptime() deprecated? It relied on the operating system's C library, so behaviour changed between systems and it did not work on Windows.
How do I know if parsing failed? createFromFormat() returns false. date_parse_from_format() sets error_count and warning_count above 0. Always check these before using the result.
Next: strtotime() strtotime() converts an English date and time description, such as '2023-11-14' or 'next M.
strtotime() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion