6

In my controller, when I create an event, it saves perfectly fine. The user enters a date in dd-mm-yyyy and it gets saved in MySQL DATETIME format. Then the details view renders it completely fine, just like the edit view via model binding. As soon as I try to save from the edit form, the date somehow fails and returns 1970-01-01 00:00:00.

I am not really sure why this only happens on my update method, as my store method is in essence the same.

$input = Input::all();
$input['plannedTime'] = date('Y-m-d H:i:s', strtotime(Input::get('plannedTime')));

How comes the update method returns no error, validation is alright, but it still won't save it correctly?

2
  • What's the value of Input::get('plannedTime')? Commented Apr 15, 2015 at 19:31
  • The value of 'plannedTime' is a string of date format d-m-Y H:i:s. Commented Apr 15, 2015 at 19:33

2 Answers 2

12

The value of 'plannedTime' is a string of date format d-m-Y H:i:s.

There's your problem. PHP's strtotime does its best, but 04-05-2015 00:00:00 could be either April 5 or May 4, depending where you live.

As an example, on my install, strtotime('04-13-2014 00:00:00') fails (which'll get converted to 0, which'll get converted to 1970-01-01).

Laravel's date validation expects a value that strtotime can handle. If you're using an ambiguous date format, use createFromFormat to parse it, then format to spit it out in a more standard format.

$date = DateTime::createFromFormat('d-m-Y H:i:s', Input::get('plannedTime'));
$usableDate = $date->format('Y-m-d H:i:s');
Sign up to request clarification or add additional context in comments.

2 Comments

Changed code to be as so: $date = DateTime::createFromFormat('d-m-Y H:i:s', Input::get('plannedTime')); $input['plannedTime'] = $date->format('Y-m-d H:i:s'); Receiving now Call to a member function format() on boolean ($date in this case).
If $date is a boolean Input::get('plannedTime') was not d-m-Y H:i:s.
1

MySQL never returns any error for invalid dates as far as i can remember, it just sets it to EPOCH and thats it!

You should enforce your dates to be always converted to a "Y-m-d H:i:s" format when communicating with your site and display the date in "d-m-Y" only on the output side.

You should try and use

public static DateTime DateTime::createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] )

To create a datetime object from a format. If you know your format such as in this case, then provide the format and get a date object back which will be persisted correctly!

1 Comment

Thank you for your quick answer, Mathieu! Now, when I am running the edited plannedTime through the validation in my model, I receive an error 'the planned time is not a valid date'. My model describes 'plannedTime' as 'required|date'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.