PageRenderTime 30ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/lib/date.lib.php

https://bitbucket.org/speedealing/speedealing
PHP | 825 lines | 469 code | 80 blank | 276 comment | 202 complexity | 110dc69f67f4aba629344d9d6811abd1 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * or see http://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/core/lib/date.lib.php
  22. * \brief Set of function to manipulate dates
  23. */
  24. /**
  25. * Return an array with timezone values
  26. *
  27. * @return array Array with timezone values
  28. */
  29. function get_tz_array()
  30. {
  31. $tzarray=array(
  32. -11=>"Pacific/Midway",
  33. -10=>"Pacific/Fakaofo",
  34. -9=>"America/Anchorage",
  35. -8=>"America/Los_Angeles",
  36. -7=>"America/Dawson_Creek",
  37. -6=>"America/Chicago",
  38. -5=>"America/Bogota",
  39. -4=>"America/Anguilla",
  40. -3=>"America/Araguaina",
  41. -2=>"America/Noronha",
  42. -1=>"Atlantic/Azores",
  43. 0=>"Africa/Abidjan",
  44. 1=>"Europe/Paris",
  45. 2=>"Europe/Helsinki",
  46. 3=>"Europe/Moscow",
  47. 4=>"Asia/Dubai",
  48. 5=>"Asia/Karachi",
  49. 6=>"Indian/Chagos",
  50. 7=>"Asia/Jakarta",
  51. 8=>"Asia/Hong_Kong",
  52. 9=>"Asia/Tokyo",
  53. 10=>"Australia/Sydney",
  54. 11=>"Pacific/Noumea",
  55. 12=>"Pacific/Auckland",
  56. 13=>"Pacific/Enderbury"
  57. );
  58. return $tzarray;
  59. }
  60. /**
  61. * Return server timezone string
  62. *
  63. * @return string PHP server timezone string ('Europe/Paris')
  64. */
  65. function getServerTimeZoneString()
  66. {
  67. if (function_exists('date_default_timezone_get')) return date_default_timezone_get();
  68. else return '';
  69. }
  70. /**
  71. * Return server timezone int.
  72. * If $conf->global->MAIN_OLD_DATE is set or PHP too old, we use old behaviour: All convertions does not take care of dayling saving time.
  73. *
  74. * @param string $refgmtdate Reference period for timezone (timezone differs on winter and summer. May be 'now', 'winter' or 'summer')
  75. * @return int An offset in hour (+1 for Europe/Paris on winter and +2 for Europe/Paris on summer)
  76. */
  77. function getServerTimeZoneInt($refgmtdate='now')
  78. {
  79. global $conf;
  80. if (method_exists('DateTimeZone','getOffset') && empty($conf->global->MAIN_OLD_DATE))
  81. {
  82. // Method 1 (include daylight)
  83. $gmtnow=dol_now('gmt'); $yearref=dol_print_date($gmtnow,'%Y'); $monthref=dol_print_date($gmtnow,'%m'); $dayref=dol_print_date($gmtnow,'%d');
  84. if ($refgmtdate == 'now') $newrefgmtdate=$yearref.'-'.$monthref.'-'.$dayref;
  85. elseif ($refgmtdate == 'summer') $newrefgmtdate=$yearref.'-05-15';
  86. else $newrefgmtdate=$yearref.'-01-01';
  87. $localtz = new DateTimeZone(getServerTimeZoneString());
  88. $localdt = new DateTime($newrefgmtdate, $localtz);
  89. $tmp=-1*$localtz->getOffset($localdt);
  90. //print $refgmtdate.'='.$tmp;
  91. }
  92. else
  93. {
  94. // Method 2 (does not include daylight, not supported by adodb)
  95. if ($refgmtdate == 'now')
  96. {
  97. if (ini_get("date.timezone")=='UTC') return 0;
  98. // We don't know server timezone string, so we don't know location, so we can't guess daylight. We assume we use same than client. Fix is to use new PHP with not MAIN_OLD_DATE.
  99. $gmtnow=dol_now('gmt'); $yearref=dol_print_date($gmtnow,'%Y'); $monthref=dol_print_date($gmtnow,'%m'); $dayref=dol_print_date($gmtnow,'%d');
  100. if (dol_stringtotime($_SESSION['dol_dst_first']) <= $gmtnow && $gmtnow < dol_stringtotime($_SESSION['dol_dst_second'])) $daylight=1;
  101. else $daylight=0;
  102. $tmp=dol_mktime(0,0,0,$monthref,$dayref,$yearref,false,0)-dol_mktime(0,0,0,$monthref,$dayref,$yearref,true,0)-($daylight*3600);
  103. return 'unknown'; // For true result
  104. }
  105. elseif ($refgmtdate == 'summer')
  106. {
  107. if (ini_get("date.timezone")=='UTC') return 0;
  108. // We don't know server timezone string, so we don't know location, so we can't guess daylight. We assume we use same than client. Fix is to use new PHP with not MAIN_OLD_DATE.
  109. $gmtnow=dol_now('gmt'); $yearref=dol_print_date($gmtnow,'%Y'); $monthref='08'; $dayref='01';
  110. if (dol_stringtotime($_SESSION['dol_dst_first']) <= dol_stringtotime($yearref.'-'.$monthref.'-'.$dayref) && dol_stringtotime($yearref.'-'.$monthref.'-'.$dayref) < dol_stringtotime($_SESSION['dol_dst_second'])) $daylight=1;
  111. else $daylight=0;
  112. $tmp=dol_mktime(0,0,0,$monthref,$dayref,$yearref,false,0)-dol_mktime(0,0,0,$monthref,$dayref,$yearref,true,0)-($daylight*3600);
  113. return 'unknown'; // For true result
  114. }
  115. else $tmp=dol_mktime(0,0,0,1,1,1970);
  116. }
  117. $tz=round(($tmp<0?1:-1)*abs($tmp/3600));
  118. return $tz;
  119. }
  120. /**
  121. * Return server timezone string
  122. *
  123. * @return string Parent company timezone string ('Europe/Paris')
  124. *
  125. function getParentCompanyTimeZoneString()
  126. {
  127. if (function_exists('date_default_timezone_get')) return date_default_timezone_get();
  128. else return '';
  129. }
  130. */
  131. /**
  132. * Return parent company timezone int.
  133. * If $conf->global->MAIN_NEW_DATE is set, we use new behaviour: All convertions take care of dayling saving time.
  134. *
  135. * @param string $refdate Reference date for timezone (timezone differs on winter and summer)
  136. * @return int An offset in hour (+1 for Europe/Paris on winter and +2 for Europe/Paris on summer)
  137. *
  138. function getParentCompanyTimeZoneInt($refgmtdate='now')
  139. {
  140. global $conf;
  141. if (class_exists('DateTime') && empty($conf->global->MAIN_OLD_DATE))
  142. {
  143. // Method 1 (include daylight)
  144. $localtz = new DateTimeZone(getParentCompanyTimeZoneString());
  145. $localdt = new DateTime($refgmtdate, $localtz);
  146. $tmp=-1*$localtz->getOffset($localdt);
  147. }
  148. else
  149. {
  150. // Method 2 (does not include daylight)
  151. $tmp=dol_mktime(0,0,0,1,1,1970);
  152. }
  153. $tz=($tmp<0?1:-1)*abs($tmp/3600);
  154. return $tz;
  155. }*/
  156. /**
  157. * Add a delay of a timezone to a date
  158. *
  159. * @param timestamp $time Date timestamp
  160. * @param string $timezone Timezone
  161. * @return timestamp New timestamp
  162. */
  163. function dol_time_plus_timezone($time,$timezone)
  164. {
  165. // TODO Finish function
  166. return $time;
  167. }
  168. /**
  169. * Add a delay to a date
  170. *
  171. * @param timestamp $time Date timestamp (or string with format YYYY-MM-DD)
  172. * @param int $duration_value Value of delay to add
  173. * @param int $duration_unit Unit of added delay (d, m, y, w)
  174. * @return timestamp New timestamp
  175. */
  176. function dol_time_plus_duree($time,$duration_value,$duration_unit)
  177. {
  178. if ($duration_value == 0) return $time;
  179. if ($duration_value > 0) $deltastring="+".abs($duration_value);
  180. if ($duration_value < 0) $deltastring="-".abs($duration_value);
  181. if ($duration_unit == 'w') { $deltastring.=" week"; }
  182. if ($duration_unit == 'd') { $deltastring.=" day"; }
  183. if ($duration_unit == 'm') { $deltastring.=" month"; }
  184. if ($duration_unit == 'y') { $deltastring.=" year"; }
  185. return strtotime($deltastring,$time);
  186. }
  187. /**
  188. * Convert hours and minutes into seconds
  189. *
  190. * @param int $iHours Hours
  191. * @param int $iMinutes Minutes
  192. * @param int $iSeconds Seconds
  193. * @return int Time into seconds
  194. */
  195. function convertTime2Seconds($iHours=0,$iMinutes=0,$iSeconds=0)
  196. {
  197. $iResult=($iHours*3600)+($iMinutes*60)+$iSeconds;
  198. return $iResult;
  199. }
  200. /** Return, in clear text, value of a number of seconds in days, hours and minutes
  201. *
  202. * @param int $iSecond Number of seconds
  203. * @param string $format Output format (all: complete display, hour: displays only hours, min: displays only minutes, sec: displays only seconds, month: display month only, year: displays only year);
  204. * @param int $lengthOfDay Length of day (default 86400 seconds for 1 day, 28800 for 8 hour)
  205. * @param int $lengthOfWeek Length of week (default 7)
  206. * @return sTime Formated text of duration
  207. * Example: 0 return 00:00, 3600 return 1:00, 86400 return 1d, 90000 return 1 Day 01:00
  208. */
  209. function convertSecondToTime($iSecond,$format='all',$lengthOfDay=86400,$lengthOfWeek=7)
  210. {
  211. global $langs;
  212. if (empty($lengthOfDay)) $lengthOfDay = 86400; // 1 day = 24 hours
  213. if (empty($lengthOfWeek)) $lengthOfWeek = 7; // 1 week = 7 days
  214. if ($format == 'all')
  215. {
  216. if ($iSecond === 0) return '0'; // This is to avoid having 0 return a 12:00 AM for en_US
  217. $sTime='';
  218. $sDay=0;
  219. $sWeek='';
  220. if ($iSecond >= $lengthOfDay)
  221. {
  222. for($i = $iSecond; $i >= $lengthOfDay; $i -= $lengthOfDay )
  223. {
  224. $sDay++;
  225. $iSecond-=$lengthOfDay;
  226. }
  227. $dayTranslate = $langs->trans("Day");
  228. if ($iSecond >= ($lengthOfDay*2)) $dayTranslate = $langs->trans("Days");
  229. }
  230. if ($lengthOfWeek < 7)
  231. {
  232. if ($sDay)
  233. {
  234. if ($sDay >= $lengthOfWeek)
  235. {
  236. $sWeek = (int) (($sDay - $sDay % $lengthOfWeek ) / $lengthOfWeek);
  237. $sDay = $sDay % $lengthOfWeek;
  238. $weekTranslate = $langs->trans("DurationWeek");
  239. if ($sWeek >= 2) $weekTranslate = $langs->trans("DurationWeeks");
  240. $sTime.=$sWeek.' '.$weekTranslate.' ';
  241. }
  242. /* if ($sDay>0)
  243. {
  244. $dayTranslate = $langs->trans("Day");
  245. if ($sDay > 1) $dayTranslate = $langs->trans("Days");
  246. $sTime.=$sDay.' '.$dayTranslate.' ';
  247. }
  248. */
  249. }
  250. }
  251. if ($sDay>0)
  252. {
  253. $dayTranslate = $langs->trans("Day");
  254. if ($sDay > 1) $dayTranslate = $langs->trans("Days");
  255. $sTime.=$sDay.' '.$dayTranslate.' ';
  256. }
  257. // if ($sDay) $sTime.=$sDay.' '.$dayTranslate.' ';
  258. if ($iSecond || empty($sDay))
  259. {
  260. $sTime.= dol_print_date($iSecond,'hourduration',true);
  261. }
  262. }
  263. else if ($format == 'hour')
  264. {
  265. $sTime=dol_print_date($iSecond,'%H',true);
  266. }
  267. else if ($format == 'min')
  268. {
  269. $sTime=dol_print_date($iSecond,'%M',true);
  270. }
  271. else if ($format == 'sec')
  272. {
  273. $sTime=dol_print_date($iSecond,'%S',true);
  274. }
  275. else if ($format == 'month')
  276. {
  277. $sTime=dol_print_date($iSecond,'%m',true);
  278. }
  279. else if ($format == 'year')
  280. {
  281. $sTime=dol_print_date($iSecond,'%Y',true);
  282. }
  283. return trim($sTime);
  284. }
  285. /**
  286. * Convert a string date into a GM Timestamps date
  287. *
  288. * @param string $string Date in a string
  289. * YYYYMMDD
  290. * YYYYMMDDHHMMSS
  291. * YYYYMMDDTHHMMSSZ
  292. * YYYY-MM-DDTHH:MM:SSZ (RFC3339)
  293. * DD/MM/YY or DD/MM/YYYY (this format should not be used anymore)
  294. * DD/MM/YY HH:MM:SS or DD/MM/YYYY HH:MM:SS (this format should not be used anymore)
  295. * @param int $gm 1 =Input date is GM date,
  296. * 0 =Input date is local date using PHP server timezone
  297. * -1=Input date is local date using timezone provided as third parameter
  298. * @param string $tz Timezone to use. This means param $gm=-1
  299. * @return date Date
  300. * 19700101020000 -> 7200 with gm=1
  301. *
  302. * @see dol_print_date, dol_mktime, dol_getdate
  303. */
  304. function dol_stringtotime($string, $gm=1, $tz='')
  305. {
  306. // Convert date with format DD/MM/YYY HH:MM:SS. This part of code should not be used.
  307. if (preg_match('/^([0-9]+)\/([0-9]+)\/([0-9]+)\s?([0-9]+)?:?([0-9]+)?:?([0-9]+)?/i',$string,$reg))
  308. {
  309. dol_syslog("dol_stringtotime call to function with deprecated parameter", LOG_WARNING);
  310. // Date est au format 'DD/MM/YY' ou 'DD/MM/YY HH:MM:SS'
  311. // Date est au format 'DD/MM/YYYY' ou 'DD/MM/YYYY HH:MM:SS'
  312. $sday = $reg[1];
  313. $smonth = $reg[2];
  314. $syear = $reg[3];
  315. $shour = $reg[4];
  316. $smin = $reg[5];
  317. $ssec = $reg[6];
  318. if ($syear < 50) $syear+=1900;
  319. if ($syear >= 50 && $syear < 100) $syear+=2000;
  320. $string=sprintf("%04d%02d%02d%02d%02d%02d",$syear,$smonth,$sday,$shour,$smin,$ssec);
  321. }
  322. // Convert date with format RFC3339
  323. else if (preg_match('/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})Z$/i',$string,$reg))
  324. {
  325. $syear = $reg[1];
  326. $smonth = $reg[2];
  327. $sday = $reg[3];
  328. $shour = $reg[4];
  329. $smin = $reg[5];
  330. $ssec = $reg[6];
  331. $string=sprintf("%04d%02d%02d%02d%02d%02d",$syear,$smonth,$sday,$shour,$smin,$ssec);
  332. }
  333. // Convert date with format YYYYMMDDTHHMMSSZ
  334. else if (preg_match('/^([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})Z$/i',$string,$reg))
  335. {
  336. $syear = $reg[1];
  337. $smonth = $reg[2];
  338. $sday = $reg[3];
  339. $shour = $reg[4];
  340. $smin = $reg[5];
  341. $ssec = $reg[6];
  342. $string=sprintf("%04d%02d%02d%02d%02d%02d",$syear,$smonth,$sday,$shour,$smin,$ssec);
  343. }
  344. $string=preg_replace('/([^0-9])/i','',$string);
  345. $tmp=$string.'000000';
  346. $date=dol_mktime(substr($tmp,8,2),substr($tmp,10,2),substr($tmp,12,2),substr($tmp,4,2),substr($tmp,6,2),substr($tmp,0,4),($gm?1:0));
  347. if ($gm == -1)
  348. {
  349. $date=dol_time_plus_timezone($date,$tz);
  350. }
  351. return date("c",$date);
  352. }
  353. /** Return previous day
  354. *
  355. * @param int $day Day
  356. * @param int $month Month
  357. * @param int $year Year
  358. * @return array Previous year,month,day
  359. */
  360. function dol_get_prev_day($day, $month, $year)
  361. {
  362. $time=dol_mktime(12,0,0,$month,$day,$year,1,0);
  363. $time-=24*60*60;
  364. $tmparray=dol_getdate($time,true);
  365. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  366. }
  367. /** Return next day
  368. *
  369. * @param int $day Day
  370. * @param int $month Month
  371. * @param int $year Year
  372. * @return array Next year,month,day
  373. */
  374. function dol_get_next_day($day, $month, $year)
  375. {
  376. $time=dol_mktime(12,0,0,$month,$day,$year,1,0);
  377. $time+=24*60*60;
  378. $tmparray=dol_getdate($time,true);
  379. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  380. }
  381. /** Return previous month
  382. *
  383. * @param int $month Month
  384. * @param int $year Year
  385. * @return array Previous year,month
  386. */
  387. function dol_get_prev_month($month, $year)
  388. {
  389. if ($month == 1)
  390. {
  391. $prev_month = 12;
  392. $prev_year = $year - 1;
  393. }
  394. else
  395. {
  396. $prev_month = $month-1;
  397. $prev_year = $year;
  398. }
  399. return array('year' => $prev_year, 'month' => $prev_month);
  400. }
  401. /** Return next month
  402. *
  403. * @param int $month Month
  404. * @param int $year Year
  405. * @return array Next year,month
  406. */
  407. function dol_get_next_month($month, $year)
  408. {
  409. if ($month == 12)
  410. {
  411. $next_month = 1;
  412. $next_year = $year + 1;
  413. }
  414. else
  415. {
  416. $next_month = $month + 1;
  417. $next_year = $year;
  418. }
  419. return array('year' => $next_year, 'month' => $next_month);
  420. }
  421. /** Return previous week
  422. *
  423. * @param int $day Day
  424. * @param int $week Week
  425. * @param int $month Month
  426. * @param int $year Year
  427. * @return array Previous year,month,day
  428. */
  429. function dol_get_prev_week($day, $week, $month, $year)
  430. {
  431. $tmparray = dol_get_first_day_week($day, $month, $year);
  432. $time=dol_mktime(12,0,0,$month,$tmparray['first_day'],$year,1,0);
  433. $time-=24*60*60*7;
  434. $tmparray=dol_getdate($time,true);
  435. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  436. }
  437. /** Return next week
  438. *
  439. * @param int $day Day
  440. * @param int $week Week
  441. * @param int $month Month
  442. * @param int $year Year
  443. * @return array Next year,month,day
  444. */
  445. function dol_get_next_week($day, $week, $month, $year)
  446. {
  447. $tmparray = dol_get_first_day_week($day, $month, $year);
  448. $time=dol_mktime(12,0,0,$month,$tmparray['first_day'],$year,1,0);
  449. $time+=24*60*60*7;
  450. $tmparray=dol_getdate($time,true);
  451. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  452. }
  453. /** Return GMT time for first day of a month or year
  454. *
  455. * @param int $year Year
  456. * @param int $month Month
  457. * @param boolean $gm False = Return date to compare with server TZ, True to compare with GM date.
  458. * Exemple: dol_get_first_day(1970,1,false) will return -3600 with TZ+1, after a dol_print_date will return 1970-01-01 00:00:00
  459. * Exemple: dol_get_first_day(1970,1,true) will return 0 whatever is TZ, after a dol_print_date will return 1970-01-01 00:00:00
  460. * @return timestamp Date for first day
  461. */
  462. function dol_get_first_day($year,$month=1,$gm=false)
  463. {
  464. return dol_mktime(0,0,0,$month,1,$year,$gm);
  465. }
  466. /** Return GMT time for last day of a month or year
  467. *
  468. * @param int $year Year
  469. * @param int $month Month
  470. * @param boolean $gm False = Return date to compare with server TZ, True to compare with GM date.
  471. * @return timestamp Date for first day
  472. */
  473. function dol_get_last_day($year,$month=12,$gm=false)
  474. {
  475. if ($month == 12)
  476. {
  477. $month = 1;
  478. $year += 1;
  479. }
  480. else
  481. {
  482. $month += 1;
  483. }
  484. // On se deplace au debut du mois suivant, et on retire un jour
  485. $datelim=dol_mktime(23,59,59,$month,1,$year,$gm);
  486. $datelim -= (3600 * 24);
  487. return $datelim;
  488. }
  489. /** Return first day of week for a date
  490. *
  491. * @param int $day Day
  492. * @param int $month Month
  493. * @param int $year Year
  494. * @param int $gm False = Return date to compare with server TZ, True to compare with GM date.
  495. * @return array year,month, week,first_day,prev_year,prev_month,prev_day
  496. */
  497. function dol_get_first_day_week($day,$month,$year,$gm=false)
  498. {
  499. global $conf;
  500. $date = dol_mktime(0,0,0,$month,$day,$year,$gm);
  501. //Checking conf of start week
  502. $start_week = (isset($conf->global->MAIN_START_WEEK)?$conf->global->MAIN_START_WEEK:1);
  503. $tmparray = dol_getdate($date,true);
  504. //Calculate days to count
  505. $days = $start_week - $tmparray['wday'];
  506. if ($days>=1) $days=7-$days;
  507. $days = abs($days);
  508. $seconds = $days*24*60*60;
  509. //Get first day of week
  510. $tmpday = date($tmparray[0])-$seconds;
  511. $tmpday = date("d",$tmpday);
  512. //Check first day of week is form this month or not
  513. if ($tmpday>$day)
  514. {
  515. $prev_month = $month-1;
  516. $prev_year = $year;
  517. if ($prev_month==0)
  518. {
  519. $prev_month = 12;
  520. $prev_year = $year-1;
  521. }
  522. }
  523. else
  524. {
  525. $prev_month = $month;
  526. $prev_year = $year;
  527. }
  528. //Get first day of next week
  529. $tmptime=dol_mktime(12,0,0,$month,$tmpday,$year,1,0);
  530. $tmptime-=24*60*60*7;
  531. $tmparray=dol_getdate($tmptime,true);
  532. $prev_day = $tmparray['mday'];
  533. //Check first day of week is form this month or not
  534. if ($prev_day>$tmpday)
  535. {
  536. $prev_month = $month-1;
  537. $prev_year = $year;
  538. if ($prev_month==0)
  539. {
  540. $prev_month = 12;
  541. $prev_year = $year-1;
  542. }
  543. }
  544. $week = date("W",dol_mktime(0,0,0,$month,$tmpday,$year,$gm));
  545. return array('year' => $year, 'month' => $month, 'week' => $week, 'first_day' => $tmpday, 'prev_year' => $prev_year, 'prev_month' => $prev_month, 'prev_day' => $prev_day);
  546. }
  547. /**
  548. * Fonction retournant le nombre de jour feries samedis et dimanches entre 2 dates entrees en timestamp
  549. * Called by function num_open_day
  550. *
  551. * @param timestamp $timestampStart Timestamp de debut
  552. * @param timestamp $timestampEnd Timestamp de fin
  553. * @param string $countrycode Country code
  554. * @return int Nombre de jours feries
  555. */
  556. function num_public_holiday($timestampStart, $timestampEnd, $countrycode='FR')
  557. {
  558. $nbFerie = 0;
  559. while ($timestampStart != $timestampEnd)
  560. {
  561. $ferie=false;
  562. $countryfound=0;
  563. $jour = date("d", $timestampStart);
  564. $mois = date("m", $timestampStart);
  565. $annee = date("Y", $timestampStart);
  566. if ($countrycode == 'FR')
  567. {
  568. $countryfound=1;
  569. // Definition des dates feriees fixes
  570. if($jour == 1 && $mois == 1) $ferie=true; // 1er janvier
  571. if($jour == 1 && $mois == 5) $ferie=true; // 1er mai
  572. if($jour == 8 && $mois == 5) $ferie=true; // 5 mai
  573. if($jour == 14 && $mois == 7) $ferie=true; // 14 juillet
  574. if($jour == 15 && $mois == 8) $ferie=true; // 15 aout
  575. if($jour == 1 && $mois == 11) $ferie=true; // 1 novembre
  576. if($jour == 11 && $mois == 11) $ferie=true; // 11 novembre
  577. if($jour == 25 && $mois == 12) $ferie=true; // 25 decembre
  578. // Calcul du jour de paques
  579. $date_paques = easter_date($annee);
  580. $jour_paques = date("d", $date_paques);
  581. $mois_paques = date("m", $date_paques);
  582. if($jour_paques == $jour && $mois_paques == $mois) $ferie=true;
  583. // Paques
  584. // Calcul du jour de l ascension (38 jours apres Paques)
  585. $date_ascension = mktime(
  586. date("H", $date_paques),
  587. date("i", $date_paques),
  588. date("s", $date_paques),
  589. date("m", $date_paques),
  590. date("d", $date_paques) + 38,
  591. date("Y", $date_paques)
  592. );
  593. $jour_ascension = date("d", $date_ascension);
  594. $mois_ascension = date("m", $date_ascension);
  595. if($jour_ascension == $jour && $mois_ascension == $mois) $ferie=true;
  596. //Ascension
  597. // Calcul de Pentecote (11 jours apres Paques)
  598. $date_pentecote = mktime(
  599. date("H", $date_ascension),
  600. date("i", $date_ascension),
  601. date("s", $date_ascension),
  602. date("m", $date_ascension),
  603. date("d", $date_ascension) + 11,
  604. date("Y", $date_ascension)
  605. );
  606. $jour_pentecote = date("d", $date_pentecote);
  607. $mois_pentecote = date("m", $date_pentecote);
  608. if($jour_pentecote == $jour && $mois_pentecote == $mois) $ferie=true;
  609. //Pentecote
  610. // Calul des samedis et dimanches
  611. $jour_julien = unixtojd($timestampStart);
  612. $jour_semaine = jddayofweek($jour_julien, 0);
  613. if($jour_semaine == 0 || $jour_semaine == 6) $ferie=true;
  614. //Samedi (6) et dimanche (0)
  615. }
  616. // Pentecoste and Ascensione in Italy go to the sunday after: isn't holiday.
  617. // Pentecoste is 50 days after Easter, Ascensione 40
  618. if ($countrycode == 'IT')
  619. {
  620. $countryfound=1;
  621. // Definition des dates feriees fixes
  622. if($jour == 1 && $mois == 1) $ferie=true; // Capodanno
  623. if($jour == 6 && $mois == 1) $ferie=true; // Epifania
  624. if($jour == 25 && $mois == 4) $ferie=true; // Anniversario Liberazione
  625. if($jour == 1 && $mois == 5) $ferie=true; // Festa del Lavoro
  626. if($jour == 2 && $mois == 6) $ferie=true; // Festa della Repubblica
  627. if($jour == 15 && $mois == 8) $ferie=true; // Ferragosto
  628. if($jour == 1 && $mois == 11) $ferie=true; // Tutti i Santi
  629. if($jour == 8 && $mois == 12) $ferie=true; // Immacolata Concezione
  630. if($jour == 25 && $mois == 12) $ferie=true; // 25 decembre
  631. if($jour == 26 && $mois == 12) $ferie=true; // Santo Stefano
  632. // Calcul du jour de paques
  633. $date_paques = easter_date($annee);
  634. $jour_paques = date("d", $date_paques);
  635. $mois_paques = date("m", $date_paques);
  636. if($jour_paques == $jour && $mois_paques == $mois) $ferie=true;
  637. // Paques
  638. // Calul des samedis et dimanches
  639. $jour_julien = unixtojd($timestampStart);
  640. $jour_semaine = jddayofweek($jour_julien, 0);
  641. if($jour_semaine == 0 || $jour_semaine == 6) $ferie=true;
  642. //Samedi (6) et dimanche (0)
  643. }
  644. // Cas pays non defini
  645. if (! $countryfound)
  646. {
  647. // Calul des samedis et dimanches
  648. $jour_julien = unixtojd($timestampStart);
  649. $jour_semaine = jddayofweek($jour_julien, 0);
  650. if($jour_semaine == 0 || $jour_semaine == 6) $ferie=true;
  651. //Samedi (6) et dimanche (0)
  652. }
  653. // On incremente compteur
  654. if ($ferie) $nbFerie++;
  655. // Incrementation du nombre de jour (on avance dans la boucle)
  656. $jour++;
  657. $timestampStart=mktime(0,0,0,$mois,$jour,$annee);
  658. }
  659. return $nbFerie;
  660. }
  661. /**
  662. * Fonction retournant le nombre de jour entre deux dates
  663. * Example: 2012-01-01 2012-01-02 => 1 if lastday=0, 2 if lastday=1
  664. *
  665. * @param timestamp $timestampStart Timestamp de debut
  666. * @param timestamp $timestampEnd Timestamp de fin
  667. * @param int $lastday Last day is included, 0: non, 1:oui
  668. * @return int Number of days
  669. */
  670. function num_between_day($timestampStart, $timestampEnd, $lastday=0)
  671. {
  672. if ($timestampStart < $timestampEnd)
  673. {
  674. if ($lastday == 1)
  675. {
  676. $bit = 0;
  677. }
  678. else
  679. {
  680. $bit = 1;
  681. }
  682. $nbjours = (int) floor(($timestampEnd - $timestampStart)/(60*60*24)) + 1 - $bit;
  683. }
  684. //print ($timestampEnd - $timestampStart) - $lastday;
  685. return $nbjours;
  686. }
  687. /**
  688. * Fonction retournant le nombre de jour entre deux dates sans les jours feries (jours ouvres)
  689. *
  690. * @param timestamp $timestampStart Timestamp de debut
  691. * @param timestamp $timestampEnd Timestamp de fin
  692. * @param int $inhour 0: sort le nombre de jour , 1: sort le nombre d'heure (72 max)
  693. * @param int $lastday We include last day, 0: non, 1:oui
  694. * @return int Nombre de jours ou d'heures
  695. */
  696. function num_open_day($timestampStart, $timestampEnd,$inhour=0,$lastday=0)
  697. {
  698. global $langs;
  699. dol_syslog('num_open_day timestampStart='.$timestampStart.' timestampEnd='.$timestampEnd.' bit='.$lastday);
  700. //print 'num_open_day timestampStart='.$timestampStart.' timestampEnd='.$timestampEnd.' bit='.$lastday;
  701. if ($timestampStart < $timestampEnd)
  702. {
  703. //print num_between_day($timestampStart, $timestampEnd, $lastday).' - '.num_public_holiday($timestampStart, $timestampEnd);
  704. $nbOpenDay = num_between_day($timestampStart, $timestampEnd, $lastday) - num_public_holiday($timestampStart, $timestampEnd, $lastday);
  705. $nbOpenDay.= " ".$langs->trans("Days");
  706. if ($inhour == 1 && $nbOpenDay <= 3) $nbOpenDay = $nbOpenDay*24 . $langs->trans("HourShort");
  707. return $nbOpenDay;
  708. }
  709. elseif ($timestampStart == $timestampEnd)
  710. {
  711. $nbOpenDay=$lastday;
  712. if ($inhour == 1) $nbOpenDay = $nbOpenDay*24 . $langs->trans("HourShort");
  713. return $nbOpenDay=1;
  714. }
  715. else
  716. {
  717. return $langs->trans("Error");
  718. }
  719. }
  720. /**
  721. * Return array of translated months or selected month.
  722. * This replace old function monthArrayOrSelected.
  723. *
  724. * @param Translate $outputlangs Object langs
  725. * @return array Month string or array if selected < 0
  726. */
  727. function monthArray($outputlangs)
  728. {
  729. $montharray = array (
  730. 1 => $outputlangs->trans("January"),
  731. 2 => $outputlangs->trans("February"),
  732. 3 => $outputlangs->trans("March"),
  733. 4 => $outputlangs->trans("April"),
  734. 5 => $outputlangs->trans("May"),
  735. 6 => $outputlangs->trans("June"),
  736. 7 => $outputlangs->trans("July"),
  737. 8 => $outputlangs->trans("August"),
  738. 9 => $outputlangs->trans("September"),
  739. 10 => $outputlangs->trans("October"),
  740. 11 => $outputlangs->trans("November"),
  741. 12 => $outputlangs->trans("December")
  742. );
  743. return $montharray;
  744. }
  745. ?>