如何正确处理日期和时间? [英] How to correctly handle dates and times?

查看:93
本文介绍了如何正确处理日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模型Training中,我有一个录音.

In my model Training I have a recording.

日期培训:15/09/2019 | hour_start:18:00 | hour_end:20:00 |摩托车:000001

date training : 15/09/2019 | hour_start : 18:00 | hour_end : 20:00 | motorbike : 000001

然后,如果我在Revision中有摩托车000001,则从14:00至16:00在01/09/2019至15/09/2019.

Then, if I have the motorbike 000001 in Revision, on 01/09/2019 to 15/09/2019 from 14:00 to 16:00.

现在,例如,如果要添加格式为Training的录音:

Now, If I want to add a recording in my form Training for example :

日期培训:15/09/2019 | hour_start:08:00 | hour_end:10:00 |摩托车:000001

date training : 15/09/2019 | hour_start : 08:00 | hour_end : 10:00 | motorbike : 000001

通常,摩托车正在修订中,但是我可以在其中添加???如何创建阻止?

Normally, the motorbike is in revision, but here I can the add ??? How to create a blocking?

    public function store(Request $request)
    {
        $request->validate([
                'date_seance' => 'required',
                'hour_start' => 'required',
                'hour_end' => 'required',
                'fk_motorbike' => 'required',
                'fk_former' => 'required',
                'fk_student' => 'required',
                'fk_typeseance' => 'required'


        ]);


       $date_seance = Carbon::parse($request->get('date_seance'))->format('Y-m-d');
       $hour_start = $request->get('hour_start'); 
       $hour_end = $request->get('hour_end'); 
       $fk_motorbike = $request->get('fk_motorbike');
       $fk_student = $request->get('fk_student');
       $fk_former = $request->get('fk_former');
       $fk_typeseance = $request->get('fk_typeseance');


       $conflictTraining = Training::where('fk_motorbike', $request->get('fk_motorbike')) 
       ->whereDate('date_seance', "=" , Carbon::parse($date_seance)) 
       ->where('hour_start', "<=" , $request->get('hour_start')) 
       ->where('hour_end', ">=" , $request->get('hour_end'))
       ->where('fk_former', $request->get('fk_former'))
       ->first();

      $conflictRevision = Revision::where('fk_motorbike', $fk_motorbike)
       ->whereDate('date_revision_start', "<=" ,  Carbon::parse($date_seance)) 
       ->where('hour_start', "<=" , $request->get('hour_start')) 
       ->where('hour_end', ">=" , $request->get('hour_end'))
       ->whereDate('date_revision_end', "<=" ,  Carbon::parse($date_seance))
       ->first();

      if(isset($conflictRevision)) {
            return redirect()->route('trainings.index')
             ->with('error', 'revision');
        }

       if(isset($conflictTraining)){
            return redirect()->route('trainings.index')
             ->with('error', 'training');
        }

      else{
            Training::create($request->all());
                return redirect()->route('trainings.index')
                    ->with('success', 'Add');
        }



    }

我认为我的问题是小时吗?

I think my problem is here with the hours ?

$conflictRevision = Revision::where('fk_motorbike', $fk_motorbike)
       ->whereDate('date_revision_start', "<=" ,  Carbon::parse($date_seance)) 
       ->where('hour_start', "<=" , $request->get('hour_start')) 
       ->where('hour_end', ">=" , $request->get('hour_end'))
       ->whereDate('date_revision_end', "<=" ,  Carbon::parse($date_seance))
       ->first();

我提前想你.

推荐答案

我不确定错误是什么.您编写的代码似乎正确地允许修订中的自行车进入了新的培训.在这一行:

I'm not sure what the error is. Your code as written seems to be correctly letting the bike in revision enter a new training. In this line:

$conflictRevision = Revision::where('fk_motorbike', $fk_motorbike)
   ->whereDate('date_revision_start', "<=" ,  Carbon::parse($date_seance)) 
   ->where('hour_start', "<=" , $request->get('hour_start')) 
   ->where('hour_end', ">=" , $request->get('hour_end'))
   ->whereDate('date_revision_end', "<=" ,  Carbon::parse($date_seance))
   ->first();

您的代码要求在特定的日期范围和特定的小时范围内查找任何经过修订的自行车.根据上面的表格屏幕快照,您有一辆要求在15/9进行训练的自行车.通常,这会产生冲突消息,因为自行车正在修订中.如果您对此冲突的查询是这样的:

your code is asking to find any bike in revision within a specific date range and within a certain hour range. According to your form screenshot above, you have a bike that is requesting training on 15/9. Normally that would produce a conflict message, since the bike is in revision. If your query for this conflict was this:

$conflictRevision = Revision::where('fk_motorbike', $fk_motorbike)
   ->whereDate('date_revision_start', "<=" ,  Carbon::parse($date_seance)) 
   ->whereDate('date_revision_end', ">=" ,  Carbon::parse($date_seance))
   ->first();

由于自行车在15/9进行了修订,而您又想在15/9进行其他训练,因此if-check将产生冲突.然而!您还将时间包括在查询中.您和我都知道自行车整天在15/9进行修订.但是您进一步限制了查询,使其说告诉我所有15/9修订中的自行车,并仅告诉我它们是否在08:00至10:00之间进行了修订".这不会产生修订版的自行车,因为查询会在当天和当天的那个时间段中询问自行车.

The if-check would produce a conflict since the bike is in revision on 15/9 and you want another training on 15/9. However! You have also included the time in the query. You and i know that the bike is in revision all day on 15/9. But you have further constrained the query to say "tell me all bikes in revision on 15/9 AND only tell me if they are in revision between 08:00 and 10:00". This produces NO bikes in revision because the query asked for bikes on that day and within that time frame on that day.

解决方案1:

您可以在查询中保留时间,这将转化为我们上面所说的-即使当天进行了修订,也可以预订自行车进行训练,只要在这些小时内.

You can keep the times in the query, which would translate to what we said above - it is OK to book the bike into training even though it is in revision on that day, as long as it isn't during those hours.

解决方案2:

或者,您可以像上面我的第二个代码一样从查询中删除时间.这将转化为在当天的任何时间是否对自行车进行了修订,如果是,则阻止培训".从现实世界的角度来看,第二个似乎更合逻辑.

Or, you can remove the time from the query like in my second code above. This would translate to "is the Bike in revision at any time at all during that day, and if so, block the training". This second one seems more logical from a real world flow to me.

这篇关于如何正确处理日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆