首页 > 开发 > PHP > 正文

PHPExcel导入Excel文件并对其日期单元格处理的代码示例

2024-05-04 22:01:48
字体:大 中 小
来源:转载
供稿:网友

本篇文章给大家带来的内容是关于PHPExcel导入Excel文件并对其日期单元格处理的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

PHPExcel导入Excel文件,对Excel中日期单元格处理:

  1. /** 
  2.  
  3.  * 判断字符串是否是日期格式 
  4.  
  5.  * @param $date 
  6.  
  7.  * @param $format 
  8.  
  9.  * @return bool 
  10.  
  11.  */ 
  12.  
  13. function is_date($date, $format = 'Y-m-d') 
  14.  
  15. { 
  16.  
  17.   if (!$date || $date == '0000-00-00') return false; 
  18.  
  19.   $unix_time_1 = strtotime($date); 
  20.  
  21.   if (!is_numeric($unix_time_1)) return false; //非数字格式 
  22.  
  23.   $format_date = date($format, $unix_time_1); 
  24.  
  25.   $unix_time_2 = strtotime($format_date); 
  26.  
  27.   return ($unix_time_1 == $unix_time_2); 
  28.  
  29. } 
  30.  
  31. /** 
  32.  
  33.  * excel数据导入  日期格式化 
  34.  
  35.  * @param $date 
  36.  
  37.  * @return false|string 
  38.  
  39.  */ 
  40.  
  41. function get_date_by_excel($date) 
  42.  
  43. { 
  44.  
  45.   if (!$date || $date == '0000-00-00') return null; 
  46.  
  47.   
  48.  
  49.   $unix_time = /PHPExcel_Shared_Date::ExcelToPHP($date); 
  50.  
  51.   
  52.  
  53.   return ($unix_time < 0) ? date('Y-m-d', $unix_time) : date('Y-m-d', strtotime(gmdate('Y-m-d', $unix_time))); 
  54.  
  55. } 
  56.  
  57. /** 
  58.  
  59.  * 获取excel日期格式化结果 
  60.  
  61.  * @param $date string excel日期单元格字符串 
  62.  
  63.  * @param $default string  $date未非日期时返回默认日期 
  64.  
  65.  * @return string 
  66.  
  67.  */ 
  68.  
  69. function excel_date_format($date, $default = '') 
  70. //Vevb.com 
  71. { 
  72.  
  73.   if ($default == '') $default = date('Y-m-d'); 
  74.  
  75.   if (is_date($date)) return $date; 
  76.  
  77.   return get_date_by_excel($date) ?: $default; 
  78.  
  79. } 

以上就是PHPExcel导入Excel文件并对其日期单元格处理的代码示例的详细内容。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表