dongshang4984 2012-11-20 09:15
浏览 60
已采纳

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?

  • 写回答

3条回答 默认 最新

  • dongxun6458 2012-11-20 09:36
    关注

    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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?