如何在PHP中检查时间是否介于两次之间

编程入门 行业动态 更新时间:2024-10-28 02:32:09
本文介绍了如何在PHP中检查时间是否介于两次之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下三个字符串(当前时间,日落和日出),如何确定当前时间是否在日出和日落之间?

I have following three strings (current time, sunset and sunrise), how can I find if the current time is between the sunrise and sunset?

$current_time = "10:59 pm"; $sunrise = "5:42 am"; $sunset = "6:26 pm";

推荐答案

$current_time = "4:59 pm"; $sunrise = "5:42 am"; $sunset = "6:26 pm"; $date1 = DateTime::createFromFormat('H:i a', $current_time); $date2 = DateTime::createFromFormat('H:i a', $sunrise); $date3 = DateTime::createFromFormat('H:i a', $sunset); if ($date1 > $date2 && $date1 < $date3) { echo 'here'; }

查看实际效果

参考

  • DateTime

更多推荐

如何在PHP中检查时间是否介于两次之间

本文发布于:2023-10-27 03:52:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532228.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   时间   如何在   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!