Friday, September 5, 2025
HomeLanguagesPHP | strptime() Function

PHP | strptime() Function

The strptime() function is an inbuilt function in PHP which is used to parse a time / date generated with strftime() function. The date and format are sent as a parameter to the strptime() function and it returns an array on success or False on failure. The array returned by the strptime() function contains the following parameters:

  • tm_sec: It denotes the seconds after the minute (0-61).
  • tm_min: It denotes the minutes after the hour (0-59)
  • tm_hour: It denotes the hour since midnight (0-23)
  • tm_mday: It denotes the day of the month (1-31)
  • tm_mon: It denotes the months since January (0-11)
  • tm_year: It denotes the years since 1900
  • tm_wday: It denotes the days since Sunday (0-6)
  • tm_yday: It denotes the days since January 1 (0-365)
  • unparsed: It denotes the date part which was not recognized using the specified format

Syntax:

array strptime( $date, $format )

Parameters: This function accepts two parameter as mentioned above and described below:

  • $date: It is a mandatory parameter which specifies the string to parse.
  • $format: It is a mandatory parameter which specifies the format used in the date.

Return Value: This function returns an array on success or False on failure.

Below programs illustrate the strptime() function in PHP:

Program 1:




<?php
  
// Declaring the format of date/time
$format = "%d/%m/%Y %H:%M:%S";
  
// Parsing the date/time
$dt = strftime( $format );
echo "$dt";
print_r(strptime( $dt, $format ));
?>


Output:

22/08/2018 11:46:57Array
(
    [tm_sec] => 57
    [tm_min] => 46
    [tm_hour] => 11
    [tm_mday] => 22
    [tm_mon] => 7
    [tm_year] => 118
    [tm_wday] => 3
    [tm_yday] => 233
    [unparsed] => 
)

Program 2:




<?php
  
// Ddeclaring a different format of date/time
$format="%d/%m/%y %I:%M:%S";
  
// Parsing the date/time
$dt = strftime( $format );
echo "$dt";
print_r(strptime( $dt, $format ));
?>


Output:

22/08/18 11:46:59Array
(
    [tm_sec] => 59
    [tm_min] => 46
    [tm_hour] => 11
    [tm_mday] => 22
    [tm_mon] => 7
    [tm_year] => 118
    [tm_wday] => 3
    [tm_yday] => 233
    [unparsed] => 
)

Related Articles:

Reference: http://php.net/manual/en/function.strptime.php

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6636 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS