我正在创建一个预订系统,用户A可以在数据库中插入个人可用时间。我将这些作为时间戳存储在MySql数据库中start_time
和end_time
。如果用户选择00:00:00
之间的时间来说14:00:00
,我的代码就能完美运行。我产生30分钟的间隔并存储时间。现在,当用户选择时,我可以说00:00:00
到00:00:00
或midnight
,然后我会得到正确的DatePeriod
,但是当我将其分成30分钟的时间间隔时,它会显示最后一个00:00:00
时间作为当前日期和00:00:00
,但我需要的是结束日期为下一天 //get variables
$start_date = Input::get('start');
$end_date = Input::get('end');
$startDate = new DateTime($start_date);
$endDate = new DateTime($end_date);
$interval = new DateInterval('PT30M'); //interval of 30 minutes
$available_times_date_range = new DatePeriod($startDate, $interval ,$endDate);
//store the times in array
foreach($available_times_date_range as $available_date){
$dates_and_times[] = [
'start_time' => $available_date->format("Y-m-d")." ".$available_date->format("H:i:s"),
'end_time' => $available_date->format("Y-m-d")." ".$available_date->add($interval)->format("H:i:s")
];
}
//insert to database
foreach ($dates_and_times as $date_and_time) {
$test[] = $date_and_time['end_time'];
$start_time = $date_and_time['start_time'];
$end_time = $date_and_time['end_time'];
//insert
// Nanny_availability::addAvailability($start_time, $end_time);
}
。
这就是我现在所拥有的:
json
当我从午夜到午夜选择时,我从$available_times_date_range
{
"start":{
"date":"2017-11-30 00:00:00.000000",
"timezone_type":3,
"timezone":"Europe\/Helsinki"
},
"current":{
"date":"2017-12-01 00:00:00.000000",
"timezone_type":3,
"timezone":"Europe\/Helsinki"
},
"end":{
"date":"2017-12-01 00:00:00.000000",
"timezone_type":3,
"timezone":"Europe\/Helsinki"
},
"interval":{
"y":0,
"m":0,
"d":0,
"h":0,
"i":30,
"s":0,
"weekday":0,
"weekday_behavior":0,
"first_last_day_of":0,
"invert":0,
"days":false,
"special_type":0,
"special_amount":0,
"have_weekday_relative":0,
"have_special_relative":0
},
"recurrences":1,
"include_start_date":true
}
foreach($available_times_date_range as $available_date)
但是在[
{
"start_time":"2017-11-30 00:00:00",
"end_time":"2017-11-30 00:30:00"
},
//all the other objects
{
"start_time":"2017-11-30 23:30:00",
"end_time":"2017-11-30 00:00:00"
}
]
我得到了这样的回复:
end_time
所以最后2017-12-01 00:00:00
应为public void animateSolution(Node node)
{
Stack<Node> solution = new Stack<>();
while (node != null)
{
solution.push(node);
node = node.getParent();
}
solution.pop();
while (!solution.isEmpty())
{
Node current = solution.pop();
try
{
Thread.sleep(750);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
moveBuilder(current);
}
}
。我怎样才能做到这一点?
答案 0 :(得分:2)
问题出现在您对结束时间$available_date
的修改,而不是结束日期。在将格式化日期添加到阵列之前添加间隔。
foreach($available_times_date_range as $available_date){
$dates_and_times[] = [
'start_time' => $available_date->format("Y-m-d")." ".$available_date->format("H:i:s"),
'end_time' => $available_date->add($interval)->format("Y-m-d")." ".$available_date->format("H:i:s")
];
}
就个人而言,我宁愿稍微设定结束日期/时间
foreach($available_times_date_range as $available_date){
$endTime = (clone $available_date)->add($interval);
$dates_and_times[] = [
'start_time' => $available_date->format("Y-m-d H:i:s"),
'end_time' => $endTime->format("Y-m-d H:i:s"),
];
}
(请注意,像这样的克隆需要PHP7)
答案 1 :(得分:0)
嗨,这很简单
为了计算结束日期,你可以这样做
'end_time' => $available_date->add($interval)->format("Y-m-d H:i:s")
在您的情况下,您的打印旧日期和稍后添加间隔,这是导致问题
以下更新代码应该是
//get variables
$start_date = '2017-11-30 00:00:00.000000';
$end_date = '2017-12-01 00:00:00.000000';
$startDate = new DateTime($start_date);
$endDate = new DateTime($end_date);
$interval = new DateInterval('PT30M'); //interval of 30 minutes
$available_times_date_range = new DatePeriod($startDate, $interval ,$endDate);
//store the times in array
foreach($available_times_date_range as $available_date){
$dates_and_times[] = [
'start_time' => $available_date->format("Y-m-d")." ".$available_date->format("H:i:s"),
'end_time' => $available_date->add($interval)->format("Y-m-d H:i:s")
];
}
print_r($dates_and_times);
示例输出
Array
(
[0] => Array
(
[start_time] => 2017-11-30 00:00:00
[end_time] => 2017-11-30 00:30:00
)
[1] => Array
(
[start_time] => 2017-11-30 00:30:00
[end_time] => 2017-11-30 01:00:00
)
[2] => Array
(
[start_time] => 2017-11-30 01:00:00
[end_time] => 2017-11-30 01:30:00
)
[3] => Array
(
[start_time] => 2017-11-30 01:30:00
[end_time] => 2017-11-30 02:00:00
)
[4] => Array
(
[start_time] => 2017-11-30 02:00:00
[end_time] => 2017-11-30 02:30:00
)
[5] => Array
(
[start_time] => 2017-11-30 02:30:00
[end_time] => 2017-11-30 03:00:00
)
[6] => Array
(
[start_time] => 2017-11-30 03:00:00
[end_time] => 2017-11-30 03:30:00
)
[7] => Array
(
[start_time] => 2017-11-30 03:30:00
[end_time] => 2017-11-30 04:00:00
)
[8] => Array
(
[start_time] => 2017-11-30 04:00:00
[end_time] => 2017-11-30 04:30:00
)
[9] => Array
(
[start_time] => 2017-11-30 04:30:00
[end_time] => 2017-11-30 05:00:00
)
[10] => Array
(
[start_time] => 2017-11-30 05:00:00
[end_time] => 2017-11-30 05:30:00
)
[11] => Array
(
[start_time] => 2017-11-30 05:30:00
[end_time] => 2017-11-30 06:00:00
)
[12] => Array
(
[start_time] => 2017-11-30 06:00:00
[end_time] => 2017-11-30 06:30:00
)
[13] => Array
(
[start_time] => 2017-11-30 06:30:00
[end_time] => 2017-11-30 07:00:00
)
[14] => Array
(
[start_time] => 2017-11-30 07:00:00
[end_time] => 2017-11-30 07:30:00
)
[15] => Array
(
[start_time] => 2017-11-30 07:30:00
[end_time] => 2017-11-30 08:00:00
)
[16] => Array
(
[start_time] => 2017-11-30 08:00:00
[end_time] => 2017-11-30 08:30:00
)
[17] => Array
(
[start_time] => 2017-11-30 08:30:00
[end_time] => 2017-11-30 09:00:00
)
[18] => Array
(
[start_time] => 2017-11-30 09:00:00
[end_time] => 2017-11-30 09:30:00
)
[19] => Array
(
[start_time] => 2017-11-30 09:30:00
[end_time] => 2017-11-30 10:00:00
)
[20] => Array
(
[start_time] => 2017-11-30 10:00:00
[end_time] => 2017-11-30 10:30:00
)
[21] => Array
(
[start_time] => 2017-11-30 10:30:00
[end_time] => 2017-11-30 11:00:00
)
[22] => Array
(
[start_time] => 2017-11-30 11:00:00
[end_time] => 2017-11-30 11:30:00
)
[23] => Array
(
[start_time] => 2017-11-30 11:30:00
[end_time] => 2017-11-30 12:00:00
)
[24] => Array
(
[start_time] => 2017-11-30 12:00:00
[end_time] => 2017-11-30 12:30:00
)
[25] => Array
(
[start_time] => 2017-11-30 12:30:00
[end_time] => 2017-11-30 13:00:00
)
[26] => Array
(
[start_time] => 2017-11-30 13:00:00
[end_time] => 2017-11-30 13:30:00
)
[27] => Array
(
[start_time] => 2017-11-30 13:30:00
[end_time] => 2017-11-30 14:00:00
)
[28] => Array
(
[start_time] => 2017-11-30 14:00:00
[end_time] => 2017-11-30 14:30:00
)
[29] => Array
(
[start_time] => 2017-11-30 14:30:00
[end_time] => 2017-11-30 15:00:00
)
[30] => Array
(
[start_time] => 2017-11-30 15:00:00
[end_time] => 2017-11-30 15:30:00
)
[31] => Array
(
[start_time] => 2017-11-30 15:30:00
[end_time] => 2017-11-30 16:00:00
)
[32] => Array
(
[start_time] => 2017-11-30 16:00:00
[end_time] => 2017-11-30 16:30:00
)
[33] => Array
(
[start_time] => 2017-11-30 16:30:00
[end_time] => 2017-11-30 17:00:00
)
[34] => Array
(
[start_time] => 2017-11-30 17:00:00
[end_time] => 2017-11-30 17:30:00
)
[35] => Array
(
[start_time] => 2017-11-30 17:30:00
[end_time] => 2017-11-30 18:00:00
)
[36] => Array
(
[start_time] => 2017-11-30 18:00:00
[end_time] => 2017-11-30 18:30:00
)
[37] => Array
(
[start_time] => 2017-11-30 18:30:00
[end_time] => 2017-11-30 19:00:00
)
[38] => Array
(
[start_time] => 2017-11-30 19:00:00
[end_time] => 2017-11-30 19:30:00
)
[39] => Array
(
[start_time] => 2017-11-30 19:30:00
[end_time] => 2017-11-30 20:00:00
)
[40] => Array
(
[start_time] => 2017-11-30 20:00:00
[end_time] => 2017-11-30 20:30:00
)
[41] => Array
(
[start_time] => 2017-11-30 20:30:00
[end_time] => 2017-11-30 21:00:00
)
[42] => Array
(
[start_time] => 2017-11-30 21:00:00
[end_time] => 2017-11-30 21:30:00
)
[43] => Array
(
[start_time] => 2017-11-30 21:30:00
[end_time] => 2017-11-30 22:00:00
)
[44] => Array
(
[start_time] => 2017-11-30 22:00:00
[end_time] => 2017-11-30 22:30:00
)
[45] => Array
(
[start_time] => 2017-11-30 22:30:00
[end_time] => 2017-11-30 23:00:00
)
[46] => Array
(
[start_time] => 2017-11-30 23:00:00
[end_time] => 2017-11-30 23:30:00
)
[47] => Array
(
[start_time] => 2017-11-30 23:30:00
[end_time] => 2017-12-01 00:00:00
)
)
我希望这会有所帮助