date.lib.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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-2015 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. return @date_default_timezone_get();
  68. }
  69. /**
  70. * Return server timezone int.
  71. *
  72. * @param string $refgmtdate Reference period for timezone (timezone differs on winter and summer. May be 'now', 'winter' or 'summer')
  73. * @return int An offset in hour (+1 for Europe/Paris on winter and +2 for Europe/Paris on summer)
  74. */
  75. function getServerTimeZoneInt($refgmtdate='now')
  76. {
  77. global $conf;
  78. if (method_exists('DateTimeZone','getOffset'))
  79. {
  80. // Method 1 (include daylight)
  81. $gmtnow=dol_now('gmt'); $yearref=dol_print_date($gmtnow,'%Y'); $monthref=dol_print_date($gmtnow,'%m'); $dayref=dol_print_date($gmtnow,'%d');
  82. if ($refgmtdate == 'now') $newrefgmtdate=$yearref.'-'.$monthref.'-'.$dayref;
  83. elseif ($refgmtdate == 'summer') $newrefgmtdate=$yearref.'-08-01';
  84. else $newrefgmtdate=$yearref.'-01-01';
  85. $newrefgmtdate.='T00:00:00+00:00';
  86. $localtz = new DateTimeZone(getServerTimeZoneString());
  87. $localdt = new DateTime($newrefgmtdate, $localtz);
  88. $tmp=-1*$localtz->getOffset($localdt);
  89. //print $refgmtdate.'='.$tmp;
  90. }
  91. else
  92. {
  93. $tmp=0;
  94. dol_print_error('','PHP version must be 5.3+');
  95. }
  96. $tz=round(($tmp<0?1:-1)*abs($tmp/3600));
  97. return $tz;
  98. }
  99. /**
  100. * Add a delay to a date
  101. *
  102. * @param int $time Date timestamp (or string with format YYYY-MM-DD)
  103. * @param int $duration_value Value of delay to add
  104. * @param int $duration_unit Unit of added delay (d, m, y, w, h)
  105. * @return int New timestamp
  106. */
  107. function dol_time_plus_duree($time,$duration_value,$duration_unit)
  108. {
  109. if ($duration_value == 0) return $time;
  110. if ($duration_unit == 'h') return $time + (3600*$duration_value);
  111. if ($duration_unit == 'w') return $time + (3600*24*7*$duration_value);
  112. if ($duration_value > 0) $deltastring="+".abs($duration_value);
  113. if ($duration_value < 0) $deltastring="-".abs($duration_value);
  114. if ($duration_unit == 'd') { $deltastring.=" day"; }
  115. if ($duration_unit == 'm') { $deltastring.=" month"; }
  116. if ($duration_unit == 'y') { $deltastring.=" year"; }
  117. return strtotime($deltastring,$time);
  118. }
  119. /**
  120. * Convert hours and minutes into seconds
  121. *
  122. * @param int $iHours Hours
  123. * @param int $iMinutes Minutes
  124. * @param int $iSeconds Seconds
  125. * @return int Time into seconds
  126. */
  127. function convertTime2Seconds($iHours=0,$iMinutes=0,$iSeconds=0)
  128. {
  129. $iResult=($iHours*3600)+($iMinutes*60)+$iSeconds;
  130. return $iResult;
  131. }
  132. /** Return, in clear text, value of a number of seconds in days, hours and minutes
  133. *
  134. * @param int $iSecond Number of seconds
  135. * @param string $format Output format ('all': total delay days hour:min like "2 days 12:30", 'allwithouthour': total delay days without hour part like "2 days", 'allhourmin': total delay with format hours:min like "60:30", 'allhour': total delay hours without min/sec like "60:30", 'fullhour': total delay hour decimal like "60.5" for 60:30, 'hour': only hours part "12", 'min': only minutes part "30", 'sec': only seconds part, 'month': only month part, 'year': only year part);
  136. * @param int $lengthOfDay Length of day (default 86400 seconds for 1 day, 28800 for 8 hour)
  137. * @param int $lengthOfWeek Length of week (default 7)
  138. * @return string Formated text of duration
  139. * Example: 0 return 00:00, 3600 return 1:00, 86400 return 1d, 90000 return 1 Day 01:00
  140. */
  141. function convertSecondToTime($iSecond, $format='all', $lengthOfDay=86400, $lengthOfWeek=7)
  142. {
  143. global $langs;
  144. if (empty($lengthOfDay)) $lengthOfDay = 86400; // 1 day = 24 hours
  145. if (empty($lengthOfWeek)) $lengthOfWeek = 7; // 1 week = 7 days
  146. if ($format == 'all' || $format == 'allwithouthour' || $format == 'allhour' || $format == 'allhourmin')
  147. {
  148. if ($iSecond === 0) return '0'; // This is to avoid having 0 return a 12:00 AM for en_US
  149. $sTime='';
  150. $sDay=0;
  151. $sWeek='';
  152. if ($iSecond >= $lengthOfDay)
  153. {
  154. for($i = $iSecond; $i >= $lengthOfDay; $i -= $lengthOfDay )
  155. {
  156. $sDay++;
  157. $iSecond-=$lengthOfDay;
  158. }
  159. $dayTranslate = $langs->trans("Day");
  160. if ($iSecond >= ($lengthOfDay*2)) $dayTranslate = $langs->trans("Days");
  161. }
  162. if ($lengthOfWeek < 7)
  163. {
  164. if ($sDay)
  165. {
  166. if ($sDay >= $lengthOfWeek)
  167. {
  168. $sWeek = (int) (($sDay - $sDay % $lengthOfWeek ) / $lengthOfWeek);
  169. $sDay = $sDay % $lengthOfWeek;
  170. $weekTranslate = $langs->trans("DurationWeek");
  171. if ($sWeek >= 2) $weekTranslate = $langs->trans("DurationWeeks");
  172. $sTime.=$sWeek.' '.$weekTranslate.' ';
  173. }
  174. }
  175. }
  176. if ($sDay>0)
  177. {
  178. $dayTranslate = $langs->trans("Day");
  179. if ($sDay > 1) $dayTranslate = $langs->trans("Days");
  180. $sTime.=$sDay.' '.$dayTranslate.' ';
  181. }
  182. if ($format == 'all')
  183. {
  184. if ($iSecond || empty($sDay))
  185. {
  186. $sTime.= dol_print_date($iSecond,'hourduration',true);
  187. }
  188. }
  189. if ($format == 'allhourmin')
  190. {
  191. return sprintf("%02d",($sWeek*$lengthOfWeek*24 + $sDay*24 + (int) floor($iSecond/3600))).':'.sprintf("%02d",((int) floor(($iSecond % 3600)/60)));
  192. }
  193. if ($format == 'allhour')
  194. {
  195. return sprintf("%02d",($sWeek*$lengthOfWeek*24 + $sDay*24 + (int) floor($iSecond/3600)));
  196. }
  197. }
  198. else if ($format == 'hour') // only hour part
  199. {
  200. $sTime=dol_print_date($iSecond,'%H',true);
  201. }
  202. else if ($format == 'fullhour')
  203. {
  204. if (!empty($iSecond)) {
  205. $iSecond=$iSecond/3600;
  206. }
  207. else {
  208. $iSecond=0;
  209. }
  210. $sTime=$iSecond;
  211. }
  212. else if ($format == 'min') // only min part
  213. {
  214. $sTime=dol_print_date($iSecond,'%M',true);
  215. }
  216. else if ($format == 'sec') // only sec part
  217. {
  218. $sTime=dol_print_date($iSecond,'%S',true);
  219. }
  220. else if ($format == 'month') // only month part
  221. {
  222. $sTime=dol_print_date($iSecond,'%m',true);
  223. }
  224. else if ($format == 'year') // only year part
  225. {
  226. $sTime=dol_print_date($iSecond,'%Y',true);
  227. }
  228. return trim($sTime);
  229. }
  230. /**
  231. * Convert a string date into a GM Timestamps date
  232. * Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not supported. If parameter gm is 1, we will use no TZ, if not we will use TZ of server, not the one inside string.
  233. *
  234. * @param string $string Date in a string
  235. * YYYYMMDD
  236. * YYYYMMDDHHMMSS
  237. * YYYYMMDDTHHMMSSZ
  238. * YYYY-MM-DDTHH:MM:SSZ (RFC3339)
  239. * DD/MM/YY or DD/MM/YYYY (deprecated)
  240. * DD/MM/YY HH:MM:SS or DD/MM/YYYY HH:MM:SS (deprecated)
  241. * @param int $gm 1 =Input date is GM date,
  242. * 0 =Input date is local date using PHP server timezone
  243. * @return int Date as a timestamp
  244. * 19700101020000 -> 7200 with gm=1
  245. *
  246. * @see dol_print_date, dol_mktime, dol_getdate
  247. */
  248. function dol_stringtotime($string, $gm=1)
  249. {
  250. // Convert date with format DD/MM/YYY HH:MM:SS. This part of code should not be used.
  251. if (preg_match('/^([0-9]+)\/([0-9]+)\/([0-9]+)\s?([0-9]+)?:?([0-9]+)?:?([0-9]+)?/i',$string,$reg))
  252. {
  253. dol_syslog("dol_stringtotime call to function with deprecated parameter format", LOG_WARNING);
  254. // Date est au format 'DD/MM/YY' ou 'DD/MM/YY HH:MM:SS'
  255. // Date est au format 'DD/MM/YYYY' ou 'DD/MM/YYYY HH:MM:SS'
  256. $sday = $reg[1];
  257. $smonth = $reg[2];
  258. $syear = $reg[3];
  259. $shour = $reg[4];
  260. $smin = $reg[5];
  261. $ssec = $reg[6];
  262. if ($syear < 50) $syear+=1900;
  263. if ($syear >= 50 && $syear < 100) $syear+=2000;
  264. $string=sprintf("%04d%02d%02d%02d%02d%02d",$syear,$smonth,$sday,$shour,$smin,$ssec);
  265. }
  266. else if (
  267. 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) // Convert date with format YYYY-MM-DDTHH:MM:SSZ (RFC3339)
  268. || preg_match('/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/i',$string,$reg) // Convert date with format YYYY-MM-DD HH:MM:SS
  269. || 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) // Convert date with format YYYYMMDDTHHMMSSZ
  270. )
  271. {
  272. $syear = $reg[1];
  273. $smonth = $reg[2];
  274. $sday = $reg[3];
  275. $shour = $reg[4];
  276. $smin = $reg[5];
  277. $ssec = $reg[6];
  278. $string=sprintf("%04d%02d%02d%02d%02d%02d",$syear,$smonth,$sday,$shour,$smin,$ssec);
  279. }
  280. $string=preg_replace('/([^0-9])/i','',$string);
  281. $tmp=$string.'000000';
  282. $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));
  283. return $date;
  284. }
  285. /** Return previous day
  286. *
  287. * @param int $day Day
  288. * @param int $month Month
  289. * @param int $year Year
  290. * @return array Previous year,month,day
  291. */
  292. function dol_get_prev_day($day, $month, $year)
  293. {
  294. $time=dol_mktime(12,0,0,$month,$day,$year,1,0);
  295. $time-=24*60*60;
  296. $tmparray=dol_getdate($time,true);
  297. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  298. }
  299. /** Return next day
  300. *
  301. * @param int $day Day
  302. * @param int $month Month
  303. * @param int $year Year
  304. * @return array Next year,month,day
  305. */
  306. function dol_get_next_day($day, $month, $year)
  307. {
  308. $time=dol_mktime(12,0,0,$month,$day,$year,1,0);
  309. $time+=24*60*60;
  310. $tmparray=dol_getdate($time,true);
  311. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  312. }
  313. /** Return previous month
  314. *
  315. * @param int $month Month
  316. * @param int $year Year
  317. * @return array Previous year,month
  318. */
  319. function dol_get_prev_month($month, $year)
  320. {
  321. if ($month == 1)
  322. {
  323. $prev_month = 12;
  324. $prev_year = $year - 1;
  325. }
  326. else
  327. {
  328. $prev_month = $month-1;
  329. $prev_year = $year;
  330. }
  331. return array('year' => $prev_year, 'month' => $prev_month);
  332. }
  333. /** Return next month
  334. *
  335. * @param int $month Month
  336. * @param int $year Year
  337. * @return array Next year,month
  338. */
  339. function dol_get_next_month($month, $year)
  340. {
  341. if ($month == 12)
  342. {
  343. $next_month = 1;
  344. $next_year = $year + 1;
  345. }
  346. else
  347. {
  348. $next_month = $month + 1;
  349. $next_year = $year;
  350. }
  351. return array('year' => $next_year, 'month' => $next_month);
  352. }
  353. /** Return previous week
  354. *
  355. * @param int $day Day
  356. * @param int $week Week
  357. * @param int $month Month
  358. * @param int $year Year
  359. * @return array Previous year,month,day
  360. */
  361. function dol_get_prev_week($day, $week, $month, $year)
  362. {
  363. $tmparray = dol_get_first_day_week($day, $month, $year);
  364. $time=dol_mktime(12,0,0,$month,$tmparray['first_day'],$year,1,0);
  365. $time-=24*60*60*7;
  366. $tmparray=dol_getdate($time,true);
  367. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  368. }
  369. /** Return next week
  370. *
  371. * @param int $day Day
  372. * @param int $week Week
  373. * @param int $month Month
  374. * @param int $year Year
  375. * @return array Next year,month,day
  376. */
  377. function dol_get_next_week($day, $week, $month, $year)
  378. {
  379. $tmparray = dol_get_first_day_week($day, $month, $year);
  380. $time=dol_mktime(12,0,0,$month,$tmparray['first_day'],$year,1,0);
  381. $time+=24*60*60*7;
  382. $tmparray=dol_getdate($time,true);
  383. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  384. }
  385. /** Return GMT time for first day of a month or year
  386. *
  387. * @param int $year Year
  388. * @param int $month Month
  389. * @param mixed $gm False or 0 or 'server' = Return date to compare with server TZ, True or 1 to compare with GM date.
  390. * 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
  391. * 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
  392. * @return int Date for first day, '' if error
  393. */
  394. function dol_get_first_day($year,$month=1,$gm=false)
  395. {
  396. if ($year > 9999) return '';
  397. return dol_mktime(0,0,0,$month,1,$year,$gm);
  398. }
  399. /** Return GMT time for last day of a month or year
  400. *
  401. * @param int $year Year
  402. * @param int $month Month
  403. * @param boolean $gm False or 0 or 'server' = Return date to compare with server TZ, True or 1 to compare with GM date.
  404. * @return int Date for first day, '' if error
  405. */
  406. function dol_get_last_day($year,$month=12,$gm=false)
  407. {
  408. if ($year > 9999) return '';
  409. if ($month == 12)
  410. {
  411. $month = 1;
  412. $year += 1;
  413. }
  414. else
  415. {
  416. $month += 1;
  417. }
  418. // On se deplace au debut du mois suivant, et on retire un jour
  419. $datelim=dol_mktime(23,59,59,$month,1,$year,$gm);
  420. $datelim -= (3600 * 24);
  421. return $datelim;
  422. }
  423. /** Return first day of week for a date. First day of week may be monday if option MAIN_START_WEEK is 1.
  424. *
  425. * @param int $day Day
  426. * @param int $month Month
  427. * @param int $year Year
  428. * @param int $gm False or 0 or 'server' = Return date to compare with server TZ, True or 1 to compare with GM date.
  429. * @return array year,month,week,first_day,first_month,first_year,prev_day,prev_month,prev_year
  430. */
  431. function dol_get_first_day_week($day,$month,$year,$gm=false)
  432. {
  433. global $conf;
  434. //$day=2; $month=2; $year=2015;
  435. $date = dol_mktime(0,0,0,$month,$day,$year,$gm);
  436. //Checking conf of start week
  437. $start_week = (isset($conf->global->MAIN_START_WEEK)?$conf->global->MAIN_START_WEEK:1);
  438. $tmparray = dol_getdate($date,true); // detail of current day
  439. //Calculate days = offset from current day
  440. $days = $start_week - $tmparray['wday'];
  441. if ($days>=1) $days=7-$days;
  442. $days = abs($days);
  443. $seconds = $days*24*60*60;
  444. //print 'start_week='.$start_week.' tmparray[wday]='.$tmparray['wday'].' day offset='.$days.' seconds offset='.$seconds.'<br>';
  445. //Get first day of week
  446. $tmpdaytms = date($tmparray[0])-$seconds; // $tmparray[0] is day of parameters
  447. $tmpday = date("d",$tmpdaytms);
  448. //Check first day of week is in same month than current day or not
  449. if ($tmpday>$day)
  450. {
  451. $prev_month = $month-1;
  452. $prev_year = $year;
  453. if ($prev_month==0)
  454. {
  455. $prev_month = 12;
  456. $prev_year = $year-1;
  457. }
  458. }
  459. else
  460. {
  461. $prev_month = $month;
  462. $prev_year = $year;
  463. }
  464. $tmpmonth = $prev_month;
  465. $tmpyear = $prev_year;
  466. //Get first day of next week
  467. $tmptime=dol_mktime(12,0,0,$month,$tmpday,$year,1,0);
  468. $tmptime-=24*60*60*7;
  469. $tmparray=dol_getdate($tmptime,true);
  470. $prev_day = $tmparray['mday'];
  471. //Check prev day of week is in same month than first day or not
  472. if ($prev_day > $tmpday)
  473. {
  474. $prev_month = $month-1;
  475. $prev_year = $year;
  476. if ($prev_month==0)
  477. {
  478. $prev_month = 12;
  479. $prev_year = $year-1;
  480. }
  481. }
  482. $week = date("W",dol_mktime(0,0,0,$tmpmonth,$tmpday,$tmpyear,$gm));
  483. return array('year' => $year, 'month' => $month, 'week' => $week, 'first_day' => $tmpday, 'first_month' => $tmpmonth, 'first_year' => $tmpyear, 'prev_year' => $prev_year, 'prev_month' => $prev_month, 'prev_day' => $prev_day);
  484. }
  485. /**
  486. * Fonction retournant le nombre de jour feries, samedis et dimanches entre 2 dates entrees en timestamp. Dates must be UTC with hour, day, min to 0
  487. * Called by function num_open_day
  488. *
  489. * @param int $timestampStart Timestamp de debut
  490. * @param int $timestampEnd Timestamp de fin
  491. * @param string $countrycode Country code
  492. * @return int Nombre de jours feries
  493. */
  494. function num_public_holiday($timestampStart, $timestampEnd, $countrycode='FR')
  495. {
  496. $nbFerie = 0;
  497. // Check to ensure we use correct parameters
  498. if ((($timestampEnd - $timestampStart) % 86400) != 0) return 'ErrorDates must use same hours and must be GMT dates';
  499. $i=0;
  500. while ($timestampStart < $timestampEnd && ($i < 50000)) // Loop end when equals (Test on i is a security loop to avoid infinite loop)
  501. {
  502. $ferie=false;
  503. $countryfound=0;
  504. $jour = date("d", $timestampStart);
  505. $mois = date("m", $timestampStart);
  506. $annee = date("Y", $timestampStart);
  507. if ($countrycode == 'FR')
  508. {
  509. $countryfound=1;
  510. // Definition des dates feriees fixes
  511. if($jour == 1 && $mois == 1) $ferie=true; // 1er janvier
  512. if($jour == 1 && $mois == 5) $ferie=true; // 1er mai
  513. if($jour == 8 && $mois == 5) $ferie=true; // 5 mai
  514. if($jour == 14 && $mois == 7) $ferie=true; // 14 juillet
  515. if($jour == 15 && $mois == 8) $ferie=true; // 15 aout
  516. if($jour == 1 && $mois == 11) $ferie=true; // 1 novembre
  517. if($jour == 11 && $mois == 11) $ferie=true; // 11 novembre
  518. if($jour == 25 && $mois == 12) $ferie=true; // 25 decembre
  519. // Calcul du jour de paques
  520. $date_paques = easter_date($annee);
  521. $jour_paques = date("d", $date_paques);
  522. $mois_paques = date("m", $date_paques);
  523. if($jour_paques == $jour && $mois_paques == $mois) $ferie=true;
  524. // Paques
  525. // Calcul du jour de l ascension (38 jours apres Paques)
  526. $date_ascension = mktime(
  527. date("H", $date_paques),
  528. date("i", $date_paques),
  529. date("s", $date_paques),
  530. date("m", $date_paques),
  531. date("d", $date_paques) + 38,
  532. date("Y", $date_paques)
  533. );
  534. $jour_ascension = date("d", $date_ascension);
  535. $mois_ascension = date("m", $date_ascension);
  536. if($jour_ascension == $jour && $mois_ascension == $mois) $ferie=true;
  537. //Ascension
  538. // Calcul de Pentecote (11 jours apres Paques)
  539. $date_pentecote = mktime(
  540. date("H", $date_ascension),
  541. date("i", $date_ascension),
  542. date("s", $date_ascension),
  543. date("m", $date_ascension),
  544. date("d", $date_ascension) + 11,
  545. date("Y", $date_ascension)
  546. );
  547. $jour_pentecote = date("d", $date_pentecote);
  548. $mois_pentecote = date("m", $date_pentecote);
  549. if($jour_pentecote == $jour && $mois_pentecote == $mois) $ferie=true;
  550. //Pentecote
  551. // Calul des samedis et dimanches
  552. $jour_julien = unixtojd($timestampStart);
  553. $jour_semaine = jddayofweek($jour_julien, 0);
  554. if($jour_semaine == 0 || $jour_semaine == 6) $ferie=true;
  555. //Samedi (6) et dimanche (0)
  556. }
  557. // Pentecoste and Ascensione in Italy go to the sunday after: isn't holiday.
  558. // Pentecoste is 50 days after Easter, Ascensione 40
  559. if ($countrycode == 'IT')
  560. {
  561. $countryfound=1;
  562. // Definition des dates feriees fixes
  563. if($jour == 1 && $mois == 1) $ferie=true; // Capodanno
  564. if($jour == 6 && $mois == 1) $ferie=true; // Epifania
  565. if($jour == 25 && $mois == 4) $ferie=true; // Anniversario Liberazione
  566. if($jour == 1 && $mois == 5) $ferie=true; // Festa del Lavoro
  567. if($jour == 2 && $mois == 6) $ferie=true; // Festa della Repubblica
  568. if($jour == 15 && $mois == 8) $ferie=true; // Ferragosto
  569. if($jour == 1 && $mois == 11) $ferie=true; // Tutti i Santi
  570. if($jour == 8 && $mois == 12) $ferie=true; // Immacolata Concezione
  571. if($jour == 25 && $mois == 12) $ferie=true; // 25 decembre
  572. if($jour == 26 && $mois == 12) $ferie=true; // Santo Stefano
  573. // Calcul du jour de paques
  574. $date_paques = easter_date($annee);
  575. $jour_paques = date("d", $date_paques);
  576. $mois_paques = date("m", $date_paques);
  577. if($jour_paques == $jour && $mois_paques == $mois) $ferie=true;
  578. // Paques
  579. // Calul des samedis et dimanches
  580. $jour_julien = unixtojd($timestampStart);
  581. $jour_semaine = jddayofweek($jour_julien, 0);
  582. if($jour_semaine == 0 || $jour_semaine == 6) $ferie=true;
  583. //Samedi (6) et dimanche (0)
  584. }
  585. if ($countrycode == 'ES')
  586. {
  587. $countryfound=1;
  588. // Definition des dates feriees fixes
  589. if($jour == 1 && $mois == 1) $ferie=true; // Año nuevo
  590. if($jour == 6 && $mois == 1) $ferie=true; // Día Reyes
  591. if($jour == 1 && $mois == 5) $ferie=true; // 1 Mayo
  592. if($jour == 15 && $mois == 8) $ferie=true; // 15 Agosto
  593. if($jour == 12 && $mois == 10) $ferie=true; // Día Hispanidad
  594. if($jour == 1 && $mois == 11) $ferie=true; // 1 noviembre
  595. if($jour == 6 && $mois == 12) $ferie=true; // Constitución
  596. if($jour == 8 && $mois == 12) $ferie=true; // Inmaculada
  597. if($jour == 25 && $mois == 12) $ferie=true; // 25 diciembre
  598. // Calcul día de Pascua
  599. $date_paques = easter_date($annee);
  600. $jour_paques = date("d", $date_paques);
  601. $mois_paques = date("m", $date_paques);
  602. if($jour_paques == $jour && $mois_paques == $mois) $ferie=true;
  603. // Paques
  604. // Viernes Santo
  605. $date_viernes = mktime(
  606. date("H", $date_paques),
  607. date("i", $date_paques),
  608. date("s", $date_paques),
  609. date("m", $date_paques),
  610. date("d", $date_paques) -2,
  611. date("Y", $date_paques)
  612. );
  613. $jour_viernes = date("d", $date_viernes);
  614. $mois_viernes = date("m", $date_viernes);
  615. if($jour_viernes == $jour && $mois_viernes == $mois) $ferie=true;
  616. //Viernes Santo
  617. // Calul des samedis et dimanches
  618. $jour_julien = unixtojd($timestampStart);
  619. $jour_semaine = jddayofweek($jour_julien, 0);
  620. if($jour_semaine == 0 || $jour_semaine == 6) $ferie=true;
  621. //Samedi (6) et dimanche (0)
  622. }
  623. // Cas pays non defini
  624. if (! $countryfound)
  625. {
  626. // Calul des samedis et dimanches
  627. $jour_julien = unixtojd($timestampStart);
  628. $jour_semaine = jddayofweek($jour_julien, 0);
  629. if($jour_semaine == 0 || $jour_semaine == 6) $ferie=true;
  630. //Samedi (6) et dimanche (0)
  631. }
  632. // On incremente compteur
  633. if ($ferie) $nbFerie++;
  634. // Increase number of days (on go up into loop)
  635. $timestampStart=dol_time_plus_duree($timestampStart, 1, 'd');
  636. //var_dump($jour.' '.$mois.' '.$annee.' '.$timestampStart);
  637. $i++;
  638. }
  639. return $nbFerie;
  640. }
  641. /**
  642. * Function to return number of days between two dates (date must be UTC date !)
  643. * Example: 2012-01-01 2012-01-02 => 1 if lastday=0, 2 if lastday=1
  644. *
  645. * @param int $timestampStart Timestamp start UTC
  646. * @param int $timestampEnd Timestamp end UTC
  647. * @param int $lastday Last day is included, 0: non, 1:oui
  648. * @return int Number of days
  649. */
  650. function num_between_day($timestampStart, $timestampEnd, $lastday=0)
  651. {
  652. if ($timestampStart < $timestampEnd)
  653. {
  654. if ($lastday == 1)
  655. {
  656. $bit = 0;
  657. }
  658. else
  659. {
  660. $bit = 1;
  661. }
  662. $nbjours = (int) floor(($timestampEnd - $timestampStart)/(60*60*24)) + 1 - $bit;
  663. }
  664. //print ($timestampEnd - $timestampStart) - $lastday;
  665. return $nbjours;
  666. }
  667. /**
  668. * Function to return number of working days (and text of units) between two dates (working days)
  669. *
  670. * @param int $timestampStart Timestamp for start date (date must be UTC to avoid calculation errors)
  671. * @param int $timestampEnd Timestamp for end date (date must be UTC to avoid calculation errors)
  672. * @param int $inhour 0: return number of days, 1: return number of hours
  673. * @param int $lastday We include last day, 0: no, 1:yes
  674. * @param int $halfday Tag to define half day when holiday start and end
  675. * @param string $country_code Country code (company country code if not defined)
  676. * @return int Number of days or hours
  677. */
  678. function num_open_day($timestampStart, $timestampEnd, $inhour=0, $lastday=0, $halfday=0, $country_code='')
  679. {
  680. global $langs,$mysoc;
  681. if (empty($country_code)) $country_code=$mysoc->country_code;
  682. dol_syslog('num_open_day timestampStart='.$timestampStart.' timestampEnd='.$timestampEnd.' bit='.$lastday.' country_code='.$country_code);
  683. // Check parameters
  684. if (! is_int($timestampStart) && ! is_float($timestampStart)) return 'ErrorBadParameter_num_open_day';
  685. if (! is_int($timestampEnd) && ! is_float($timestampEnd)) return 'ErrorBadParameter_num_open_day';
  686. //print 'num_open_day timestampStart='.$timestampStart.' timestampEnd='.$timestampEnd.' bit='.$lastday;
  687. if ($timestampStart < $timestampEnd)
  688. {
  689. $numdays = num_between_day($timestampStart, $timestampEnd, $lastday);
  690. $numholidays = num_public_holiday($timestampStart, $timestampEnd, $country_code);
  691. $nbOpenDay = $numdays - $numholidays;
  692. $nbOpenDay.= " " . $langs->trans("Days");
  693. if ($inhour == 1 && $nbOpenDay <= 3) $nbOpenDay = $nbOpenDay*24 . $langs->trans("HourShort");
  694. return $nbOpenDay - (($inhour == 1 ? 12 : 0.5) * abs($halfday));
  695. }
  696. elseif ($timestampStart == $timestampEnd)
  697. {
  698. $nbOpenDay=$lastday;
  699. if ($inhour == 1) $nbOpenDay = $nbOpenDay*24 . $langs->trans("HourShort");
  700. return $nbOpenDay - (($inhour == 1 ? 12 : 0.5) * abs($halfday));
  701. }
  702. else
  703. {
  704. return $langs->trans("Error");
  705. }
  706. }
  707. /**
  708. * Return array of translated months or selected month.
  709. * This replace old function monthArrayOrSelected.
  710. *
  711. * @param Translate $outputlangs Object langs
  712. * @param int $short 1=Return short label
  713. * @return array Month string or array if selected < 0
  714. */
  715. function monthArray($outputlangs,$short=0)
  716. {
  717. $montharray = array (
  718. 1 => $outputlangs->trans("January"),
  719. 2 => $outputlangs->trans("February"),
  720. 3 => $outputlangs->trans("March"),
  721. 4 => $outputlangs->trans("April"),
  722. 5 => $outputlangs->trans("May"),
  723. 6 => $outputlangs->trans("June"),
  724. 7 => $outputlangs->trans("July"),
  725. 8 => $outputlangs->trans("August"),
  726. 9 => $outputlangs->trans("September"),
  727. 10 => $outputlangs->trans("October"),
  728. 11 => $outputlangs->trans("November"),
  729. 12 => $outputlangs->trans("December")
  730. );
  731. if (! empty($short))
  732. {
  733. $montharray = array (
  734. 1 => $outputlangs->trans("JanuaryMin"),
  735. 2 => $outputlangs->trans("FebruaryMin"),
  736. 3 => $outputlangs->trans("MarchMin"),
  737. 4 => $outputlangs->trans("AprilMin"),
  738. 5 => $outputlangs->trans("MayMin"),
  739. 6 => $outputlangs->trans("JuneMin"),
  740. 7 => $outputlangs->trans("JulyMin"),
  741. 8 => $outputlangs->trans("AugustMin"),
  742. 9 => $outputlangs->trans("SeptemberMin"),
  743. 10 => $outputlangs->trans("OctoberMin"),
  744. 11 => $outputlangs->trans("NovemberMin"),
  745. 12 => $outputlangs->trans("DecemberMin")
  746. );
  747. }
  748. return $montharray;
  749. }