Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date from excel changes when uploaded into mysql

Tags:

php

mysql

excel

I am trying to upload an excel spreadsheet with a column that contains dates and also other text.

the date is in the format 1/24/2012 but when i load it into mysql it changes it to some decimal format e.g. 40932 ????

how can i just upload it as is, without it changing? o What can i do i'm really stuck on this one!!!!

Any help would be most appreciated.

like image 725
Sabari Avatar asked Oct 27 '25 05:10

Sabari


1 Answers

Excel holds date values as the "real" number of days since a base date, which can be either 1st January 1900 (the default for Windows versions of Excel) or 1st January 1904 (the default for Mac versions of Excel): the time is the fractional part, so midday on any given date is 0.5 greater than midnight. To add to the misery, Feb29th 1900 is a valid date for the Windows 1900 calendar.

Assuming the Windows 1900 calendar:

function ExcelToPHP($dateValue = 0) {
    $myExcelBaseDate = 25569;
    //  Adjust for the spurious 29-Feb-1900 (Day 60)
    if ($dateValue < 60) {
        --$myExcelBaseDate;
    }

    // Perform conversion
    if ($dateValue >= 1) {
        $utcDays = $dateValue - $myExcelBaseDate;
        $returnValue = round($utcDays * 86400);
        if (($returnValue <= PHP_INT_MAX) && ($returnValue >= -PHP_INT_MAX)) {
            $returnValue = (integer) $returnValue;
        }
    } else {
        $hours = round($dateValue * 24);
        $mins = round($dateValue * 1440) - round($hours * 60);
        $secs = round($dateValue * 86400) - round($hours * 3600) - round($mins * 60);
        $returnValue = (integer) gmmktime($hours, $mins, $secs);
    }

    // Return
    return $returnValue;
}   //  function ExcelToPHP()

if Mac 1904 base, replace

$myExcelBaseDate = 25569;
    //  Adjust for the spurious 29-Feb-1900 (Day 60)
    if ($dateValue < 60) {
        --$myExcelBaseDate;
}

with

$myExcelBaseDate = 24107;

This will return a PHP date/time value (standard 1970 base date), that you can then format as you wish using date();

like image 98
Mark Baker Avatar answered Oct 29 '25 20:10

Mark Baker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!