需要一个判断函数,返回是否合法时间,PHP
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了需要一个判断函数,返回是否合法时间,PHP,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2444字,纯文字阅读大概需要4分钟。
内容图文
![需要一个判断函数,返回是否合法时间,PHP](/upload/InfoBanner/zyjiaocheng/149/68ff5311c0df4af8b377bcab7e773a78.jpg)
需要写一个函数isValidDate($date), 条件如下:
function isValidDate($date)
{
//1. $date 是本周时 + time()要在$date前一天的18:00之前 = true
//2. $date 为下周时 + time()要在本周四下午六点后 = true
//3. 其余返回false. (注:一周从周一开始)
$orderTime = strtotime($date);
$now = time();
if(date('W',$orderTime) == date('W',$now) && (strtotime($date,$now) - $now) > 86400/4) //预订前一天的18:00,截止预订
{
return true;
}
if(date('W',$orderTime) == date('W',$now) + 1 && $now > strtotime('saturday 18:05 -2 day',$now)) //预订第二周,周四下午六点及之后
{
return true;
}
return false;
}
其中,我写第二个条件的时候,发现条件覆盖的时间好像有点问题,想看下各位的见解哈。
回复内容:
需要写一个函数isValidDate($date), 条件如下:
function isValidDate($date)
{
//1. $date 是本周时 + time()要在$date前一天的18:00之前 = true
//2. $date 为下周时 + time()要在本周四下午六点后 = true
//3. 其余返回false. (注:一周从周一开始)
$orderTime = strtotime($date);
$now = time();
if(date('W',$orderTime) == date('W',$now) && (strtotime($date,$now) - $now) > 86400/4) //预订前一天的18:00,截止预订
{
return true;
}
if(date('W',$orderTime) == date('W',$now) + 1 && $now > strtotime('saturday 18:05 -2 day',$now)) //预订第二周,周四下午六点及之后
{
return true;
}
return false;
}
其中,我写第二个条件的时候,发现条件覆盖的时间好像有点问题,想看下各位的见解哈。
怎么感觉这是要强行给别人做面试题的呢?既然条理都能列 123 了,实现应该不是问题吧。。。
安装 Carbon
use Carbon\Carbon;
/**
* 校验日期
* @param string $date 日期
* @return boolean
*/
function isValidDate($date)
{
// $date 是本周时 + time()要在$date前一天的18:00之前 = true
if (Carbon::parse($date)->format('W') == Carbon::now()->format('W') &&
time() < Carbon::parse($date)->subDay(1)->hour(18)->minute(0)->timestamp
) {
return true;
}
// $date 为下周时 + time()要在本周四下午六点后 = true
elseif (
Carbon::parse($date)->format('W') == Carbon::now()->addWeek(1)->format('W') &&
time() > Carbon::now()->startOfDay()->addDay(3)->hour(18)->minute(0)->timestamp
) {
return true;
}
return false;
}
简单改一下题主的 if 语句 return
function isValidDate($date)
{
//1. $date 是本周时 + time()要在$date前一天的18:00之前 = true
//2. $date 为下周时 + time()要在本周四下午六点后 = true
//3. 其余返回false. (注:一周从周一开始)
$orderTime = strtotime($date);
$now = time();
if(date('W',$orderTime) === date('W',$now)) // 当前周
{
// time()要在$date前一天的18:00之前 = true
return $now < strtotime(date('Y-m-d 18:00:00',strtotime("$date -1 day")));
}
if(date('W',$orderTime) === (date('W',$now) + 1)) // 下周
{
// time()要在本周四下午六点后 = true
return $now > strtotime(date('Y-m-d 18:00:00',strtotime( '+'. 4-date('w') .' days' )));
}
return false;
}
内容总结
以上是互联网集市为您收集整理的需要一个判断函数,返回是否合法时间,PHP全部内容,希望文章能够帮你解决需要一个判断函数,返回是否合法时间,PHP所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。