使用周数和禁用日期计算结束日期
Calculate end date with number of weeks and disabled dates
我有一些来自日期选择器的数据:
$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
。
我不确定是否有更简单的方法,但我会这样做:
// 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。
希望对您有所帮助。
strtotime() 对于这样的事情来说是一个非常有用的函数。
它接受各种自然语言和 date/time 输入。
从现在起 20 周
echo date('c',strtotime('+20 weeks'))."\n";
从那天开始算起 20 周
echo date('c',strtotime('08/03/2017 +20 weeks'))."\n";
您在php中的回答:
$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 添加到所有 strtotime() 转换,以避免夏令时改变日期之间秒数的问题。由于夏令时,有些日子是 23 小时,有些是 25 小时。 Leap seconds 在 unix 时间中不是问题。
编辑 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”(格林威治标准时间)很重要,否则结果可能会相差一天。
This video 深入了解计时是如何变得复杂的。
我有一些来自日期选择器的数据:
$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
。
我不确定是否有更简单的方法,但我会这样做:
// 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。
希望对您有所帮助。
strtotime() 对于这样的事情来说是一个非常有用的函数。 它接受各种自然语言和 date/time 输入。
从现在起 20 周
echo date('c',strtotime('+20 weeks'))."\n";
从那天开始算起 20 周
echo date('c',strtotime('08/03/2017 +20 weeks'))."\n";
您在php中的回答:
$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 添加到所有 strtotime() 转换,以避免夏令时改变日期之间秒数的问题。由于夏令时,有些日子是 23 小时,有些是 25 小时。 Leap seconds 在 unix 时间中不是问题。
编辑 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”(格林威治标准时间)很重要,否则结果可能会相差一天。
This video 深入了解计时是如何变得复杂的。