php strtotime有一天未完成

I have some date, like:

20 November 06:10
12 November 08:12
10 October 13:23

There all in the past 6 months, Now I want to strtotime() them, but they are all un complete (lack of year), how to make some process so that I could strtotime() them?

Try this:

$dates = array("10 October 13:23", "12 November 08:12", "10 October 13:23");

    foreach($dates as $d){
        $exploded = explode(" ", $d);
        $newDate = array_slice($exploded, 0,2,true)+array(2=>"2012")+array(3 => $exploded[2]);

        //print_r($newDate);
        $time = strtotime(implode($newDate));
        echo $time."<br/>";
    }

The output i got is:

1349868180
1352704320
1349868180

The logic is:

You lack the year, so I exploded the dates into an array to slice them, insert the year (the +array(2=>"2012") part) and glue them again with implode, and then run the strtotime.

This work only for this year, so you can use this logic to add the year to all your dates, or in the future there will be absolutely no way to filter dates from different years.

  • I added the dates into an array for loop through all of them, you can use the loop other ways, depending on where you have all your dates stored. For example if they are in a database you can include the script in the while($row = mysqli_fetch_assoc($result)) part where $d would be $row['date'] instead.

You should use the DateTime class and its createFromFormat and getTimeStamp methods instead of strtotime.

print_r(date_parse_from_format("d F H:i", '20 November 06:10'));

gives you:

Array
(
    [year] => 
    [month] => 11
    [day] => 20
    [hour] => 6
    [minute] => 10
    [second] => 0
    [fraction] => 
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] => 
)