2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3718908x100
  • 7,939
  • 15
  • 64
  • 123
  • What line of code is throwing this error? – disperse Aug 11 '15 at 14:59
  • Line 42, where i have $schedule->start = strtotime(...); – user3718908x100 Aug 11 '15 at 15:01
  • Ahh, your Date/Time string is incorrect, see: http://php.net/manual/en/datetime.formats.php – disperse Aug 11 '15 at 15:03
  • 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. – user3718908x100 Aug 11 '15 at 15:06
  • This has been answered before, look here: http://stackoverflow.com/questions/8405087/what-is-this-date-format-2011-08-12t201746-384z – disperse Aug 11 '15 at 15:06
  • 1
    since you are using laravel, why not use the Carbon, it's loaded into laravel already, here is the docs (https://github.com/briannesbitt/Carbon) – yangqi Aug 11 '15 at 15:10
  • @disperse Yes looks like it but that is in Java and uses "SimpleDateFormat' which i am not familiar with, how would i do this using PHP's strtotime and date functions? I have no idea how to handle the 'T' and the 'Z'. – user3718908x100 Aug 11 '15 at 15:10
  • Assuming you want to store the date in your database as a unix timestamp, the answer I gave below should work. – disperse Aug 11 '15 at 15:16
  • @yangqi thing is i am not familiar with that format if i use carbon as you suggested how then would i achieve what i intend to do? – user3718908x100 Aug 11 '15 at 15:17
  • @user3718908 ok, looks like you don't need to use Carbon. just use `strtotime($request->end)`, it converts string to date, you don't need to use date() at all. – yangqi Aug 11 '15 at 15:28

3 Answers3

3

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

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.

disperse
  • 1,216
  • 10
  • 22
0

Adding strtotime($mydateValue) fixed it for me.

Iyke Perry
  • 341
  • 4
  • 2