简体   繁体   中英

PHP Spreadsheet_Excel_Reader - Reading Dates

I'm trying to use this class to import a large amount of data. Most of the data is being read correctly, however I have two date columns which are giving me problems.

The dates are in the format DD/MM/YYYY and the values returned are one day ahead of those in the spreadsheet. For example, 04/03/2011 becomes 05/03/2011

I have tried accessing the data like this:

$data->sheets[$sheet]['cells'][$row][$col];

I have also tried using the raw data:

$data->sheets[$sheet]['cellsInfo'][$row][$col]['raw']

Which returns the date as a unix timestamp but still it is one day ahead of what it should be.

Is there anyway I can force the class to return the value of the column as a simple string?

Any advice appreciated.

Thanks.

Spreadsheet_Excel_Reader converst the Excel numeric date/timestamp to a PHP date string using the standard PHP date() function. The numeric value held as a date/timestamp in Excel is effectively UTC, so this is likely to be a timezone issue. Try issuing a date_default_timezone_set() to UTC in your PHP script before importing the workbook.

I have the same problem, I resolve it with a little function...

$zone=dateadde("d",-1,$cell);

where : [ # enter date = 23/08/2011

 function dateadde($interval, $number, $date) 
 {
    $jour=substr("$date", 0, 2);
    $mois=substr("$date", 3, 2);
    $annee=substr("$date", 6, 4);
    $adate = mktime(0,0,0,$mois,$jour,$annee);

    $date_time_array = getdate($adate);
    $hours = $date_time_array['hours'];
    $minutes = $date_time_array['minutes'];
    $seconds = $date_time_array['seconds'];
    $month = $date_time_array['mon'];
    $day = $date_time_array['mday'];
    $year = $date_time_array['year'];
    switch ($interval) 
    {
        case 'yyyy':
            $year+=$number;
            break;
        case 'q':
            $year+=($number*3);
            break;
        case 'm':
            $month+=$number;
            break;
        case 'y':
        case 'd':
        case 'w':
            $day+=$number;
            break;
        case 'ww':
            $day+=($number*7);
            break;  
    }
    $timestamp= mktime(0,0,0,$month,$day,$year);
    $jourascii=strftime("%d/%m/%Y",$timestamp);
    return $jourascii;
 }

]

so bad editor !!

I think that the dates in file are not in Excel date format. It's posible?

Can you get the filed like string and then format another string in MM/DD/YYYY format and use strtotime function?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM