简体   繁体   English

如何将j / m / y日期格式更改为时间戳?

[英]How can i change j/M/y date format to timestamp?

I tried convert to timestamp but not working, My code is below 我尝试将其转换为时间戳,但无法正常工作,下面是我的代码

$date = $objPHPExcel->getActiveSheet()->getCell('A' . $x)->getFormattedValue();//output value is 9/Feb/16

echo strtotime($date);//return value is empty

It's return empty value. 它返回空值。 I want to compare Excel sheet date and PHP returned date. 我想比较Excel工作表日期和PHP返回日期。 Excel sheet returned date is like this -> 9/Feb/16 Excel工作表返回的日期是这样的-> 9 / Feb / 16

You just need to change slashes to minus symbol so strtotime will recognize you $date as date 您只需要将斜杠更改为减号,以便strtotime会将您将$ date识别为日期

$date = str_replace('/','-',$date);
echo strtotime($date); //output 1454976000

live sample

If the value in the cell is an MS Excel serialized timestamp, then you can get the "raw" value from the cell 如果单元格中的值是MS Excel序列化的时间戳,则可以从单元格中获取“原始”值

$date = $objPHPExcel->getActiveSheet()
    ->getCell('A' . $x)->getValue();

which should return a number like 42409 for 2nd February 2016 应该会在2016年2月2日传回类似42409的数字

Then use the built-in date/time conversion functions to convert that either to a unix timestamp, or to a PHP DateTime object 然后使用内置的日期/时间转换功能将其转换为Unix时间戳或PHP DateTime对象

$date = PHPExcel_Shared_Date::ExcelToPHP($date); // returns a unix timestamp
echo date('Y-m-d H:i:s', $date);

or 要么

$date = PHPExcel_Shared_Date::ExcelToPHPObject($date); // returns a DateTime object
echo $date->format('Y-m-d H:i:s');
$d = DateTime::createFromFormat('j/M/y', '9/Feb/16');
echo $d->getTimestamp();

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

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