本文实例讲述了PHP判断两个给定日期是否在同一周的方法。分享给大家供大家参考,具体如下:
/**
* 判断两日期是不是同一周
* 星期是按周日到周六
*/
function getSameWeek($pretime,$aftertime){
$flag = false;//默认不是同一周
$afweek = date('w',$aftertime);//当前是星期几
$mintime = $aftertime - $afweek * 3600*24;//一周开始时间
$maxtime = $aftertime + (7-$afweek)*3600*24;//一周结束时间
if ( $pretime >= $mintime && $pretime <= $maxtime){//同一周
$flag = true;
}
return $flag;
}
$testWeek_start=strtotime('2017-8-19');
$testWeek_end=strtotime('2017-8-23');
echo getSameWeek($testWeek_start,$testWeek_end)?'在同一周':'不在同一周';
运行结果:
不在同一周
[b]PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:[/b]
[b]在线日期/天数计算器:
[/b][url=http://tools.jb51.net/jisuanqi/date_jisuanqi]http://tools.jb51.net/jisuanqi/date_jisuanqi[/url]
[b]在线日期计算器/相差天数计算器:
[/b][url=http://tools.jb51.net/jisuanqi/datecalc]http://tools.jb51.net/jisuanqi/datecalc[/url]
[b]在线日期天数差计算器:
[/b][url=http://tools.jb51.net/jisuanqi/onlinedatejsq]http://tools.jb51.net/jisuanqi/onlinedatejsq[/url]
[b]Unix时间戳(timestamp)转换工具:
[/b][url=http://tools.jb51.net/code/unixtime]http://tools.jb51.net/code/unixtime[/url]
更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/96.htm]php日期与时间用法总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/348.htm]PHP基本语法入门教程[/url]》、《[url=http://www.1sucai.cn/Special/357.htm]PHP运算与运算符用法总结[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》及《[url=http://www.1sucai.cn/Special/630.htm]PHP数学运算技巧总结[/url]》
希望本文所述对大家PHP程序设计有所帮助。