Home >>PHP Date Time Functions >PHP strptime() Function
PHP strptime() function is used to format a time or date generated with strftime() function. It accepts two parameters $date and $format. It returns an array containing all the information to the given date on success or False on failure.
Syntax:
strptime(date, format);
Parameter | Description |
---|---|
date | This is a required parameter. This parameter defines the string to parse. |
format | This is a required parameter. This parameter defines the format used in the date. |
Here is an example of strptime() in PHP:
<html> <body> <pre> <?php print_r(strptime(strftime("%d/%m/%Y %H:%M:%S"),"%d/%m/%Y")); ?> </pre> </body> </html>
Array ( [tm_sec] => 0 [tm_min] => 0 [tm_hour] => 0 [tm_mday] => 3 [tm_mon] => 1 [tm_year] => 120 [tm_wday] => 1 [tm_yday] => 33 [unparsed] => 05:57:31 )
Example 2:
<html> <body> <pre> <?php $format="%d/%m/%Y %H:%M:%S"; $strf=strftime($format); echo("$strf")."<br>"; print_r(strptime($strf,$format)); ?> </pre> </body> </html>
03/02/2020 05:57:14 Array ( [tm_sec] => 14 [tm_min] => 57 [tm_hour] => 5 [tm_mday] => 3 [tm_mon] => 1 [tm_year] => 120 [tm_wday] => 1 [tm_yday] => 33 [unparsed] => )