date.lib.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. <?php
  2. /* Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2011-2015 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2017 Ferran Marcet <fmarcet@2byte.es>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. * or see http://www.gnu.org/
  20. */
  21. /**
  22. * \file htdocs/core/lib/date.lib.php
  23. * \brief Set of function to manipulate dates
  24. */
  25. /**
  26. * Return an array with timezone values
  27. *
  28. * @return array Array with timezone values
  29. */
  30. function get_tz_array()
  31. {
  32. $tzarray=array(
  33. -11=>"Pacific/Midway",
  34. -10=>"Pacific/Fakaofo",
  35. -9=>"America/Anchorage",
  36. -8=>"America/Los_Angeles",
  37. -7=>"America/Dawson_Creek",
  38. -6=>"America/Chicago",
  39. -5=>"America/Bogota",
  40. -4=>"America/Anguilla",
  41. -3=>"America/Araguaina",
  42. -2=>"America/Noronha",
  43. -1=>"Atlantic/Azores",
  44. 0=>"Africa/Abidjan",
  45. 1=>"Europe/Paris",
  46. 2=>"Europe/Helsinki",
  47. 3=>"Europe/Moscow",
  48. 4=>"Asia/Dubai",
  49. 5=>"Asia/Karachi",
  50. 6=>"Indian/Chagos",
  51. 7=>"Asia/Jakarta",
  52. 8=>"Asia/Hong_Kong",
  53. 9=>"Asia/Tokyo",
  54. 10=>"Australia/Sydney",
  55. 11=>"Pacific/Noumea",
  56. 12=>"Pacific/Auckland",
  57. 13=>"Pacific/Enderbury"
  58. );
  59. return $tzarray;
  60. }
  61. /**
  62. * Return server timezone string
  63. *
  64. * @return string PHP server timezone string ('Europe/Paris')
  65. */
  66. function getServerTimeZoneString()
  67. {
  68. return @date_default_timezone_get();
  69. }
  70. /**
  71. * Return server timezone int.
  72. *
  73. * @param string $refgmtdate Reference period for timezone (timezone differs on winter and summer. May be 'now', 'winter' or 'summer')
  74. * @return int An offset in hour (+1 for Europe/Paris on winter and +2 for Europe/Paris on summer)
  75. */
  76. function getServerTimeZoneInt($refgmtdate='now')
  77. {
  78. global $conf;
  79. if (method_exists('DateTimeZone','getOffset'))
  80. {
  81. // Method 1 (include daylight)
  82. $gmtnow=dol_now('gmt'); $yearref=dol_print_date($gmtnow,'%Y'); $monthref=dol_print_date($gmtnow,'%m'); $dayref=dol_print_date($gmtnow,'%d');
  83. if ($refgmtdate == 'now') $newrefgmtdate=$yearref.'-'.$monthref.'-'.$dayref;
  84. elseif ($refgmtdate == 'summer') $newrefgmtdate=$yearref.'-08-01';
  85. else $newrefgmtdate=$yearref.'-01-01';
  86. $newrefgmtdate.='T00:00:00+00:00';
  87. $localtz = new DateTimeZone(getServerTimeZoneString());
  88. $localdt = new DateTime($newrefgmtdate, $localtz);
  89. $tmp=-1*$localtz->getOffset($localdt);
  90. //print $refgmtdate.'='.$tmp;
  91. }
  92. else
  93. {
  94. $tmp=0;
  95. dol_print_error('','PHP version must be 5.3+');
  96. }
  97. $tz=round(($tmp<0?1:-1)*abs($tmp/3600));
  98. return $tz;
  99. }
  100. /**
  101. * Add a delay to a date
  102. *
  103. * @param int $time Date timestamp (or string with format YYYY-MM-DD)
  104. * @param int $duration_value Value of delay to add
  105. * @param int $duration_unit Unit of added delay (d, m, y, w, h)
  106. * @return int New timestamp
  107. */
  108. function dol_time_plus_duree($time, $duration_value, $duration_unit)
  109. {
  110. global $conf;
  111. if ($duration_value == 0) return $time;
  112. if ($duration_unit == 'h') return $time + (3600*$duration_value);
  113. if ($duration_unit == 'w') return $time + (3600*24*7*$duration_value);
  114. $deltastring='P';
  115. if ($duration_value > 0){ $deltastring.=abs($duration_value); $sub= false; }
  116. if ($duration_value < 0){ $deltastring.=abs($duration_value); $sub= true; }
  117. if ($duration_unit == 'd') { $deltastring.="D"; }
  118. if ($duration_unit == 'm') { $deltastring.="M"; }
  119. if ($duration_unit == 'y') { $deltastring.="Y"; }
  120. $date = new DateTime();
  121. if (! empty($conf->global->MAIN_DATE_IN_MEMORY_ARE_GMT)) $date->setTimezone(new DateTimeZone('UTC'));
  122. $date->setTimestamp($time);
  123. $interval = new DateInterval($deltastring);
  124. if($sub) $date->sub($interval);
  125. else $date->add( $interval );
  126. return $date->getTimestamp();
  127. }
  128. /**
  129. * Convert hours and minutes into seconds
  130. *
  131. * @param int $iHours Hours
  132. * @param int $iMinutes Minutes
  133. * @param int $iSeconds Seconds
  134. * @return int Time into seconds
  135. * @see convertSecondToTime
  136. */
  137. function convertTime2Seconds($iHours=0,$iMinutes=0,$iSeconds=0)
  138. {
  139. $iResult=($iHours*3600)+($iMinutes*60)+$iSeconds;
  140. return $iResult;
  141. }
  142. /** Return, in clear text, value of a number of seconds in days, hours and minutes.
  143. * Can be used to show a duration.
  144. *
  145. * @param int $iSecond Number of seconds
  146. * @param string $format Output format ('all': total delay days hour:min like "2 days 12:30",
  147. * - 'allwithouthour': total delay days without hour part like "2 days",
  148. * - 'allhourmin': total delay with format hours:min like "60:30",
  149. * - 'allhour': total delay hours without min/sec like "60:30",
  150. * - 'fullhour': total delay hour decimal like "60.5" for 60:30,
  151. * - 'hour': only hours part "12",
  152. * - 'min': only minutes part "30",
  153. * - 'sec': only seconds part,
  154. * - 'month': only month part,
  155. * - 'year': only year part);
  156. * @param int $lengthOfDay Length of day (default 86400 seconds for 1 day, 28800 for 8 hour)
  157. * @param int $lengthOfWeek Length of week (default 7)
  158. * @return string Formated text of duration
  159. * Example: 0 return 00:00, 3600 return 1:00, 86400 return 1d, 90000 return 1 Day 01:00
  160. * @see convertTime2Seconds
  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 == 'allwithouthour' || $format == 'allhour' || $format == 'allhourmin')
  168. {
  169. if ((int) $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=0;
  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. * 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.
  254. *
  255. * @param string $string Date in a string
  256. * YYYYMMDD
  257. * YYYYMMDDHHMMSS
  258. * YYYYMMDDTHHMMSSZ
  259. * YYYY-MM-DDTHH:MM:SSZ (RFC3339)
  260. * DD/MM/YY or DD/MM/YYYY (deprecated)
  261. * DD/MM/YY HH:MM:SS or DD/MM/YYYY HH:MM:SS (deprecated)
  262. * @param int $gm 1 =Input date is GM date,
  263. * 0 =Input date is local date using PHP server timezone
  264. * @return int Date as a timestamp
  265. * 19700101020000 -> 7200 with gm=1
  266. *
  267. * @see dol_print_date, dol_mktime, dol_getdate
  268. */
  269. function dol_stringtotime($string, $gm=1)
  270. {
  271. // Convert date with format DD/MM/YYY HH:MM:SS. This part of code should not be used.
  272. if (preg_match('/^([0-9]+)\/([0-9]+)\/([0-9]+)\s?([0-9]+)?:?([0-9]+)?:?([0-9]+)?/i',$string,$reg))
  273. {
  274. dol_syslog("dol_stringtotime call to function with deprecated parameter format", LOG_WARNING);
  275. // Date est au format 'DD/MM/YY' ou 'DD/MM/YY HH:MM:SS'
  276. // Date est au format 'DD/MM/YYYY' ou 'DD/MM/YYYY HH:MM:SS'
  277. $sday = $reg[1];
  278. $smonth = $reg[2];
  279. $syear = $reg[3];
  280. $shour = $reg[4];
  281. $smin = $reg[5];
  282. $ssec = $reg[6];
  283. if ($syear < 50) $syear+=1900;
  284. if ($syear >= 50 && $syear < 100) $syear+=2000;
  285. $string=sprintf("%04d%02d%02d%02d%02d%02d",$syear,$smonth,$sday,$shour,$smin,$ssec);
  286. }
  287. else if (
  288. 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)
  289. || 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
  290. || 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
  291. )
  292. {
  293. $syear = $reg[1];
  294. $smonth = $reg[2];
  295. $sday = $reg[3];
  296. $shour = $reg[4];
  297. $smin = $reg[5];
  298. $ssec = $reg[6];
  299. $string=sprintf("%04d%02d%02d%02d%02d%02d",$syear,$smonth,$sday,$shour,$smin,$ssec);
  300. }
  301. $string=preg_replace('/([^0-9])/i','',$string);
  302. $tmp=$string.'000000';
  303. $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));
  304. return $date;
  305. }
  306. /** Return previous day
  307. *
  308. * @param int $day Day
  309. * @param int $month Month
  310. * @param int $year Year
  311. * @return array Previous year,month,day
  312. */
  313. function dol_get_prev_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 next day
  321. *
  322. * @param int $day Day
  323. * @param int $month Month
  324. * @param int $year Year
  325. * @return array Next year,month,day
  326. */
  327. function dol_get_next_day($day, $month, $year)
  328. {
  329. $time=dol_mktime(12,0,0,$month,$day,$year,1,0);
  330. $time+=24*60*60;
  331. $tmparray=dol_getdate($time,true);
  332. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  333. }
  334. /** Return previous month
  335. *
  336. * @param int $month Month
  337. * @param int $year Year
  338. * @return array Previous year,month
  339. */
  340. function dol_get_prev_month($month, $year)
  341. {
  342. if ($month == 1)
  343. {
  344. $prev_month = 12;
  345. $prev_year = $year - 1;
  346. }
  347. else
  348. {
  349. $prev_month = $month-1;
  350. $prev_year = $year;
  351. }
  352. return array('year' => $prev_year, 'month' => $prev_month);
  353. }
  354. /** Return next month
  355. *
  356. * @param int $month Month
  357. * @param int $year Year
  358. * @return array Next year,month
  359. */
  360. function dol_get_next_month($month, $year)
  361. {
  362. if ($month == 12)
  363. {
  364. $next_month = 1;
  365. $next_year = $year + 1;
  366. }
  367. else
  368. {
  369. $next_month = $month + 1;
  370. $next_year = $year;
  371. }
  372. return array('year' => $next_year, 'month' => $next_month);
  373. }
  374. /** Return previous week
  375. *
  376. * @param int $day Day
  377. * @param int $week Week
  378. * @param int $month Month
  379. * @param int $year Year
  380. * @return array Previous year,month,day
  381. */
  382. function dol_get_prev_week($day, $week, $month, $year)
  383. {
  384. $tmparray = dol_get_first_day_week($day, $month, $year);
  385. $time=dol_mktime(12,0,0,$month,$tmparray['first_day'],$year,1,0);
  386. $time-=24*60*60*7;
  387. $tmparray=dol_getdate($time,true);
  388. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  389. }
  390. /** Return next week
  391. *
  392. * @param int $day Day
  393. * @param int $week Week
  394. * @param int $month Month
  395. * @param int $year Year
  396. * @return array Next year,month,day
  397. */
  398. function dol_get_next_week($day, $week, $month, $year)
  399. {
  400. $tmparray = dol_get_first_day_week($day, $month, $year);
  401. $time=dol_mktime(12,0,0,$tmparray['first_month'],$tmparray['first_day'],$tmparray['first_year'],1,0);
  402. $time+=24*60*60*7;
  403. $tmparray=dol_getdate($time,true);
  404. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  405. }
  406. /** Return GMT time for first day of a month or year
  407. *
  408. * @param int $year Year
  409. * @param int $month Month
  410. * @param mixed $gm False or 0 or 'server' = Return date to compare with server TZ, True or 1 to compare with GM date.
  411. * 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
  412. * 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
  413. * @return int Date for first day, '' if error
  414. */
  415. function dol_get_first_day($year,$month=1,$gm=false)
  416. {
  417. if ($year > 9999) return '';
  418. return dol_mktime(0,0,0,$month,1,$year,$gm);
  419. }
  420. /** Return GMT time for last day of a month or year
  421. *
  422. * @param int $year Year
  423. * @param int $month Month
  424. * @param boolean $gm False or 0 or 'server' = Return date to compare with server TZ, True or 1 to compare with GM date.
  425. * @return int Date for first day, '' if error
  426. */
  427. function dol_get_last_day($year,$month=12,$gm=false)
  428. {
  429. if ($year > 9999) return '';
  430. if ($month == 12)
  431. {
  432. $month = 1;
  433. $year += 1;
  434. }
  435. else
  436. {
  437. $month += 1;
  438. }
  439. // On se deplace au debut du mois suivant, et on retire un jour
  440. $datelim=dol_mktime(23,59,59,$month,1,$year,$gm);
  441. $datelim -= (3600 * 24);
  442. return $datelim;
  443. }
  444. /** Return first day of week for a date. First day of week may be monday if option MAIN_START_WEEK is 1.
  445. *
  446. * @param int $day Day
  447. * @param int $month Month
  448. * @param int $year Year
  449. * @param int $gm False or 0 or 'server' = Return date to compare with server TZ, True or 1 to compare with GM date.
  450. * @return array year,month,week,first_day,first_month,first_year,prev_day,prev_month,prev_year
  451. */
  452. function dol_get_first_day_week($day,$month,$year,$gm=false)
  453. {
  454. global $conf;
  455. //$day=2; $month=2; $year=2015;
  456. $date = dol_mktime(0,0,0,$month,$day,$year,$gm);
  457. //Checking conf of start week
  458. $start_week = (isset($conf->global->MAIN_START_WEEK)?$conf->global->MAIN_START_WEEK:1);
  459. $tmparray = dol_getdate($date,true); // detail of current day
  460. //Calculate days = offset from current day
  461. $days = $start_week - $tmparray['wday'];
  462. if ($days>=1) $days=7-$days;
  463. $days = abs($days);
  464. $seconds = $days*24*60*60;
  465. //print 'start_week='.$start_week.' tmparray[wday]='.$tmparray['wday'].' day offset='.$days.' seconds offset='.$seconds.'<br>';
  466. //Get first day of week
  467. $tmpdaytms = date($tmparray[0])-$seconds; // $tmparray[0] is day of parameters
  468. $tmpday = date("d",$tmpdaytms);
  469. //Check first day of week is in same month than current day or not
  470. if ($tmpday>$day)
  471. {
  472. $prev_month = $month-1;
  473. $prev_year = $year;
  474. if ($prev_month==0)
  475. {
  476. $prev_month = 12;
  477. $prev_year = $year-1;
  478. }
  479. }
  480. else
  481. {
  482. $prev_month = $month;
  483. $prev_year = $year;
  484. }
  485. $tmpmonth = $prev_month;
  486. $tmpyear = $prev_year;
  487. //Get first day of next week
  488. $tmptime=dol_mktime(12,0,0,$month,$tmpday,$year,1,0);
  489. $tmptime-=24*60*60*7;
  490. $tmparray=dol_getdate($tmptime,true);
  491. $prev_day = $tmparray['mday'];
  492. //Check prev day of week is in same month than first day or not
  493. if ($prev_day > $tmpday)
  494. {
  495. $prev_month = $month-1;
  496. $prev_year = $year;
  497. if ($prev_month==0)
  498. {
  499. $prev_month = 12;
  500. $prev_year = $year-1;
  501. }
  502. }
  503. $week = date("W",dol_mktime(0,0,0,$tmpmonth,$tmpday,$tmpyear,$gm));
  504. 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);
  505. }
  506. /**
  507. * 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
  508. * Called by function num_open_day
  509. *
  510. * @param int $timestampStart Timestamp de debut
  511. * @param int $timestampEnd Timestamp de fin
  512. * @param string $countrycode Country code
  513. * @param int $lastday Last day is included, 0: no, 1:yes
  514. * @return int Nombre de jours feries
  515. * @see num_between_day, num_open_day
  516. */
  517. function num_public_holiday($timestampStart, $timestampEnd, $countrycode='FR', $lastday=0)
  518. {
  519. global $conf;
  520. $nbFerie = 0;
  521. // Check to ensure we use correct parameters
  522. if ((($timestampEnd - $timestampStart) % 86400) != 0) return 'ErrorDates must use same hours and must be GMT dates';
  523. $i=0;
  524. while (( ($lastday == 0 && $timestampStart < $timestampEnd) || ($lastday && $timestampStart <= $timestampEnd) )
  525. && ($i < 50000)) // Loop end when equals (Test on i is a security loop to avoid infinite loop)
  526. {
  527. $ferie=false;
  528. $countryfound=0;
  529. $includesaturdayandsunday=1;
  530. $jour = date("d", $timestampStart);
  531. $mois = date("m", $timestampStart);
  532. $annee = date("Y", $timestampStart);
  533. // Check into var $conf->global->HOLIDAY_MORE_DAYS MM-DD,YYYY-MM-DD, ...
  534. if (! empty($conf->global->HOLIDAY_MORE_PUBLIC_HOLIDAYS))
  535. {
  536. $arrayofdaystring=explode(',',$conf->global->HOLIDAY_MORE_PUBLIC_HOLIDAYS);
  537. foreach($arrayofdaystring as $daystring)
  538. {
  539. $tmp=explode('-',$daystring);
  540. if ($tmp[2])
  541. {
  542. if ($tmp[0] == $annee && $tmp[1] == $mois && $tmp[2] == $jour) $ferie=true;
  543. }
  544. else
  545. {
  546. if ($tmp[0] == $mois && $tmp[1] == $jour) $ferie=true;
  547. }
  548. }
  549. }
  550. if ($countrycode == 'FR')
  551. {
  552. $countryfound=1;
  553. // Definition of fixed working days
  554. if($jour == 1 && $mois == 1) $ferie=true; // 1er january
  555. if($jour == 1 && $mois == 5) $ferie=true; // 1er may
  556. if($jour == 8 && $mois == 5) $ferie=true; // 5 may
  557. if($jour == 14 && $mois == 7) $ferie=true; // 14 july
  558. if($jour == 15 && $mois == 8) $ferie=true; // 15 august
  559. if($jour == 1 && $mois == 11) $ferie=true; // 1 november
  560. if($jour == 11 && $mois == 11) $ferie=true; // 11 november
  561. if($jour == 25 && $mois == 12) $ferie=true; // 25 december
  562. // Calculation for easter date
  563. $date_paques = easter_date($annee);
  564. $jour_paques = date("d", $date_paques);
  565. $mois_paques = date("m", $date_paques);
  566. if($jour_paques == $jour && $mois_paques == $mois) $ferie=true;
  567. // Pâques
  568. // Calculation for the monday of easter date
  569. $date_lundi_paques = mktime(
  570. date("H", $date_paques),
  571. date("i", $date_paques),
  572. date("s", $date_paques),
  573. date("m", $date_paques),
  574. date("d", $date_paques) + 1,
  575. date("Y", $date_paques)
  576. );
  577. $jour_lundi_ascension = date("d", $date_lundi_paques);
  578. $mois_lundi_ascension = date("m", $date_lundi_paques);
  579. if($jour_lundi_ascension == $jour && $mois_lundi_ascension == $mois) $ferie=true;
  580. // Lundi de Pâques
  581. // Calcul du jour de l'ascension (38 days after easter day)
  582. $date_ascension = mktime(
  583. date("H", $date_paques),
  584. date("i", $date_paques),
  585. date("s", $date_paques),
  586. date("m", $date_paques),
  587. date("d", $date_paques) + 39,
  588. date("Y", $date_paques)
  589. );
  590. $jour_ascension = date("d", $date_ascension);
  591. $mois_ascension = date("m", $date_ascension);
  592. if($jour_ascension == $jour && $mois_ascension == $mois) $ferie=true;
  593. // Ascension
  594. // Calculation of "Pentecote" (11 days after easter day)
  595. $date_pentecote = mktime(
  596. date("H", $date_paques),
  597. date("i", $date_paques),
  598. date("s", $date_paques),
  599. date("m", $date_paques),
  600. date("d", $date_paques) + 49,
  601. date("Y", $date_paques)
  602. );
  603. $jour_pentecote = date("d", $date_pentecote);
  604. $mois_pentecote = date("m", $date_pentecote);
  605. if($jour_pentecote == $jour && $mois_pentecote == $mois) $ferie=true;
  606. // "Pentecote"
  607. }
  608. // Pentecoste and Ascensione in Italy go to the sunday after: isn't holiday.
  609. // Pentecoste is 50 days after Easter, Ascensione 40
  610. if ($countrycode == 'IT')
  611. {
  612. $countryfound=1;
  613. // Definition des dates feriees fixes
  614. if($jour == 1 && $mois == 1) $ferie=true; // Capodanno
  615. if($jour == 6 && $mois == 1) $ferie=true; // Epifania
  616. if($jour == 25 && $mois == 4) $ferie=true; // Anniversario Liberazione
  617. if($jour == 1 && $mois == 5) $ferie=true; // Festa del Lavoro
  618. if($jour == 2 && $mois == 6) $ferie=true; // Festa della Repubblica
  619. if($jour == 15 && $mois == 8) $ferie=true; // Ferragosto
  620. if($jour == 1 && $mois == 11) $ferie=true; // Tutti i Santi
  621. if($jour == 8 && $mois == 12) $ferie=true; // Immacolata Concezione
  622. if($jour == 25 && $mois == 12) $ferie=true; // 25 decembre
  623. if($jour == 26 && $mois == 12) $ferie=true; // Santo Stefano
  624. // Calcul du jour de paques
  625. $date_paques = easter_date($annee);
  626. $jour_paques = date("d", $date_paques);
  627. $mois_paques = date("m", $date_paques);
  628. if($jour_paques == $jour && $mois_paques == $mois) $ferie=true;
  629. // Paques
  630. }
  631. if ($countrycode == 'IN')
  632. {
  633. $countryfound=1;
  634. if($jour == 1 && $mois == 1) $ferie=true; // New Year's Day
  635. if($jour == 26 && $mois == 1) $ferie=true; // Republic Day
  636. if($jour == 1 && $mois == 5) $ferie=true; // May Day
  637. if($jour == 15 && $mois == 8) $ferie=true; // Independence Day
  638. if($jour == 2 && $mois == 10) $ferie=true; // Gandhi Jayanti
  639. if($jour == 25 && $mois == 12) $ferie=true; // Christmas
  640. }
  641. if ($countrycode == 'ES')
  642. {
  643. $countryfound=1;
  644. // Definition des dates feriees fixes
  645. if($jour == 1 && $mois == 1) $ferie=true; // Año nuevo
  646. if($jour == 6 && $mois == 1) $ferie=true; // Día Reyes
  647. if($jour == 1 && $mois == 5) $ferie=true; // 1 Mayo
  648. if($jour == 15 && $mois == 8) $ferie=true; // 15 Agosto
  649. if($jour == 12 && $mois == 10) $ferie=true; // Día Hispanidad
  650. if($jour == 1 && $mois == 11) $ferie=true; // 1 noviembre
  651. if($jour == 6 && $mois == 12) $ferie=true; // Constitución
  652. if($jour == 8 && $mois == 12) $ferie=true; // Inmaculada
  653. if($jour == 25 && $mois == 12) $ferie=true; // 25 diciembre
  654. // Calcul día de Pascua
  655. $date_paques = easter_date($annee);
  656. $jour_paques = date("d", $date_paques);
  657. $mois_paques = date("m", $date_paques);
  658. if($jour_paques == $jour && $mois_paques == $mois) $ferie=true;
  659. // Paques
  660. // Viernes Santo
  661. $date_viernes = mktime(
  662. date("H", $date_paques),
  663. date("i", $date_paques),
  664. date("s", $date_paques),
  665. date("m", $date_paques),
  666. date("d", $date_paques) -2,
  667. date("Y", $date_paques)
  668. );
  669. $jour_viernes = date("d", $date_viernes);
  670. $mois_viernes = date("m", $date_viernes);
  671. if($jour_viernes == $jour && $mois_viernes == $mois) $ferie=true;
  672. //Viernes Santo
  673. }
  674. if ($countrycode == 'AT')
  675. {
  676. $countryfound=1;
  677. // Definition des dates feriees fixes
  678. if($jour == 1 && $mois == 1) $ferie=true; // Neujahr
  679. if($jour == 6 && $mois == 1) $ferie=true; // Hl. 3 Koenige
  680. if($jour == 1 && $mois == 5) $ferie=true; // 1. Mai
  681. if($jour == 15 && $mois == 8) $ferie=true; // Mariae Himmelfahrt
  682. if($jour == 26 && $mois == 10) $ferie=true; // 26. Oktober
  683. if($jour == 1 && $mois == 11) $ferie=true; // Allerheiligen
  684. if($jour == 8 && $mois == 12) $ferie=true; // Mariae Empfaengnis
  685. if($jour == 24 && $mois == 12) $ferie=true; // Heilig abend
  686. if($jour == 25 && $mois == 12) $ferie=true; // Christtag
  687. if($jour == 26 && $mois == 12) $ferie=true; // Stefanietag
  688. if($jour == 31 && $mois == 12) $ferie=true; // Silvester
  689. // Easter calculation
  690. $date_paques = easter_date($annee);
  691. $jour_paques = date("d", $date_paques);
  692. $mois_paques = date("m", $date_paques);
  693. if($jour_paques == $jour && $mois_paques == $mois) $ferie=true;
  694. // Easter sunday
  695. // Monday after easter
  696. $date_eastermonday = mktime(
  697. date("H", $date_paques),
  698. date("i", $date_paques),
  699. date("s", $date_paques),
  700. date("m", $date_paques),
  701. date("d", $date_paques) + 1,
  702. date("Y", $date_paques)
  703. );
  704. $jour_eastermonday = date("d", $date_eastermonday);
  705. $mois_eastermonday = date("m", $date_eastermonday);
  706. if($jour_eastermonday == $jour && $mois_eastermonday == $mois) $ferie=true;
  707. // Easter monday
  708. // Christi Himmelfahrt (39 days after easter sunday)
  709. $date_ch = mktime(
  710. date("H", $date_paques),
  711. date("i", $date_paques),
  712. date("s", $date_paques),
  713. date("m", $date_paques),
  714. date("d", $date_paques) + 39,
  715. date("Y", $date_paques)
  716. );
  717. $jour_ch = date("d", $date_ch);
  718. $mois_ch = date("m", $date_ch);
  719. if($jour_ch == $jour && $mois_ch == $mois) $ferie=true;
  720. // Christi Himmelfahrt
  721. // Pfingsten (50 days after easter sunday)
  722. $date_pentecote = mktime(
  723. date("H", $date_paques),
  724. date("i", $date_paques),
  725. date("s", $date_paques),
  726. date("m", $date_paques),
  727. date("d", $date_paques) + 50,
  728. date("Y", $date_paques)
  729. );
  730. $jour_pentecote = date("d", $date_pentecote);
  731. $mois_pentecote = date("m", $date_pentecote);
  732. if($jour_pentecote == $jour && $mois_pentecote == $mois) $ferie=true;
  733. // Pfingsten
  734. // Fronleichnam (60 days after easter sunday)
  735. $date_fronleichnam = mktime(
  736. date("H", $date_paques),
  737. date("i", $date_paques),
  738. date("s", $date_paques),
  739. date("m", $date_paques),
  740. date("d", $date_paques) + 60,
  741. date("Y", $date_paques)
  742. );
  743. $jour_fronleichnam = date("d", $date_fronleichnam);
  744. $mois_fronleichnam = date("m", $date_fronleichnam);
  745. if($jour_fronleichnam == $jour && $mois_fronleichnam == $mois) $ferie=true;
  746. // Fronleichnam
  747. }
  748. // If we have to include saturday and sunday
  749. if ($includesaturdayandsunday)
  750. {
  751. $jour_julien = unixtojd($timestampStart);
  752. $jour_semaine = jddayofweek($jour_julien, 0);
  753. if($jour_semaine == 0 || $jour_semaine == 6) $ferie=true;
  754. //Saturday (6) and Sunday (0)
  755. }
  756. // On incremente compteur
  757. if ($ferie) $nbFerie++;
  758. // Increase number of days (on go up into loop)
  759. $timestampStart=dol_time_plus_duree($timestampStart, 1, 'd');
  760. //var_dump($jour.' '.$mois.' '.$annee.' '.$timestampStart);
  761. $i++;
  762. }
  763. return $nbFerie;
  764. }
  765. /**
  766. * Function to return number of days between two dates (date must be UTC date !)
  767. * Example: 2012-01-01 2012-01-02 => 1 if lastday=0, 2 if lastday=1
  768. *
  769. * @param int $timestampStart Timestamp start UTC
  770. * @param int $timestampEnd Timestamp end UTC
  771. * @param int $lastday Last day is included, 0: no, 1:yes
  772. * @return int Number of days
  773. * @see also num_public_holiday, num_open_day
  774. */
  775. function num_between_day($timestampStart, $timestampEnd, $lastday=0)
  776. {
  777. if ($timestampStart < $timestampEnd)
  778. {
  779. if ($lastday == 1)
  780. {
  781. $bit = 0;
  782. }
  783. else
  784. {
  785. $bit = 1;
  786. }
  787. $nbjours = (int) floor(($timestampEnd - $timestampStart)/(60*60*24)) + 1 - $bit;
  788. }
  789. //print ($timestampEnd - $timestampStart) - $lastday;
  790. return $nbjours;
  791. }
  792. /**
  793. * Function to return number of working days (and text of units) between two dates (working days)
  794. *
  795. * @param int $timestampStart Timestamp for start date (date must be UTC to avoid calculation errors)
  796. * @param int $timestampEnd Timestamp for end date (date must be UTC to avoid calculation errors)
  797. * @param int $inhour 0: return number of days, 1: return number of hours
  798. * @param int $lastday We include last day, 0: no, 1:yes
  799. * @param int $halfday Tag to define half day when holiday start and end
  800. * @param string $country_code Country code (company country code if not defined)
  801. * @return int Number of days or hours
  802. * @see also num_between_day, num_public_holiday
  803. */
  804. function num_open_day($timestampStart, $timestampEnd, $inhour=0, $lastday=0, $halfday=0, $country_code='')
  805. {
  806. global $langs,$mysoc;
  807. if (empty($country_code)) $country_code=$mysoc->country_code;
  808. dol_syslog('num_open_day timestampStart='.$timestampStart.' timestampEnd='.$timestampEnd.' bit='.$lastday.' country_code='.$country_code);
  809. // Check parameters
  810. if (! is_int($timestampStart) && ! is_float($timestampStart)) return 'ErrorBadParameter_num_open_day';
  811. if (! is_int($timestampEnd) && ! is_float($timestampEnd)) return 'ErrorBadParameter_num_open_day';
  812. //print 'num_open_day timestampStart='.$timestampStart.' timestampEnd='.$timestampEnd.' bit='.$lastday;
  813. if ($timestampStart < $timestampEnd)
  814. {
  815. $numdays = num_between_day($timestampStart, $timestampEnd, $lastday);
  816. $numholidays = num_public_holiday($timestampStart, $timestampEnd, $country_code, $lastday);
  817. $nbOpenDay = $numdays - $numholidays;
  818. $nbOpenDay.= " " . $langs->trans("Days");
  819. if ($inhour == 1 && $nbOpenDay <= 3) $nbOpenDay = $nbOpenDay*24 . $langs->trans("HourShort");
  820. return $nbOpenDay - (($inhour == 1 ? 12 : 0.5) * abs($halfday));
  821. }
  822. elseif ($timestampStart == $timestampEnd)
  823. {
  824. $nbOpenDay=$lastday;
  825. if ($inhour == 1) $nbOpenDay = $nbOpenDay*24 . $langs->trans("HourShort");
  826. return $nbOpenDay - (($inhour == 1 ? 12 : 0.5) * abs($halfday));
  827. }
  828. else
  829. {
  830. return $langs->trans("Error");
  831. }
  832. }
  833. /**
  834. * Return array of translated months or selected month.
  835. * This replace old function monthArrayOrSelected.
  836. *
  837. * @param Translate $outputlangs Object langs
  838. * @param int $short 0=Return long label, 1=Return short label
  839. * @return array Month string or array if selected < 0
  840. */
  841. function monthArray($outputlangs,$short=0)
  842. {
  843. $montharray = array (
  844. 1 => $outputlangs->trans("Month01"),
  845. 2 => $outputlangs->trans("Month02"),
  846. 3 => $outputlangs->trans("Month03"),
  847. 4 => $outputlangs->trans("Month04"),
  848. 5 => $outputlangs->trans("Month05"),
  849. 6 => $outputlangs->trans("Month06"),
  850. 7 => $outputlangs->trans("Month07"),
  851. 8 => $outputlangs->trans("Month08"),
  852. 9 => $outputlangs->trans("Month09"),
  853. 10 => $outputlangs->trans("Month10"),
  854. 11 => $outputlangs->trans("Month11"),
  855. 12 => $outputlangs->trans("Month12")
  856. );
  857. if (! empty($short))
  858. {
  859. $montharray = array (
  860. 1 => $outputlangs->trans("MonthShort01"),
  861. 2 => $outputlangs->trans("MonthShort02"),
  862. 3 => $outputlangs->trans("MonthShort03"),
  863. 4 => $outputlangs->trans("MonthShort04"),
  864. 5 => $outputlangs->trans("MonthShort05"),
  865. 6 => $outputlangs->trans("MonthShort06"),
  866. 7 => $outputlangs->trans("MonthShort07"),
  867. 8 => $outputlangs->trans("MonthShort08"),
  868. 9 => $outputlangs->trans("MonthShort09"),
  869. 10 => $outputlangs->trans("MonthShort10"),
  870. 11 => $outputlangs->trans("MonthShort11"),
  871. 12 => $outputlangs->trans("MonthShort12")
  872. );
  873. }
  874. return $montharray;
  875. }