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条)

报告相同问题?

悬赏问题

  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?