strtotime在使用值作为硬编码字符串时不返回值

时间:2015-07-10 16:04:20

标签: php strtotime import-from-excel

我正在从excel读取一些数据并将它们导入数据库。但有一个奇怪的问题。除日期部分外,一切都很好。代码部分是:

$dob = $cells[$i][6];
echo $dob.'<br>';
$timestamp = strtotime($dob);
echo 'time- '.$timestamp;
echo 'time2- '.strtotime('01-01-2000');exit;

,输出为:

01-01-2000
time- time2- 946677600

我在excel文件中使用的值与从输出中看到的01-01-2000完全相同。

我不明白为什么strtotime函数可以返回硬编码字符串版本的值,但不能从excel文件中读取变量?

感谢您的帮助。

解决方案: 如果有人面临同样的问题,这就是我处理它的方式。

$dob_temp = $cells[$i][6];
$dob_numbers = preg_replace("/[^0-9]/","",$dob_temp);
$timestamp = strtotime(substr($dob_numbers,0,2).'-'.substr($dob_numbers,2,2).'-'.substr($dob_numbers,4,4));
$dob = date('Y-m-d', $timestamp);

1 个答案:

答案 0 :(得分:2)

您从excel文件中检索的字符串可能有一些奇怪的字符 - 例如短划线而不是连字符strtotime

示例:

<?php
$dob = '01―01―2000';
echo $dob.'<br>';
$timestamp = strtotime($dob);
echo 'time- '.$timestamp;
echo 'time2- '.strtotime('01-01-2000');exit;

http://codepad.org/zKIqeHhO

您可能希望清除来自文件的日期或使用DateTime::createFromFormat之类的内容来传递与字符串内容相匹配的格式。

相关问题