Code example for PHPExcel to import Excel files and process date cells

不言
Release: 2023-04-05 08:56:01
forward
4440 people have browsed it

This article brings you code examples about PHPExcel importing Excel files and processing date cells. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

PHPExcel imports Excel files and processes date cells in Excel

/**
 * 判断字符串是否是日期格式
 * @param $date
 * @param $format
 * @return bool
 */
function is_date($date, $format = 'Y-m-d')
{
  if (!$date || $date == '0000-00-00') return false;
  $unix_time_1 = strtotime($date);
  if (!is_numeric($unix_time_1)) return false; //非数字格式
  $format_date = date($format, $unix_time_1);
  $unix_time_2 = strtotime($format_date);
  return ($unix_time_1 == $unix_time_2);
}
/**
 * excel数据导入  日期格式化
 * @param $date
 * @return false|string
 */
function get_date_by_excel($date)
{
  if (!$date || $date == '0000-00-00') return null;

  $unix_time = \PHPExcel_Shared_Date::ExcelToPHP($date);

  return ($unix_time < 0) ? date(&#39;Y-m-d&#39;, $unix_time) : date(&#39;Y-m-d&#39;, strtotime(gmdate(&#39;Y-m-d&#39;, $unix_time)));
}
/**
 * 获取excel日期格式化结果
 * @param $date string excel日期单元格字符串
 * @param $default string  $date未非日期时返回默认日期
 * @return string
 */
function excel_date_format($date, $default = &#39;&#39;)
{
  if ($default == &#39;&#39;) $default = date(&#39;Y-m-d&#39;);
  if (is_date($date)) return $date;
  return get_date_by_excel($date) ?: $default;
}
Copy after login

The above is the detailed content of Code example for PHPExcel to import Excel files and process date cells. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!