如何在 Laravel 5 中以时间格式在数据库中节省时间?

Ana*_*tha 6 laravel-5.2

我一直在尝试以10:00:00.

我要的是简单地从接送时间timepicker和保存data(time)在timeformat。

这是我所做的:

  • 我用过,timepicker因为它以格式获取数据10:52 AM。

  • 我使用访问器方法来节省时间,如下所示:

    public function setRTimeAttribute($value)
    {
        $this->attributes['r_time'] = date('h:i:A', strtotime($value));
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 我的帖子控制器,以防万一,如下所示:

    public function postSessionReservation(Request $request,$profile_slug)
    {
        $restaurant = Restaurant::where('slug_profile', $profile_slug)->select('name','ar_name','slug_profile','id')->firstOrFail();
    
        $this->validate($request,[
            'no_of_seats'=>'required',
            'r_date'=>'required',
            'r_time'=>'required',
            'restaurant_id'=>'required',
        ]);
        $data = [
            'no_of_seats'=>$request->no_of_seats,
            'r_date'=>$request->r_date,
            'r_time'=>$request->r_time,
            'restaurant_id'=>$request->restaurant_id
        ];
        $minutes = 1;
    
        Session::put('reservation',json_encode($data),$minutes);
    
        return redirect($restaurant->slug_profile.'/complete-reservation');
    }
    
    Run Code Online (Sandbox Code Playgroud)

但它12:00:00每次都节省时间。

我不知道12:00:00每当我节省时间时是如何生成的。我尝试了很多,但无法弄清楚。

希望大家帮我解决这个问题。

提前致谢。

Ali*_*Ali 5

首先删除时间“10 : 52 AM”到“10:52 AM”之间的空格,然后将您的行更改为此

$this->attributes['r_time'] = date("h:i", strtotime( $value ));
Run Code Online (Sandbox Code Playgroud)

这肯定会帮助我已经测试过它。

例如,您可以通过删除空格

$a = '10 : 52 PM';
$x = preg_replace('/\s*:\s*/', ':', $a);
date("H:i", strtotime($x));
Run Code Online (Sandbox Code Playgroud)