问题描述
我必须写一些东西来弄清楚今天/月组合适合的日期范围。 p>
我有一套设定的日期范围,分别是:
$ dateRanges = array(
1 =>1月16日至4月30日,
2 =>5月1日至6月30日,
3 =>7月1日至8月15日
4 =>8月16日至9月15日,
5 =>9月15日至10月15日,
6 =>10月16日至1月15日
);
我想要返回的是当前日期适合的范围的数组键。 / p>
目前,所有这些都是通过我的头,我必须设置一个大的if语句来查看当前的 date('j ')
和 date('n')
并匹配结果。但是肯定是相当凌乱而不是很有效率?
任何想法,更好的方法来解决这个问题将非常感谢!
$ today = time();
foreach($ dateRanges as $ key => $ range){
list($ start,$ end)= explode('to',$ range);
$ start。=''。 date('Y'); //将2011年添加到字符串
$ end。=''。 date('Y');
if((strtotime($ start)< = $ today)&&(strtotime($ end)> = $ today)){
break;
}
}
$ key将是匹配日期的索引范围或null / false。
My mind seems to be going blank on this one.
I have to write something to figure out which date range todays day/month combination fits into.
I have a set amount of date ranges, which are:
$dateRanges = array(
1 => "16 January to 30 April",
2 => "1 May to 30 June",
3 => "1 July to 15 August",
4 => "16 August to 15 September",
5 => "15 September to 15 October",
6 => "16 October to 15 January"
);
All I'm trying to return is the array key of the range the current date fits into.
At the moment all thats going through my head is I'll have to set up a large if statement to look at the current date('j')
and date('n')
and match the results up. But surely thats pretty messy and not very efficient?
Any ideas of a better way to approach this problem would be much appreciated!
$today = time();
foreach ($dateRanges as $key => $range) {
list($start, $end) = explode(' to ', $range);
$start .= ' ' . date('Y'); // add 2011 to the string
$end .= ' ' . date('Y');
if ((strtotime($start) <= $today) && (strtotime($end) >= $today)) {
break;
}
}
$key will be either the index of the matching date range, or null/false.
这篇关于有效的方法来检查当前日期是否在两个日期之间(年份不重要)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!