首页 > 解决方案 > 为什么碳助手不起作用并给出日期时间错误?

问题描述

这个应用程序内置在 Lavavel 5 中。我有一个包含出生日期、月份和年份值的表格。我使用此查询查询表:

  $tarikh = DB::table('itemregistrations')
            ->select('itemregistrations.lahir_dd', 'itemregistrations.lahir_mm', 'itemregistrations.lahir_yy')
            ->get();

dd($塔里克);产生这个输出:

Collection {#709 ▼
#items: array:1123 [▼
0 => {#681 ▼
  +"lahir_dd": 9
  +"lahir_mm": "June"
  +"lahir_yy": 1979
}
1 => {#670 ▶}
2 => {#680 ▶}
3 => {#713 ▶}

我想使用碳计算年龄并使用数组映射作为年龄插入集合:

 $tarikh->map(function ($detail) {
        $detail->age = \Carbon\Carbon::parse($detail->lahir_yy)->diffInYears();
        return $detail;
    }); 

但是出现了这个错误:

 DateTime::__construct(): Failed to parse time string (0) at position 0 (0): Unexpected character

它突出显示了这段代码:

  parent::__construct($time, $timezone);
    if (isset($locale)) {
        setlocale(LC_NUMERIC, $locale);
    }

一位朋友帮助我使用了这个小提琴并且工作正常..但是在应用程序中,出现了错误。

 https://implode.io/i1GanD

有谁知道这个冲突错误的原因是什么?试图搜索相同的问题,但仍然没有解决方案。谢谢

标签: datetimelaravel-5php-carbonarray-map

解决方案


通过将代码更改为:

 $tarikh->map(function ($detail) {

        $detail->Umur = \Carbon\Carbon::createFromFormat('Y',$detail->lahir_yy)->diffInYears(); 
        return $detail;
    });    

推荐阅读