【问题标题】:Parse complex string to carbon date将复杂字符串解析为碳日期
【发布时间】:2020-12-15 03:24:57
【问题描述】:

我正在使用Laravel Framework 6.16.0 并想用碳"Dec 14 02:04 PM" 解析日期:

$fillingDate = "Dec 14 02:04 PM";
$filling_Date = Carbon::parse($fillingDate, 'UTC'); 
// result is only a string "Dec 14 02:04 PM"

当使用上述结构时,我只得到字符串。但是,我想获得一个 Carbon 对象,然后将其格式化为 ->format('Y-m-d')

任何建议我做错了什么?

感谢您的回复!

【问题讨论】:

标签: laravel php-carbon


【解决方案1】:

您可以通过调用静态函数createFromFormat从字符串创建碳对象。

在这种情况下:

$fillingDate = "Dec 14 02:04 PM";    
$filling_Date = Carbon::createFromFormat("M d g:i A", $fillingDate)

格式字符解释:

  • M - 一个月的简短文本表示,三个字母
  • d - 日期,2 位数字,前导零
  • g - 小时的 12 小时格式,不带前导零
  • i - 带前导零的分钟
  • A - 大写的 Ante meridiem 和 Post meridiem

有关格式字符的更多信息:PHP documentation

【讨论】:

    猜你喜欢
    • 2013-01-21
    • 1970-01-01
    • 2012-06-10
    • 2011-05-28
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多