PHP strptime() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

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.

PHP
$parts = strptime('2023-11-14 22:13:20', '%Y-%m-%d %H:%M:%S');
CallWhat 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
PHP
strptime(string $timestamp, string $format): array|false

Parameters

ParameterDescription
timestampRequired. The date and time string to parse.
formatRequired. The format of that string, written with % codes such as %Y, %m, %d, %H, %M and %S.

Return Value

Return ValueDescription
arrayAn array with tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday and unparsed (the leftover text).
falseReturned when the string cannot be parsed.

PHP Examples

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
<?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

  1. function_exists() avoids a fatal error on versions where strptime() is gone.
  2. The legacy path fixes tm_year (+1900) and tm_mon (+1) before printing.
  3. date_parse_from_format() already returns normal values for year, month and day.
  4. 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
<?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;

How It Works

  1. A valid string gives error_count 0.
  2. 'not-a-date' does not match the format, so error_count is above 0.
  3. '2023-02-30' matches the format but is not a real date, so PHP adds a warning.
  4. Check both error_count and warning_count before trusting the parsed values.

Example 3 — Modern Replacement with DateTimeImmutable

createFromFormat() returns an object you can format, compare and modify. It returns false for bad input.

PHP
<?php
$utc = new DateTimeZone('UTC');

$date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2023-11-14 22:13:20', $utc);
echo $date->getTimestamp(), PHP_EOL;
echo $date->format('l, F j, Y'), PHP_EOL;

$bad = DateTimeImmutable::createFromFormat('Y-m-d', 'nope', $utc);
echo $bad === false ? 'Could not parse' : 'Parsed', PHP_EOL;

How It Works

  1. DateTimeZone('UTC') makes the parsing time zone explicit.
  2. createFromFormat() reads the text using date() style letters.
  3. getTimestamp() and format() turn the object into a number or text.
  4. A string that does not match the format returns false, so test it with === false.

Frequently Asked Questions

No. It was Deprecated in PHP 8.1 (removal planned for a future PHP major). It was also never available on Windows.
Use DateTimeImmutable::createFromFormat() to get an object, or date_parse_from_format() to get an array of parts.
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().
It relied on the operating system's C library, so behaviour changed between systems and it did not work on Windows.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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