Skip to content

Instantly share code, notes, and snippets.

@radbasa
Created August 11, 2014 16:09
Show Gist options
  • Save radbasa/e257696d5ebdaee75293 to your computer and use it in GitHub Desktop.
Save radbasa/e257696d5ebdaee75293 to your computer and use it in GitHub Desktop.
<?php
// ......
$value = $this->_ExcelData->sheets[ 0 ][ 'cells' ][ $i ][ $j ];
$type = $this->_TableField[ $j - 1 ][ 'type' ];
// ......
elseif ( preg_match( '/(date|datetime|timestamp)/', $type ) )
{
// date verification assume dd-mm-yyyy format
if ( preg_match( '/[0-9]{1,2}(\-|\/)[0-9]{1,2}(\-|\/)[0-9]{2,4}/', $value ) )
{
$cell_info = $this->_ExcelData->sheets[ 0 ][ 'cellsInfo' ][ $i ][ $j ];
if ( 'date' == $cell_info[ 'type' ] )
{
$valid = true;
$value = $cell_info[ 'raw' ];
$value = date( 'Y-m-d', $value );
$value = strtotime( "$value - 1 day ago" );
$value = date( 'Y-m-d', $value );
}
else
{
// dates stored as string (') in excel
// will handle dd-mm-yyyy or dd/mm/yyyy
$datesplit = preg_split( '/(\-|\/)/', $value );
$day = $datesplit[ 0 ];
$month = $datesplit[ 1 ];
$year = $datesplit[ 2 ];
if ( checkdate( $month, $day, $year ) )
{
$valid = true;
$value = $year . '-' . $month . '-' . $day;
}
else
{
$this->_Error = 741;
$this->_ErrorText[] = 'Data at row ' . $i . 'col ' . $j . ' is not a valid date ' . $day . '-' . $month . '-' . $year . '. ' . $this->_Error;
}
}
}
elseif ( is_numeric( $value ) )
{
$valid = true;
$basedate = 'January 1, 1900';
// for some reason excel stores dates as an integer reckoned from January 1, 1900 minus two days.
// will handle integers
$value -= 2;
$value = strtotime( "$basedate + $value days" );
$value = date( 'Y-m-d', $value );
}
else
{
$this->_Error = 742;
$this->_ErrorText[] = 'Data at row ' . $i . 'col ' . $j . ' does not have the correct date format (dd-mm-yyyy) ' . $day . '-' . $month . '-' . $year . '. ' . $this->_Error;
}
}
// ......
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment