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条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧