How to parse datetime using Laravel on date and time?

MisterPi picture MisterPi · Nov 2, 2016 · Viewed 22.9k times · Source

I have datetime string: 2016-11-01 15:04:19

How I can get date and time separated?

Answer

Rwd picture Rwd · Nov 2, 2016

Carbon comes with multiple setters and getters.

http://carbon.nesbot.com/docs/#api-getters

If you have a datetime string and you want to use it with a Carbon instance you can do:

$date = \Carbon\Carbon::parse('2016-11-01 15:04:19');

Then you can do something like:

$date->format('Y-m-d')
$date->format('H:i:s')

That being said, if you are getting this datetime from an Eloquent model then you should look at @AlexeyMezenin or @Christophvh answer.

Hope this helps!