我如何从给定的日期时间结构创建碳对象?
How could i create carbon object from given datetime structure?
我使用 laravel,我需要根据我收到的 时间戳 创建 carbon 对象。
时间戳:'yy-mm-dd HH:mm'
例如。 '2016-12-20 10:26'
这可能吗?
或者任何其他解决方案?
使用Carbon::parse('2016-12-20 10:26');
,它将return一个Carbon
对象。
您可以使用 parse()
:
Carbon::parse($dateString);
或者您可以使用 $dates
属性 为列自动创建 Carbon 实例:
protected $dates = ['custom_date'];
基于carbon doc,您可以将日期字符串转换为碳对象,如:
1) Carbon::parse('1975-05-21 22:23:00.123456')
2) Carbon::create($year, $month, $day, $hour, $minute, $second, $tz);
这里是 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
我使用 laravel,我需要根据我收到的 时间戳 创建 carbon 对象。
时间戳:'yy-mm-dd HH:mm'
例如。 '2016-12-20 10:26'
这可能吗?
或者任何其他解决方案?
使用Carbon::parse('2016-12-20 10:26');
,它将return一个Carbon
对象。
您可以使用 parse()
:
Carbon::parse($dateString);
或者您可以使用 $dates
属性 为列自动创建 Carbon 实例:
protected $dates = ['custom_date'];
基于carbon doc,您可以将日期字符串转换为碳对象,如:
1) Carbon::parse('1975-05-21 22:23:00.123456')
2) Carbon::create($year, $month, $day, $hour, $minute, $second, $tz);
这里是 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