百科狗-知识改变命运!
--

strptime() - php 日期时间函数

梵高1年前 (2023-11-21)阅读数 14#技术干货
文章标签过了

strptime()

(PHP 5 >= 5.1.0, PHP 7)

解析由strftime()生成的日期/时间

说明

strptime(string $date,string $format): array

strptime()返回一个将$date解析后的数组,如果出错返回FALSE

月份和星期几的名字以及其它与语种有关的字符串对应于setlocale()设定的当前区域(LC_TIME)。

参数

$date(string)

被解析的字符串(例如从strftime()返回的)

$format(string)

$date所使用的格式(例如同strftime()中所使用的相同)。

更多有关格式选项的信息见strftime()页面。

返回值

返回一个数组或者在失败时返回FALSE

数组中包含以下单元
键名说明
tm_sec当前分钟内的秒数(0-61)
tm_min当前小时内的分钟数(0-59)
tm_hour午夜起的小时数(0-23)
tm_mday月份中的第几天(1-31)
tm_mon自一月起过了几个月(0-11)
tm_year自 1900 年起过了几年
tm_wday自星期天起过了几天(0-6)
tm_yday本年自一月一日起过了多少天(0-365)
unparsed$date中未能通过指定的$format识别的部分

范例

Example #1strptime()例子

strptime() - php 日期时间函数

以上例程的输出类似于:

03/10/2004 15:54:19
Array
(
    [tm_sec] => 19
    [tm_min] => 54
    [tm_hour] => 15
    [tm_mday] => 3
    [tm_mon] => 9
    [tm_year] => 104
    [tm_wday] => 0
    [tm_yday] => 276
    [unparsed] =>
)

注释

Note:此函数未在 Windows 平台下实现。

Note:

Internally, this function calls thestrptime()function provided by the system's C library. This function can exhibit noticeably different behaviour across different operating systems. The use ofdate_parse_from_format(), which does not suffer from these issues, is recommended on PHP 5.3.0 and later.Note:

"tm_sec"includes any leap seconds (currently upto 2 a year). For more information on leap seconds, see the» Wikipedia article on leap seconds.Note:

Prior to PHP 5.2.0, this function could return undefined behaviour. Notably,the"tm_sec","tm_min"and"tm_hour"entries would return undefined values.

参见

  • checkdate() 验证一个格里高里日期
  • strftime() 根据区域设置格式化本地时间/日期
  • date_parse_from_format()Get info about given date formatted according to the specified format
  • DateTime::createFromFormat() 根据给定的格式解析日期时间字符串
If strptime() fails to match all of the format string and therefore an error occurred the function returns NULL.
emanuil's comment / mktime() example is wrong, in that his mktime() line should have $ts['tm_mon'] + 1 because strptime() returns the months zero-based, while mktime() expects it one-based.
Another portage for windows (from ex/yks toolkit) 
Be careful: the output of strptime() ( http://www.php.net/manual/en/function.strptime.php ) cannot always be used with mktime() ( http://www.php.net/manual/en/function.mktime.php )!
This is not because of what platform you're using or what format strings glibc supports. This is simply because strptime returns years SINCE 1900 (as documented above) and mktime expects a year in the format returned by date('Y') - which is the full 4 digits.
Therefore, if you parse a date with strptime and want to give it to mktime, you have to pass in ($parsed_time['tm_year'] + 1900) as the year parameter to mktime(), not just $parsed_time['tm_year'].
This issue arose when I had a date like: 19/06/2012 12:03:34. strtotime() doesn't parse this particular format, so I needed custom parsing. So I ended up with:
1. $ts = $service->getNeededDateTime();
2. $ts = strptime($ts, '%d/%m/%Y %H:%M:%S');
3. $ts = mktime($ts['tm_hour'], $ts['tm_min'], $ts['tm_sec'],
   $ts['tm_mon'], $ts['tm_mday'], ($ts['tm_year'] + 1900));
If you need strptime but are restricted to a php version which does not support it (windows or before PHP 5), note that MySQL since Version 4.1.1 offers (almost?) the same functionality with the STR_TO_DATE function.
See http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html
It says "Parse a time/date generated with strftime()" but that's not entirely correct -- While strptime("2006131", "%Y%W%u") works as expected, strptime("2006131", "%G%V%u") returns false instead of reversing the equivalent - and unambiguous - strftime() usage. I suspect that's because glibc doesn't support that. Anyway, this docu page fails to mention that apparently not all format components supported by strftime() can be used with strptime().
the example (or the function) has an inconsistancy with other PHP functions.
the example returns 104 for the year 2004
while in the strftime function the 2 digit year is 70-100 for 1970-2000
and 1-69 for 2001-2069
On some systems, particularly those of BSD lineage (such as FreeBSD and MacOS X), the tm_wday and tm_yday fields are only initialized if requested explicitly (that is, if the %a/%A/%u/%w and %j formats are specified), while others such as Linux and Solaris will calculate them automatically.
If you want to parse a date or a /time in windows env, i re-write strptime function for windows.
I use the same param and i return the same think that the original one.
I use sscanf to parde the string.
Only some format can be parsed (%S, %M, %H, %d, %m, %Y)
See this page (because the function is too big for this notes)
http://sauron.lionel.free.fr/?page=php_lib_strptime
preview : 
The result of strptime() is not affected by the current timezone setting, even though strftime() is. Tested in PHP 5.1.6.
/***Finding the days of a week ***/ 
For Windows user! It's rather the same as strptime!
It uses the previous function: but call strToTime($date, $format) to strToDate($date, $format) because this name is forgiven! 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)