简体   繁体   English

比较日期php / phpExcel

[英]Comparing dates php/phpExcel

I get a date value from an excel file and I change it to date like this 我从Excel文件中获取日期值,并且将其更改为这样的日期

$dateEx = $sheet->getCellByColumnAndRow(2,$line)->getValue();      
$date = date('Ymd',($dateEx - 25569)*24*60*60);

And I have 2 another date time that I convert from String to date 我还有2个将String转换为date的日期时间

  $dateOuverture = '20150306';
  $dateFerm = '20150906';              

  $dateOuverture = new Datetime($dateOuverture);
  $dateOuverture = $dateOuverture->format('Ymd'); 


  $dateFerm = new Datetime($dateFerm);
  $dateFerm = $dateFerm->format('Ymd'); 

and when I want to compare the date with an if it doesnt works 当我想将日期与是否比较不起作用时

if($date<=$dateFerm && $date>=$dateOuverture){
     echo "Im in the if";

}

what did I wrong? 我怎么了

Thanks for help 感谢帮助

from excel I get numbers like this 42164.536761227 instead of dates 从excel我得到像这样的数字42164.536761227而不是日期

Those numbers are MS Excel serialized timestamp values 这些数字是MS Excel序列化的时间戳记值

This is why PHPExcel provides functions to convert between MS Excel serialized timestamp values and PHP DateTime objects or Unix timestamps (and vice versa) 这就是为什么PHPExcel提供在MS Excel序列化的时间戳值与PHP DateTime对象或Unix时间戳之间进行转换的功能(反之亦然)的原因

$dateTimeObject = PHPExcel_Shared_Date::ExcelToPHP(42164.536761227);
echo $dateTimeObject->format('Y-m-d H:i:s');

or 要么

$unixTimestamp = PHPExcel_Shared_Date::ExcelToPHP(42164.536761227);
echo date('Y-m-d H:i:s', $unixTimestamp);

You can then use standard PHP functions to do any comparisons 然后,您可以使用标准PHP函数进行任何比较

PHP date functions accepts a timestamp as second argument, no ISO8601 date format. PHP日期函数接受时间戳作为第二个参数,不能使用ISO8601日期格式。 Strtotime does accept that, and returns a valid timestamp. Strtotime确实接受,并返回有效的时间戳。 Combine the two like this: 像这样结合两个:

$dateEx = $sheet->getCellByColumnAndRow(2,$line)->getValue();
$iso8601Date = ($dateEx - 25569)*24*60*60;    
$date = new DateTime($iso8601Date);

See this link for more about date formats 有关日期格式的更多信息,请参见此链接

Compare the DateTime objects 比较DateTime对象

$dateOuverture = '20150306';
$dateFerm = '20150906';

$dateOuverture = new Datetime($dateOuverture);
$dateFerm = new Datetime($dateFerm);

if($date<=$dateFerm && $date>=$dateOuverture){
    echo "Im in the if";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM