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.
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'
];
}
}
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.
Adding strtotime($mydateValue) fixed it for me.