laravel 我如何从给定的日期时间结构创建碳对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41236385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How could i create carbon object from given datetime structure?
提问by Shankar Thiyagaraajan
I use laravel, i need to create carbon objectfrom the timestampthat i received.
我使用laravel,我需要根据收到的时间戳创建碳对象。
TimeStamp : 'yy-mm-dd HH:mm'
时间戳:' yy-mm-dd HH:mm'
ex. '2016-12-20 10:26'
前任。' 2016-12-20 10:26'
Is this possible ?
这可能吗 ?
Or Any other solution ?
或任何其他解决方案?
回答by SteD
Use Carbon::parse('2016-12-20 10:26');
, it will return a Carbon
object.
使用Carbon::parse('2016-12-20 10:26');
,它会返回一个Carbon
对象。
回答by AddWeb Solution Pvt Ltd
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);
基于carbon doc,您可以将日期字符串转换为 carbon 对象,例如:
1)Carbon::parse('1975-05-21 22:23:00.123456')
2)Carbon::create($year, $month, $day, $hour, $minute, $second, $tz);
回答by Alexey Mezenin
You can use parse()
:
您可以使用parse()
:
Carbon::parse($dateString);
Or you can use $dates
property to create Carbon instance automatically for the column:
或者您可以使用$dates
属性为列自动创建 Carbon 实例:
protected $dates = ['custom_date'];
回答by Ryan
Here is the official way from the Carbon docs:
这是来自 Carbon 文档的官方方法:
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.
最后,如果您发现自己从另一个库继承了一个 \DateTime 实例,请不要害怕!您可以通过友好的 instance() 函数创建一个 Carbon 实例。
$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