6
* Copyright (c) 2006 - 2009 PHPExcel
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2.1 of the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this library; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
* @package PHPExcel_Shared
24
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
25
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26
* @version 1.7.0, 2009-08-10
30
/** PHPExcel root directory */
31
if (!defined('PHPEXCEL_ROOT')) {
35
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
39
require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
41
/** PHPExcel_Style_NumberFormat */
42
require_once PHPEXCEL_ROOT . 'PHPExcel/Style/NumberFormat.php';
46
* PHPExcel_Shared_Date
49
* @package PHPExcel_Shared
50
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
52
class PHPExcel_Shared_Date
55
const CALENDAR_WINDOWS_1900 = 1900; // Base date of 1st Jan 1900 = 1.0
56
const CALENDAR_MAC_1904 = 1904; // Base date of 2nd Jan 1904 = 1.0
58
private static $ExcelBaseDate = self::CALENDAR_WINDOWS_1900;
60
public static $dateTimeObjectType = 'DateTime';
64
* Set the Excel calendar (Windows 1900 or Mac 1904)
66
* @param integer $baseDate Excel base date
67
* @return boolean Success or failure
69
public static function setExcelCalendar($baseDate) {
70
if (($baseDate == self::CALENDAR_WINDOWS_1900) ||
71
($baseDate == self::CALENDAR_MAC_1904)) {
72
self::$ExcelBaseDate = $baseDate;
76
} // function setExcelCalendar()
80
* Return the Excel calendar (Windows 1900 or Mac 1904)
82
* @return integer $baseDate Excel base date
84
public static function getExcelCalendar() {
85
return self::$ExcelBaseDate;
86
} // function getExcelCalendar()
90
* Convert a date from Excel to PHP
92
* @param long $dateValue Excel date/time value
93
* @return long PHP serialized date/time
95
public static function ExcelToPHP($dateValue = 0) {
96
if (self::$ExcelBaseDate == self::CALENDAR_WINDOWS_1900) {
97
$myExcelBaseDate = 25569;
98
// Adjust for the spurious 29-Feb-1900 (Day 60)
99
if ($dateValue < 60) {
103
$myExcelBaseDate = 24107;
106
// Perform conversion
107
if ($dateValue >= 1) {
108
$utcDays = $dateValue - $myExcelBaseDate;
109
$returnValue = (integer) round($utcDays * 24 * 60 * 60);
111
$hours = round($dateValue * 24);
112
$mins = round($dateValue * 24 * 60) - round($hours * 60);
113
$secs = round($dateValue * 24 * 60 * 60) - round($hours * 60 * 60) - round($mins * 60);
114
$returnValue = (integer) mktime($hours, $mins, $secs);
119
} // function ExcelToPHP()
123
* Convert a date from Excel to a PHP Date/Time object
125
* @param long $dateValue Excel date/time value
126
* @return long PHP date/time object
128
public static function ExcelToPHPObject($dateValue = 0) {
129
$dateTime = self::ExcelToPHP($dateValue);
130
$days = floor($dateTime / 86400);
131
$time = round((($dateTime / 86400) - $days) * 86400);
132
$hours = round($time / 3600);
133
$minutes = round($time / 60) - ($hours * 60);
134
$seconds = round($time) - ($hours * 3600) - ($minutes * 60);
135
$dateObj = date_create('1-Jan-1970+'.$days.' days');
136
$dateObj->setTime($hours,$minutes,$seconds);
138
} // function ExcelToPHPObject()
142
* Convert a date from PHP to Excel
144
* @param mixed $dateValue PHP serialized date/time or date object
145
* @return mixed Excel date/time value
146
* or boolean False on failure
148
public static function PHPToExcel($dateValue = 0) {
149
$saveTimeZone = date_default_timezone_get();
150
date_default_timezone_set('UTC');
152
if ((is_object($dateValue)) && ($dateValue instanceof self::$dateTimeObjectType)) {
153
$retValue = self::FormattedPHPToExcel( $dateValue->format('Y'), $dateValue->format('m'), $dateValue->format('d'),
154
$dateValue->format('H'), $dateValue->format('i'), $dateValue->format('s')
156
} elseif (is_numeric($dateValue)) {
157
$retValue = self::FormattedPHPToExcel( date('Y',$dateValue), date('m',$dateValue), date('d',$dateValue),
158
date('H',$dateValue), date('i',$dateValue), date('s',$dateValue)
161
date_default_timezone_set($saveTimeZone);
164
} // function PHPToExcel()
168
* FormattedPHPToExcel
174
* @param long $minutes
175
* @param long $seconds
176
* @return long Excel date/time value
178
public static function FormattedPHPToExcel($year, $month, $day, $hours=0, $minutes=0, $seconds=0) {
179
if (self::$ExcelBaseDate == self::CALENDAR_WINDOWS_1900) {
181
// Fudge factor for the erroneous fact that the year 1900 is treated as a Leap Year in MS Excel
182
// This affects every date following 28th February 1900
184
$excel1900isLeapYear = True;
185
if (($year == 1900) && ($month <= 2)) { $excel1900isLeapYear = False; }
186
$myExcelBaseDate = 2415020;
188
$myExcelBaseDate = 2416481;
189
$excel1900isLeapYear = False;
192
// Julian base date Adjustment
200
// Calculate the Julian Date, then subtract the Excel base date (JD 2415020 = 31-Dec-1899 Giving Excel Date of 0)
201
$century = substr($year,0,2);
202
$decade = substr($year,2,2);
203
$excelDate = floor((146097 * $century) / 4) + floor((1461 * $decade) / 4) + floor((153 * $month + 2) / 5) + $day + 1721119 - $myExcelBaseDate + $excel1900isLeapYear;
205
$excelTime = (($hours * 3600) + ($minutes * 60) + $seconds) / 86400;
207
return (float) $excelDate + $excelTime;
208
} // function FormattedPHPToExcel()
212
* Is a given cell a date/time?
214
* @param PHPExcel_Cell $pCell
217
public static function isDateTime(PHPExcel_Cell $pCell) {
218
return self::isDateTimeFormat($pCell->getParent()->getStyle($pCell->getCoordinate())->getNumberFormat());
219
} // function isDateTime()
223
* Is a given number format a date/time?
225
* @param PHPExcel_Style_NumberFormat $pFormat
228
public static function isDateTimeFormat(PHPExcel_Style_NumberFormat $pFormat) {
229
return self::isDateTimeFormatCode($pFormat->getFormatCode());
230
} // function isDateTimeFormat()
233
private static $possibleDateFormatCharacters = 'ymdHis';
236
* Is a given number format code a date/time?
238
* @param string $pFormatCode
241
public static function isDateTimeFormatCode($pFormatCode = '') {
242
// Switch on formatcode
243
switch ($pFormatCode) {
244
case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD:
245
case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2:
246
case PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY:
247
case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH:
248
case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYMINUS:
249
case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMMINUS:
250
case PHPExcel_Style_NumberFormat::FORMAT_DATE_MYMINUS:
251
case PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME:
252
case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME1:
253
case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME2:
254
case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3:
255
case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4:
256
case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME5:
257
case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME6:
258
case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME7:
259
case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME8:
260
case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH:
261
case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX14:
262
case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15:
263
case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX16:
264
case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX17:
265
case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX22:
269
// Try checking for any of the date formatting characters that don't appear within square braces
270
if (preg_match('/(^|\])[^\[]*['.self::$possibleDateFormatCharacters.']/i',$pFormatCode)) {
276
} // function isDateTimeFormatCode()