English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Date & Time Function Manual
The strptime() function parses the date/time generated by strftime()
strptime()function accepts a date string (with the same format as generatedof strftime() )and the format string as parameters, and parse the given string in the specified format.
strptime($date, $format)
Serial Number | Parameters and Description |
---|---|
1 | date (required) This is a string value representing the date to be analyzed. |
2 | format (required) This is a string value representing the format used to parse the date. |
The PHP strptime() function returns an array containing the parsed date (the array after parsing date). If it fails, this function will return a boolean valuefalse. The returned array contains the following keys and minus
tm_min - Number of minutes since the start of the current hour (0-59)
tm_hour - Number of hours since midnight (0-23)
tm_mday - Day of the month (1-31)
tm_mon - Number of months passed since January (0-11)
tm_year - from 1900 years passed since
tm_wday - Number of days passed since Sunday (0-6)
tm_yday - Number of days passed since January 1st of this year (0-365)
unparsed - parts of date that could not be recognized by the specified format in date
This function was originally introduced in PHP version5.1introduced in version 5.2.0 and is available in all higher versions.
The following examples demonstratestrptime()Usage of function-
<?php $format = '%A %d %B %G %T'; $strf = strftime($format); $res = strptime($strf, $format); print_r($res); ?>Test to see‹/›
Output result
Array ( [tm_sec] => 26 [tm_min] => 35 [tm_hour] => 15 [tm_mday] => 13 [tm_mon] => 4 [tm_year] => 0 [tm_wday] => 3 [tm_yday] => 132 [unparsed] => )
<?php $format = '%d/%m/%Y %H:%M:%S'; $strf = strftime($format); echo "$strf\n"; print_r(strptime($strf, $format)); ?>Test to see‹/›
Output result
08/06/2020 09:10:32 Array ( [tm_sec] => 32 [tm_min] => 10 [tm_hour] => 9 [tm_mday] => 8 [tm_mon] => 5 [tm_year] => 120 [tm_wday] => 1 [tm_yday] => 159 [unparsed] => )
The following is the use of strftime to format dates/Various characters representing time-
%a - Abbreviated name of the day of the week
%A - Full name of the day of the week
%b - Abbreviated name of the month
%B - Full name of the month
%c - Preferred date and time representation
%C - The digit representing the century (the year divided by 100, range from 00 to 99)
%d - Day of the month (01 to 31)
%D - Time format, the same as %m/%d/The same as the %y representation
%e - Day of the month (1 to 31)
%g - Similar to the %G representation but without the century
%G - Corresponding to the ISO week number 4 Two-digit year (see %V)
%h - The same as the %b representation
%H - hour, using 24 12-hour time notation (00 to 23)
%I - hour, using 12 12-hour time notation (01 to 12)
%j - Day of the year (001 to 366)
%m - Month (01 to 12)
%M - Minute
%n - Newline
%p - am or pm corresponding to the given time value
%r - 12-hour time notation with a.m. and p.m. markers
%R - 24 12-hour time notation
%S - Second
%t - Tab
%T -
㩵n - Numeric representation of the day of the week (1 to 7), Monday[星期一] = 1. Warning: In Sun Solaris systems, Sunday[星期日] = 1
%U - Number of weeks contained in the current year, starting from the first Sunday of the year as the first week
%V - ISO week number contained in the current year 8601 week number in the format (01 to 53), week 1 Representation of the first week of the current year, which must have at least four days and start with Monday as the first day
%W - Number of weeks contained in the current year, starting from the first Monday of the year as the first week
%w - Decimal number representing the day of the week, Sunday[星期日] = 0
%x - Preferred date representation without time
%X - Preferred time representation without date
%y - Year representation without the century digit (range from 00 to 99)
%Y - Year representation including the century digit
%Z or %z - Time zone name or abbreviation
%% - Output a % character