date.lib.php 26 KB

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