Open In App

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:

Syntax:



array strptime( $date, $format )

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

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


Article Tags :