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 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题