duanpasi6287 2015-08-11 14:53 采纳率: 100%
浏览 334
已采纳

转换DateTime时遇到的格式不正确的数值

I am using the fullCalendar plugin/directive in Angular, and I am currently having an issue when trying to save the date/time into my database.

These are the values being posted to my server:

{"title":"Hey","start":"2015-08-13T00:00:00.000Z","end":"2015-08-13T00:00:00.000Z","allDay":true}

Now in my controller I try to convert both date/time string into valid date/time format before saving into my database:

public function store(ScheduleRequest $request)
{
    $schedule = new Schedules;
    $schedule->allDay = $request->allDay;
    $schedule->start = strtotime(date('Y-m-d H:i:s', $request->start));
    $schedule->end = strtotime(date('Y-m-d H:i:s', $request->end));
    $schedule->title = $request->title;

    if ($schedule->save())
    {
        return [
            'success' => 'Data Was Saved Successfully'
        ];
    }
}

This is the error I get:

A non well formed numeric value encountered

I would like to know how to convert both datetime values into valid datetime objects in PHP using the specified format.

  • 写回答

3条回答 默认 最新

  • duanchi0883649 2015-08-11 15:40
    关注

    strtotime is converting a string into a timestamp and date is converting a timestamp into a string, you need to reverse date with strtotime like so:

    public function store(ScheduleRequest $request)
    {
        $schedule = new Schedules;
        $schedule->allDay = $request->allDay;
        $schedule->start = date('Y-m-d H:i:s', strtotime($request->start));
        $schedule->end = date('Y-m-d H:i:s', strtotime($request->end));
        $schedule->title = $request->title;
    
        if ($schedule->save())
        {
            return [
                'success' => 'Data Was Saved Successfully'
            ];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1