How to convert custome value in excel to date value or text value with PHP -
i have excel sheet "custom" field present formula
[$-14009]dd-mm-yyyy hh:mm:ss for holding date value. when import excel mysql, value converted general value 41841.203275. please give suggestion how convert excel value mysql value.
that value ms excel serialized timestamp, number of days since 1-jan-1900 (or 1-jan-1904 if using excel mac calendar).
i don't know how you're reading spreadsheet, (taken directly phpexcel date handling code):
public static function exceltophp($datevalue = 0) { if (self::$excelbasedate == self::calendar_windows_1900) { $myexcelbasedate = 25569; // adjust spurious 29-feb-1900 (day 60) if ($datevalue < 60) { --$myexcelbasedate; } } else { $myexcelbasedate = 24107; } // 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; } set self::$excelbasedate == self::calendar_windows_1900 necessary indicate excel base calendar you're using: windows 1900 or mac 1904
you have unix timestamp value can use of php's date functions
and if want php datetime object instead:
public static function exceltophpobject($datevalue = 0) { $datetime = self::exceltophp($datevalue); $days = floor($datetime / 86400); $time = round((($datetime / 86400) - $days) * 86400); $hours = round($time / 3600); $minutes = round($time / 60) - ($hours * 60); $seconds = round($time) - ($hours * 3600) - ($minutes * 60); $dateobj = date_create('1-jan-1970+'.$days.' days'); $dateobj->settime($hours,$minutes,$seconds); return $dateobj; }
Comments
Post a Comment