简体   繁体   中英

How to properly insert time values in mysql using php framework laravel 5.1?

Actually, I'm trying to insert some 'time' values in mysql database using laravel 5.1 eloquent. But I'm getting that ZERO time error.

I insert some time, like '06:55:00' and in the database shows '00:00:00'.

So, I want to understand the better practices to insert datetime values and avoid this kind of errors.

Here the actual code that I'm working on:

$some_var = ModelClass::create([
                           'time1' => date('H:m:s', strtotime('06:30:00')),
                           'time2' => date('H:m:s', strtotime('14:55:00'))
                          ])->pluck('id');

And I got this in the database:

id         |     time1     |      time2     |
 1         |   00:00:00    |    00:00:00    |

Thanks to samlev

Using Carbon solved my problem, and looks like a good way to work with DateTime values.

If anybody else is has problems with another kind of php framework, maybe could use it too.

just access carbon's page here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM