Home » Php » php – Invalid datetime format: 1292 Incorrect datetime value – Laravel 5.2

php – Invalid datetime format: 1292 Incorrect datetime value – Laravel 5.2

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m getting this error when I insert these value into my database table:

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value:
'24/06/2017' for column 'registration_from' at row 1 
(SQL: insert into `campus_registrations` (`campus_major_class_id`, 
`registration_from`, `registration_to`, `testing`, `announcement`, 
`enrollment_from`, `enrollment_to`, `updated_at`, `created_at`) values (3, 
24/06/2017, 27/06/2017, 13/07/2017, 01/08/2017, 05/09/2017, 31/01/2018,
 2017-06-07 09:39:31, 2017-06-07 09:39:31))

Do I have to intiate the datetime first or what?

How to&Answers:

The error is here:

Incorrect datetime value: '24/06/2017' for column 'registration_from' at row 1

the default format for date column in mysql is Y-m-d and for datetime is Y-m-d H:i:s. So change your date to this format and try again.

Answer:

I got the same error while trying to register user from the vue js frontend to laravel backend API.

The solution was updating the mysql strict mode to false. If anyone knows the cons of this approach, please leave your comments!

//config/database.php
'mysql' => [
    'strict' => false,
]

Answer:

Since you’re not using standard datetime format, define a mutator for each date. For example:

public function setRegistrationFromAttribute($value)
{
    $this->attributes['registration_from'] =  Carbon::parse($value);
}

Answer:

Don’t forget about Y2K38(Year 2038 problem) –

timestamp has a limit of 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC (source), whilst dateTime has a range of 1000-01-01 00:00:00 to 9999-12-31 23:59:59, so consider what you plan on storing in that field before determining which type to use.

timestamp and dateTime are similar – they store a date (YYYY-MM-DD) and time (HH:MM:SS) together in a single field i.e. YYYY-MM-DD HH:MM:SS. The difference between the two is that timestamp can use (MySQL’s) CURRENT_TIMESTAMP as its value, whenever the database record is updated. This can be handled at the database level and is great for Laravel’s created_at and updated_at fields.

date stores just the date component i.e. YYYY-MM-DD (1000-00-01 to 9999-12-31).

timestamps doesn’t take an argument, it’s a shortcut to add the created_at and updated_at timestamp fields to your database.

So setting a date on timestamp bigger than 2038-01-19 03:14:07 UTC will also throw – SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value

Answer:

I got the same problem and finally i solved the problem.Yoou can add the few line of code at the end of the add.blade.php

 $('.date-picker').datepicker({
                format: 'yy/mm/dd',
                autoclose: true,
                todayHighlight: true
            });

Answer:

Reference

MYSQL recognises date value in either YYYY-MM-DD or YY-MM-DD format. You can use /. This 24/06/2017 considered invalid. Try 2017/06/24

Answer:

  1. I’ve dropped problem column in new migration:

$table->dropColumn('time_to_deliver');

  1. created new migration file and now instead of ‘timestamp’ put in ‘time’ as the type:

$table->time('time_to_deliver')->nullable()->after('date_to_deliver');

  1. in next migration file have done next(within a loop):

$orderLcl = new Order;// a Model

$orderLcl->time_to_deliver = date("H:i",$mock_timestampLcl);

.....
$orderLcl->save();

Answer:

This was happening to me under laravel 5.6, until I changed the code to
$table->date('last_date')->default(now());