我有一些数据,所以从一个datepicker:计算结束日期与禁用日期周数和
$disabled_dates = "08/10/2017, 08/17/2017";
$start_date = "08/03/2017";
$num_of_weeks = "20";
我想计算的终止日期基于关闭$start_date
和$num_of_weeks
我知道这是可能的new Date()
,但我不知道如何解释$disabled_dates
。
我有一些数据,所以从一个datepicker:计算结束日期与禁用日期周数和
$disabled_dates = "08/10/2017, 08/17/2017";
$start_date = "08/03/2017";
$num_of_weeks = "20";
我想计算的终止日期基于关闭$start_date
和$num_of_weeks
我知道这是可能的new Date()
,但我不知道如何解释$disabled_dates
。
strtotime()对于这样的事情来说是一个非常有用的功能。它接受各种各样的自然语言和日期/时间输入。
20从正好现在周从那天
echo date('c',strtotime('08/03/2017 +20 weeks'))."\n";
你的答案PHP开始
echo date('c',strtotime('+20 weeks'))."\n";
20周:
$disabled_dates = "08/10/2017, 08/17/2017";
$start_date = "08/03/2017";
$num_of_weeks = "20";
$the_end = strtotime($start_date.' GMT +'.$num_of_weeks.' weeks');
//make all the disabled dates into timestamps for easy comparison later
$disabled_dates_array = array();
foreach(explode(',', $disabled_dates) as $date){
$disabled_dates_array[] = strtotime(trim($date).' GMT');
}
//now compare and delay the end date if needed
foreach($disabled_dates_array as $timestamp){
//if there was a disabled date before the end, add a day's worth of seconds
//strtotime() returns false if it can't parse the date, so make sure it's truthy
if($timestamp && $timestamp <= $the_end){
$the_end += 86400;
}
}
$enddate = date('m/d/Y',$the_end);
编辑1:将GMT添加到所有strtotim e()转换,以避免夏令时更改日期之间秒数的问题。由于夏令时,有些日子是23小时,有些是25。在unix时间,Leap seconds不是问题。
编辑2:抢答的JavaScript:
var disabled_dates = "08/10/2017, 08/17/2017";
var start_date = "08/03/2017";
var num_of_weeks = "20";
var the_end = Date.parse(start_date + ' GMT') + parseInt(num_of_weeks)*7*86400*1000;
//in javascript Date.parse is similar to php's strtotime,
//but it returns milliseconds instead of seconds
disabled_dates = disabled_dates.split(", ");
for(var i = 0, len = disabled_dates.length; i < len; i++){
disabled_dates[i] = Date.parse(disabled_dates[i] + ' GMT');
if(disabled_dates[i] && disabled_dates[i] <= the_end){
the_end += 86400000;
}
}
the_end = new Date(the_end);
var enddate = ('0' + (the_end.getUTCMonth() + 1)).substr(-2) + '/' + ('0' + the_end.getUTCDate()).substr(-2) + '/' + the_end.getUTCFullYear();
console.log(enddate);
在这里,我遇到了夏令时间问题,因为
Sun Oct 29 2017 00:00:00 GMT+0100 (GMT Daylight Time) + 24 hours =
Sun Oct 29 2017 23:00:00 GMT+0000 (GMT Standard Time)
所以添加 'GMT'(GMT标准时间)在日期结束时很重要,否则结果可能会关闭一天。
This video对如何使时间变得复杂提供了一些见解。
真棒,谢谢!有没有更好的方法来使用jQuery或Javascript做到这一点? – 626
@ 626我在javascript中添加了一个解决方案,并编辑了我的答案以处理由夏时制引入的可能错误。 –
我不知道如果有一个更简单的方法,但这是谁,我会做到这一点:
// Put dates into array or split the string
$disabled = array(new DateTime('2012-08-01'),new DateTime('2017-09-19'));
$end_date = $date->add(new DateInterval('P'.$num_of_weeks.'D'));
$range = new DatePeriod($start_date, new DateInterval('P1D'),$end_date);
// remove disabled days
foreach($range as $date){
if(in_array($date,$disabled))
$end_date = $end_date->sub(new DateInterval('P1D'));
}
代码没有测试,但它应该工作。如果没有,让我知道xD。
希望有所帮助。
你需要考虑什么?它对我不清楚。残疾人日期应该被忽略,如果是这样的开始或结束?如果在20周的范围内找到日期,是否应该添加日期? – Yolo