date.lib.php 28 KB

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