dongshijiao2363 2018-10-30 12:17
浏览 130
已采纳

可以格式化没有日期的日期

I usually use this to format dates:

$date = DateTime::createFromFormat('Y-m-d\TH:i', $request->id_input);

Is it possible to format a date without the date; only the time? I had tried this, from Manual PHP DateTime::createFromFormat:

$hour = DateTime::createFromFormat('H\hi\m \s', $request->sel)->format('H:i:s');

This doesn't work. It throws an error that says:

 Symfony\Component\Debug\Exception\

 FatalThrowableError (E_ERROR)

 Call to a member function format() on boolean

I have a select with some options as in the following example:

<select name="sel">
  <option value="08:00:00">08:00:00</option>
  <option value="09:00:00">09:00:00</option>
  <option value="10:00:00">10:00:00</option>
</select>

I have a select to select the time and the date is in another input.

Finally, I try to save the record into my database in MySQL. This is my controller:

public function store(Request $request){

    $schedules = new SchedulesNew();

    try {

        $schedules->id = $request->id;

        $schedules->court_id = $request->court_checkbox;

        /*___________________________________________________________*/

        $hour = DateTime::createFromFormat('H\hi\m \s', $request->sel)->format('H:i:s');
        $schedules->start_time =$request->date_input + $hour;

        $hour2 = DateTime::createFromFormat('H\h i\m s\s', $request->sel2)->format('H:i:s');
        $schedules->end_time = $request->date_input_end + $hour2;

        $schedules->state_schedule_id = $request->state_schedule_id;

        $schedules->save();



    } catch (\Illuminate\Database\QueryException $e) {

         Session::flash('error', 'There was an unknown error');

         return redirect()->route('schedules.store');

    }

        Session::flash('message', "The record was saved successfully");
        return redirect()->route('schedules.store');

}

If I don't convert the time that I received from the select, my website shows me an error that says:

A non well formed numeric value encountered

On these lines:

 $schedules->start_time =$request->date_input + $request->sel;

 $schedules->end_time = $request->date_input_2 + $request->sel2;

That is the reason that I think I need to convert to a time format, not a date format.

  • 写回答

2条回答 默认 最新

  • dpmopn8542 2018-10-30 12:26
    关注

    As you are using Laravel, try with Carbon like this:

    Carbon\Carbon::createFromFormat('H:i:s', "08:00:00");
    

    this is the response

    => Carbon\Carbon {#1226
        "date": "2018-10-30 08:00:00.000000",
        "timezone_type": 3,
        "timezone": "UTC",
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图