php parse time,PHP: date_parse - Manual

It's sometimes useful to be able to store incomplete dates, for example when all you know of someone's birthdate is the year or the month and day.

date_parse() handles (and MySQL accepts) dates containing zero-value elements such as "2017-00-00" and "0000-03-29", leaving it up to the parent application to determine when to require and how to handle missing date elements. date_parse() correctly reports zero values for zero-value date elements, reports an 'invalid date' warning, and does not report an error.

Example 1: Year only

<?php print_r(date_parse('2017-00-00') );?>generates:

(

[year] =>2017[month] =>0[day] =>0[hour] =>

[minute] =>

[second] =>

[fraction] =>

[warning_count] =>1[warnings] => Array

(

[11] =>The parsed date was invalid)

[error_count] =>0[errors] => Array

(

)

[is_localtime] =>

)?>

Example 2: Month and day only

<?php print_r(date_parse('0000-03-29') )?>generates:

(

[year] =>0[month] =>3[day] =>29[hour] =>

[minute] =>

[second] =>

[fraction] =>

[warning_count] =>1[warnings] => Array

(

[11] =>The parsed date was invalid)

[error_count] =>0[errors] => Array

(

)

[is_localtime] =>

)?>

However, simply omitting date elements gives PHP too much discretion in second-guessing our intentions:

Example 3: Truncated date:

<?php print_r(date_parse('2017-03') )?>generates:

(

[year] =>2017[month] =>3[day] =>1[hour] =>

[minute] =>

[second] =>

[fraction] =>

[warning_count] =>0[warnings] => Array

(

)

[error_count] =>0[errors] => Array

(

)

[is_localtime] =>

)?>In this case, PHP supplies a day value of 1 and does not report a warning.

Similarly, this feature of accepting zero date elements does not carry over to timestamps:

displays:

(

[seconds] =>0[minutes] =>0[hours] =>0[mday] =>28[wday] =>2[mon] =>2[year] =>2017[yday] =>58[weekday] =>Tuesday[month] =>February[0] =>1488268800)?>In this case, PHP interprets the "zeroth" day of March to be the last day of February.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值