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

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

本文介绍了转换DateTime时遇到的格式不正确的数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular中使用了fullCalendar插件/指令,并且当我试图将日期/时间保存到数据库时,我目前遇到了问题。



这些是这些值发布到我的服务器:

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

现在在我的控制器中,我尝试将两个日期/时间字符串转换为有效的日期/时间格式,然后保存到我的数据库中:

 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;
$ b $ if if($ schedule-> save())
{
return [
'success'=> '数据保存成功'
];


$ / code $ / pre

这是我得到的错误:

我会喜欢了解如何使用指定的格式将两个日期时间值转换为PHP中的有效日期时间对象。

解决方案

strtotime正在将字符串到一个时间戳和日期是一个时间戳转换为一个字符串,你需要像这样strtotime反向日期:

  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;
$ b $ if if($ schedule-> save())
{
return [
'success'=> '数据保存成功'
];
}
}


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:

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

解决方案

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'
        ];
    }
}

这篇关于转换DateTime时遇到的格式不正确的数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 10:39