PHP函数检查给定范围之间的时间? [英] PHP function to check time between the given range?

查看:117
本文介绍了PHP函数检查给定范围之间的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的项目中使用PHP的Date函数,并希望检查一个给定的日期的天气,在date-time.ie的给定范围之间,例如,如果当前日期时间是2010-11-16 12:25 :00 PM,想检查是否在2010-11-16 09:00:00 AM到06:00:00 PM之间。在上述情况下,它的真实和如果时间不在范围之间它应该返回false。
是否有任何内置的PHP函数来检查这个或者我们将不得不写一个新的函数?

解决方案

使用 strtotime 将两次转换为unix时间戳:



一个示例函数可能如下所示:

  function dateIsBetween($ from,$ to ,$ date ='now'){
$ date = is_int($ date)? $ date:strtotime($ date); //转换非时间戳
$ from = is_int($ from)? $ from:strtotime($ from); // ..
$ to = is_int($ to)? $ to:strtotime($ to); // ..
return($ date> $ from)&&& ($ date< $ to); //为了清晰起见,额外的括号
}


I am using PHP's Date functions in my project and want to check weather a given date-time lies between the given range of date-time.i.e for example if the current date-time is 2010-11-16 12:25:00 PM and want to check if the time is between 2010-11-16 09:00:00 AM to 06:00:00 PM. In the above case its true and if the time is not in between the range it should return false. Is there any inbuild PHP function to check this or we will have to write a new function??

解决方案

Simply use strtotime to convert the two times into unix timestamps:

A sample function could look like:

function dateIsBetween($from, $to, $date = 'now') {
    $date = is_int($date) ? $date : strtotime($date); // convert non timestamps
    $from = is_int($from) ? $from : strtotime($from); // ..
    $to = is_int($to) ? $to : strtotime($to);         // ..
    return ($date > $from) && ($date < $to); // extra parens for clarity
}

这篇关于PHP函数检查给定范围之间的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆