How could i create carbon object from given datetime structure?

女生的网名这么多〃 提交于 2019-12-06 19:28:41

问题


I use laravel, i need to create carbon object from the timestamp that i received.

TimeStamp : 'yy-mm-dd HH:mm'

ex. '2016-12-20 10:26'

Is this possible ?

Or Any other solution ?


回答1:


Use Carbon::parse('2016-12-20 10:26');, it will return a Carbon object.




回答2:


Based on carbon doc, you can convert date string to carbon object like:
1) Carbon::parse('1975-05-21 22:23:00.123456')
2) Carbon::create($year, $month, $day, $hour, $minute, $second, $tz);




回答3:


You can use parse():

Carbon::parse($dateString);

Or you can use $dates property to create Carbon instance automatically for the column:

protected $dates = ['custom_date'];



回答4:


Here is the official way from the Carbon docs:

Finally, if you find yourself inheriting a \DateTime instance from another library, fear not! You can create a Carbon instance via a friendly instance() function.

$dt = new \DateTime('first day of January 2008');   // <== instance from another API
$carbon = Carbon::instance($dt);
echo get_class($carbon);                               // 'Carbon\Carbon'
echo $carbon->toDateTimeString();                      // 2008-01-01 00:00:00

Also shown here



来源:https://stackoverflow.com/questions/41236385/how-could-i-create-carbon-object-from-given-datetime-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!