当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP Excel导入日期单元格处理

PHP Excel导入日期单元格处理

2019年02月15日  | 移动技术网IT编程  | 我要评论
phpexcel导入excel文件,对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('y-m-d', $unix_time) : date('y-m-d', strtotime(gmdate('y-m-d', $unix_time)));
}

/**
 * 获取excel日期格式化结果
 * @param $date string excel日期单元格字符串
 * @param $default string  $date未非日期时返回默认日期
 * @return string
 */
function excel_date_format($date, $default = '')
{
  if ($default == '') $default = date('y-m-d');

  if (is_date($date)) return $date;

  return get_date_by_excel($date) ?: $default;
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网