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:
<?php
Array
(
[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:
<?php
Array
(
[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:
<?php
Array
(
[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:
<?php $dDate = strtotime( '2017-03-00' );
print_r( getdate( $dDate ) ); ?>
displays:
<?php Array
(
[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.