2
votes

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
What line of code is throwing this error?disperse
Line 42, where i have $schedule->start = strtotime(...);user3718908
Ahh, your Date/Time string is incorrect, see: php.net/manual/en/datetime.formats.phpdisperse
Yes please i know that, my question is giving the string being returned to the server how do i convert that into a valid datetime object in the format i specified.user3718908
since you are using laravel, why not use the Carbon, it's loaded into laravel already, here is the docs (github.com/briannesbitt/Carbon)yangqi

3 Answers

3
votes

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

Edit: Sorry, strtotime doesn't do what I thought it did, looks like you want DateTime::createFromFormat to create a DateTime object from a String and then you can go to a unix timestamp from there.

0
votes

Adding strtotime($mydateValue) fixed it for me.