How to check the date type in array in PHP

2019-09-06 19:56发布

I'm using PHPExcel to read the data, and then validating the data. Problem I'm getting is all the data is validating except I don't know how to check the array if it has a date or not. Problem is not a format, problem is I'm unable to find a way to check whether it is a date or another string or integer. These are one of my value in a date cell in Excel file. i.e ;

1/2/15 17:24
1/7/15 12:00
1/9/15 11:57

2条回答
放我归山
2楼-- · 2019-09-06 20:19

For each cell you can check if this is a date :

if(PHPExcel_Shared_Date::isDateTime($cellobj)) {
    //it's a date
} 
查看更多
地球回转人心会变
3楼-- · 2019-09-06 20:20

you have to check your current version of PHP because new functions are not available in old versions. Is you date format is fixed? if yes then there is function checkdate (). it returns true on valid date.

bool checkdate ( int $month , int $day , int $year )

http://php.net/manual/en/function.checkdate.php

Checks the validity of the date formed by the arguments. A date is considered valid if each parameter is properly defined.

My suggestion is, explode() 1/2/15 17:24 data from excel and then pass it to the checkdate() function, if it returns true then this is "DATE" or this entire excel column contains DATE

查看更多
登录 后发表回答