PHP日期读取问题,我得到的是数字而不是日期 [英] PHP Date reading problem, I got a Number not a Date

查看:26
本文介绍了PHP日期读取问题,我得到的是数字而不是日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Excel文件中读取日期,但是当我打印时屏幕上的日期,我只能看到一个数字,而不是日期,为什么?

I want to read dates from an Excel File, but when I print the dates to the screen I can only see one number instead Of the date, why?

Excel文件:

结果:

推荐答案

Excel将日期视为数字,数字表示1899年12月31日之后的天数.因此,第1天是1900年1月1日,第43102天是1月3日.2018年.但是等等...您的数据显示2018年1月2日!事实证明,Microsoft认为1900年是a年,所以第60天是1900年2月29日,而实际上是3月1日.无论如何,这意味着对于1900年2月28日之后的日期,您需要减去一个从日期编号中获取正确的日期.因此,要将Excel天数转换为PHP中的日期,请使用以下代码:

Excel treats dates as numbers, with the number representing the number of days after December 31 1899. So day 1 is January 1 1900, and day 43102 is January 3 2018. But wait... your data says January 2 2018! It turns out that Microsoft thinks that 1900 was a leap year and so day 60 is February 29 1900 when in the real world it was actually March 1. Anyway, what that means is that for dates after February 28 1900, you need to subtract one from the day number to get the correct date. So, to convert an Excel day number to a date in PHP, you use the following code:

$dayval = 43102;    // you would read from your file here
$date = new DateTime('1899-12-31');
$date->modify("+$dayval day -1 day");
echo $date->format('Y-m-d');

输出:

2018-01-02

这篇关于PHP日期读取问题,我得到的是数字而不是日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆