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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.