Tutorialspoint.dev

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

/div>
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



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter