duanchuaiwan0063 2016-05-20 12:44
浏览 241

我的excel日期在php中转换为文本

My excel has start date as header and the value is 05/30/16 but when i am reading this value in php, it is saying 'MayMay/MonMon/20162016'. Why is it happening. I want real value.

$excel = new Spreadsheet_Excel_Reader();
$excel->read('sample.xls');
$x=1;
while($x<=$excel->sheets[0]['numRows']) {
  echo "\t<tr>
";
  $y=1;
  while($y<=$excel->sheets[0]['numCols']) {
    $cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
    echo "\t\t<td>$cell</td>
"; 
    $y++;
  } 
  echo "\t</tr>
";
  $x++;
}
  • 写回答

1条回答 默认 最新

  • doumor942473 2016-05-20 13:22
    关注

    To get dates, use

    $cell = isset($excel->sheets[0]['cells'][$x][$y])
        ? $excel->sheets[0]['cells'][$x][$y]['raw']
        : '';
    

    This will return the "raw" MS Excel serialized timestamp value, a number like 41944.

    An MS Excel timestamp is typically the number of days since 1st January 1900 (though it could be number of days since 1st January 1904 if the spreadsheet was created using the Mac 1904 calendar)

    You need to convert this to a Unix Timestamp in order to use PHP date functions with it.... depending on which Calendar is being used, a quick and dirty conversion:

    Windows 1900 Calendar

    $unixTimestamp = ($excelTimestamp - 25569) * 86400;
    

    and for the reverse

    $excelTimestamp = ($unixTimestamp / 86400) + 25569;
    

    Mac 1904 Calendar

    $unixTimestamp = ($excelTimestamp - 24107) * 86400;
    

    and for the reverse

    $excelTimestamp =  ($unixTimestamp / 86400) + 24107;
    

    So MS Excel 41944 => $unixTimestamp = (41944 - 25569) * 86400; giving 1414800000 which is the unix timestamp for 1st November 2014

    Once you have a unix timestamp, you can then use PHP's standard date() function or DateTime objects to manipulate and format it as you wish

    echo date('d-M-Y', $unixTimestamp);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的