栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

与PHP中的日期数组相比,如何获取最接近的日期

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

与PHP中的日期数组相比,如何获取最接近的日期

我可能没有最好的命名约定,但是可以。

我计算日期数组和给定日期之间的间隔。然后,我进行排序以找到“最小”差异。

$dates = array(    '0'=> "2013-02-18 05:14:54",    '1'=> "2013-02-12 01:44:03",    '2'=> "2013-02-05 16:25:07",    '3'=> "2013-01-29 02:00:15",    '4'=> "2013-01-27 18:33:45");function find_closest($array, $date){    //$count = 0;    foreach($array as $day)    {        //$interval[$count] = abs(strtotime($date) - strtotime($day));        $interval[] = abs(strtotime($date) - strtotime($day));        //$count++;    }    asort($interval);    $closest = key($interval);    echo $array[$closest];}find_closest($dates, "2013-02-18 05:14:55");


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/443072.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号