Question

Why date_parse does not recognise years in a string?

<?php
$string = "Saturday, 23 March, 2013";
print_r(date_parse($string));
?>

result,

Array
(
    [year] => 
    [month] => 3
    [day] => 23
    [hour] => 20
    [minute] => 13
    [second] => 0
    [fraction] => 0
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] => 
    [relative] => Array
        (
            [year] => 0
            [month] => 0
            [day] => 0
            [hour] => 0
            [minute] => 0
            [second] => 0
            [weekday] => 6
        )

)
Was it helpful?

Solution

Looks like the format isn't correct. It does return the correct year, when you do the following.

$string = "Saturday, 23 March, 2013";
                             ^ //no comma here, should be just 23 March 2013

Everywhere in the supported formats, it is seen that all components of the Date-string and the time-string have the same delimiters separating them.

OTHER TIPS

You might also try strtotime(). It's an amazing function and handles a wide variety of inputs - even relative ones like "3 days ago."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top