I'm probably missing something here, but how do you create a carbon date from a MySQL date (php equivalent 'y-m-d') or datetime ('y-m-d h:i:s') string?
Do we have to use createFromFormat or use strtotime and then createFromTimestamp?
Yes, if you have a string date (ie from mysql) then you would use $instance = Carbon::createFromFormat('y-m-d', $val);. Remember the time portion defaults to now. If you want the time set to 00:00:00 you can add those to your format and $val or better yet call startOfDay() on your Carbon instance once created.
Ditto for a DateTime string, just use a different format string as you show.
If you had a DateTime instance you can create a Carbon instance with $carbon = Carbon::instance($dateTime);
Thanks for the reply. In the end, I just did Carbon::createFromTimestamp(strtotime($val)).
In that case, you can simply do $c = new \Carbon\Carbon($val);... you don't need the strtotime().
Just to correct Brian's comment further up, the code to convert from MySQL's date format would be:
$instance = Carbon::createFromFormat('Y-m-d', $val);
Notice the capital Y in Y-m-d.
Worked for me as @briannesbitt mentioned using startOfDay method after object instantiation
$carbonFromDate = Carbon::createFromFormat('Y-m-d', $fromDate)->startOfDay();
where $fromDate is a string
Most helpful comment
Thanks for the reply. In the end, I just did Carbon::createFromTimestamp(strtotime($val)).