dsgm5631 2018-11-14 19:30
浏览 318
已采纳

Laravel-Excel 2,以碳格式从“日期”获取年份和月份

In Laravel-Excel 2, I'm reading a file like this:

enter image description here

The code to read it is this:

$my_file ='some/path/to/my_file.xls';
$data = Excel::selectSheetsByIndex(0)->load($my_file, function($reader) {})->get()->toArray();

This it the output for dd($data[0]);, considering that datais an array:

enter image description here

I need to get the year (2005) and month (11) from "date". "date" is inside "fecha" and it is in Carbon format. How can I get it? I've been having a lot of problems reading that array-object thing. I thought that $data[0]["fecha"]->date would do the trick but, instead of that it works with:

$data[0]["fecha"].["date"]

But I'm having some errors in some versions of php with "Array to string conversion", but in my php it doesn't show the error, so how should I get that year and month? I don't get it.

This is the output of print_r($data[0]);die();:

Array (
    [fecha] => Carbon\Carbon Object (
        [date] => 2005-11-01 00:00:00.000000 [timezone_type] => 3 [timezone] => America/Santiago 
    ) 
    [hora] => 
    [bloquefr] => N 
    [dia] => 1 
    [activo_defecto] => 118.209 
    [reactivo_defecto] => 0 
    [empresa] => Luz Osorno 
    [barra_internal_name] => La Union 13.8 
    [barra_cen_name] => La Union 13.8 
    [barra_cne_name] => L.UNION_______013 
    [barra_cen_name_2] => La Union 1 023 
    [tipo_barra] => SPD 
)

EDIT: the output of print_r($data[0]["fecha"].["date"]);die(); is

2005-11-01 00:00:00Array

I don't know what it means that...

  • 写回答

2条回答 默认 最新

  • dqysi86208 2018-11-14 19:35
    关注

    try

    $data[0]["fecha"]->formatLocalized('Y');   // 2005
    $data[0]["fecha"]->formatLocalized('m');   // 11
    

    EDIT: the output of print_r($data[0]["fecha"].["date"]);die(); is

    2005-11-01 00:00:00Array
    

    because you are concatenating $data[0]["fecha"] and ["date"] with the operator '.' and printing it. Internally, is interpreted as:

    $data[0]["fecha"]->toString.["date"]->toString
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条