保存的日期时间与我在输入时选择的不同

DateTime saved different from what i chose on input

我尝试在 fullcalendar 上添加一些事件,归档:标题、开始和结束。标题保存正确,但开始日期和结束日期在我的数据库中保存错误。谁能帮我解决这个问题?

在输入表单中我选择了 2022-01-27 - 2022-01-28

数据库保存于 1970-01-01 - 1970-01-01,时间为 00:00

var calendar = $('#calendar').fullCalendar({
        editable:true,
        header:{
            left:'prev,next today',
            center:'title',
            right:'month,agendaWeek,agendaDay'
        },
        events:'/event-calender',
        selectable:true,
        selectHelper: true,
        //When u select some space in the calendar do the following:
        select: function (start, end, allDay) {
            //do something when space selected
            //Show 'add event' modal
            $('#eventModal').modal('show');
        },

  $('#eventSubmit').on('click', function(e){
        // We don't want this to act as a link so cancel the link action
        e.preventDefault();

        doSubmit();
    });
    $('#eventClose').on('click', function(e) {
        $('#eventModal').modal('hide');
    });

    function doSubmit(){
        
        $("#eventModal").modal('hide');
        var start = $.fullCalendar.formatDate(start, 'Y-MM-DD HH:mm:ss');
        var end = $.fullCalendar.formatDate(end, 'Y-MM-DD HH:mm:ss');
        $("#calendar").fullCalendar('renderEvent',
           $.ajax ({
                url:"/event-calender/action",
                type:"POST",
                data:{
                    title: $('#title').val(),
                    start: start,
                    end: end,
                    type: 'add',
                }
            }),
            true);
       }

这是控制器

public function action(Request $request)
    {
        if($request->ajax())
        {
            if($request->type == 'add')
            {
                $event = Events::create([
                    'title'     =>  $request->title,
                    'start'     =>  $request->start,
                    'end'       =>  $request->end
                ]);
                // dd($event);
                return response()->json($event);
            }
        }
    }

事件模式

class Events extends Model
{
    protected $table = 'events';

    protected $fillable = [
        'title',
        'start',
        'end'
    ];
}

我更改了 doSubmit() ,现在又出现了这样的错误 Uncaught TypeError: Cannot read properties of undefined (reading 'isValid')

你可以尝试使用Carbon.

use Carbon\Carbon;


...

$event = Events::create([
    'title' => $request->title,
    'start  => Carbon::parse($request->start),
    'end' => Carbon::parse($request->end),
]);

start和/或end允许为空时,可以使用Carbon::make()代替Carbon::parse()

另一种选择是使用 Carbon::createFromFormat('Y-m-d', $request->start);

此外,请确保 startend 已添加到 Events 模型上的 $fillable 属性。

回答

//When u select some space in the calendar do the following:
        select: function (start, end, allDay) {
            //do something when space selected
            //Show 'add event' modal
            $('#eventModal').modal('show');
            var start = $.fullCalendar.formatDate(start, 'Y-MM-DD HH:mm:ss');
            var end = $.fullCalendar.formatDate(end, 'Y-MM-DD HH:mm:ss');

            $('#eventSubmit').on('click', function(e){
                // We don't want this to act as a link so cancel the link action
                e.preventDefault();

                doSubmit();
            });

            $('#eventClose').on('click', function(e) {
                $('#eventModal').modal('hide');
            });

            function doSubmit(){
                
                $("#eventModal").modal('hide');
                $("#calendar").fullCalendar('renderEvent',
                $.ajax ({
                        url:"/event-calender/action",
                        type:"POST",
                        data:{
                            title: $('#title').val(),
                            start: start,
                            end: end,
                            type: 'add',
                        },
                        success:function(data)
                        {
                            calendar.fullCalendar('refetchEvents');
                            // Swal.fire("Event Created Successfully");
                        }
                    }),
                true);
            }
        },