PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/asterix14/dolibarr
PHP | 653 lines | 395 code | 73 blank | 185 comment | 173 complexity | 03aa60aef04a0814ca6171716aefeb3d MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2011 Regis Houssin <regis@dolibarr.fr>
  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 2 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( -11=>"Pacific/Midway",
  32. -10=>"Pacific/Fakaofo",
  33. -9=>"America/Anchorage",
  34. -8=>"America/Los_Angeles",
  35. -7=>"America/Dawson_Creek",
  36. -6=>"America/Chicago",
  37. -5=>"America/Bogota",
  38. -4=>"America/Anguilla",
  39. -3=>"America/Araguaina",
  40. -2=>"America/Noronha",
  41. -1=>"Atlantic/Azores",
  42. 0=>"Africa/Abidjan",
  43. 1=>"Europe/Paris",
  44. 2=>"Europe/Helsinki",
  45. 3=>"Europe/Moscow",
  46. 4=>"Asia/Dubai",
  47. 5=>"Asia/Karachi",
  48. 6=>"Indian/Chagos",
  49. 7=>"Asia/Jakarta",
  50. 8=>"Asia/Hong_Kong",
  51. 9=>"Asia/Tokyo",
  52. 10=>"Australia/Sydney",
  53. 11=>"Pacific/Noumea",
  54. 12=>"Pacific/Auckland",
  55. 13=>"Pacific/Enderbury"
  56. );
  57. return $tzarray;
  58. }
  59. /**
  60. * Return server timezone
  61. *
  62. * @return string TimeZone
  63. */
  64. function getCurrentTimeZone()
  65. {
  66. // Method 1
  67. //$tzstring=date_default_timezone_get(); // Then convert into tz
  68. // Method 2
  69. $tmp=dol_mktime(0,0,0,1,1,1970);
  70. $tz=($tmp<0?'+':'-').sprintf("%02d",abs($tmp/3600));
  71. return $tz;
  72. }
  73. /**
  74. * Add a delay to a date
  75. *
  76. * @param timestamp $time Date timestamp (or string with format YYYY-MM-DD)
  77. * @param int $duration_value Value of delay to add
  78. * @param int $duration_unit Unit of added delay (d, m, y)
  79. * @return timestamp New timestamp
  80. */
  81. function dol_time_plus_duree($time,$duration_value,$duration_unit)
  82. {
  83. if ($duration_value == 0) return $time;
  84. if ($duration_value > 0) $deltastring="+".abs($duration_value);
  85. if ($duration_value < 0) $deltastring="-".abs($duration_value);
  86. if ($duration_unit == 'd') { $deltastring.=" day"; }
  87. if ($duration_unit == 'm') { $deltastring.=" month"; }
  88. if ($duration_unit == 'y') { $deltastring.=" year"; }
  89. return strtotime($deltastring,$time);
  90. }
  91. /** Converti les heures et minutes en secondes
  92. *
  93. * @param int $iHours Heures
  94. * @param int $iMinutes Minutes
  95. * @param int $iSeconds Secondes
  96. * @return int $iResult Temps en secondes
  97. */
  98. function ConvertTime2Seconds($iHours=0,$iMinutes=0,$iSeconds=0)
  99. {
  100. $iResult=($iHours*3600)+($iMinutes*60)+$iSeconds;
  101. return $iResult;
  102. }
  103. /** Return, in clear text, value of a number of seconds in days, hours and minutes
  104. *
  105. * @param int $iSecond Number of seconds
  106. * @param string $format Output format (all: complete display, hour: displays only hours, min: displays only minutes, sec: displays only seconds, month: display month only, year: displays only year);
  107. * @param int $lengthOfDay Length of day (default 86400 seconds for 1 day, 28800 for 8 hour)
  108. * @param int $lengthOfWeek Length of week (default 7)
  109. * @return sTime Formated text of duration
  110. * Example: 0 return 00:00, 3600 return 1:00, 86400 return 1d, 90000 return 1 Day 01:00
  111. */
  112. function ConvertSecondToTime($iSecond,$format='all',$lengthOfDay=86400,$lengthOfWeek=7)
  113. {
  114. global $langs;
  115. if (empty($lengthOfDay)) $lengthOfDay = 86400; // 1 day = 24 hours
  116. if (empty($lengthOfWeek)) $lengthOfWeek = 7; // 1 week = 7 days
  117. if ($format == 'all')
  118. {
  119. if ($iSecond === 0) return '0'; // This is to avoid having 0 return a 12:00 AM for en_US
  120. $sTime='';
  121. $sDay=0;
  122. $sWeek='';
  123. if ($iSecond >= $lengthOfDay)
  124. {
  125. for($i = $iSecond; $i >= $lengthOfDay; $i -= $lengthOfDay )
  126. {
  127. $sDay++;
  128. $iSecond-=$lengthOfDay;
  129. }
  130. $dayTranslate = $langs->trans("Day");
  131. if ($iSecond >= ($lengthOfDay*2)) $dayTranslate = $langs->trans("Days");
  132. }
  133. if ($lengthOfWeek < 7)
  134. {
  135. if ($sDay)
  136. {
  137. if ($sDay >= $lengthOfWeek)
  138. {
  139. $sWeek = (int) (($sDay - $sDay % $lengthOfWeek ) / $lengthOfWeek);
  140. $sDay = $sDay % $lengthOfWeek;
  141. $weekTranslate = $langs->trans("DurationWeek");
  142. if ($sWeek >= 2) $weekTranslate = $langs->trans("DurationWeeks");
  143. $sTime.=$sWeek.' '.$weekTranslate.' ';
  144. }
  145. if ($sDay>0)
  146. {
  147. $dayTranslate = $langs->trans("Day");
  148. if ($sDay > 1) $dayTranslate = $langs->trans("Days");
  149. $sTime.=$sDay.' '.$dayTranslate.' ';
  150. }
  151. }
  152. }
  153. if ($sDay) $sTime.=$sDay.' '.$dayTranslate.' ';
  154. if ($iSecond || empty($sDay))
  155. {
  156. $sTime.= dol_print_date($iSecond,'hourduration',true);
  157. }
  158. }
  159. else if ($format == 'hour')
  160. {
  161. $sTime=dol_print_date($iSecond,'%H',true);
  162. }
  163. else if ($format == 'min')
  164. {
  165. $sTime=dol_print_date($iSecond,'%M',true);
  166. }
  167. else if ($format == 'sec')
  168. {
  169. $sTime=dol_print_date($iSecond,'%S',true);
  170. }
  171. else if ($format == 'month')
  172. {
  173. $sTime=dol_print_date($iSecond,'%m',true);
  174. }
  175. else if ($format == 'year')
  176. {
  177. $sTime=dol_print_date($iSecond,'%Y',true);
  178. }
  179. return trim($sTime);
  180. }
  181. /** Return previous day
  182. *
  183. * @param int $day Day
  184. * @param int $month Month
  185. * @param int $year Year
  186. * @return array Previous year,month,day
  187. */
  188. function dol_get_prev_day($day, $month, $year)
  189. {
  190. $time=dol_mktime(12,0,0,$month,$day,$year,1,0);
  191. $time-=24*60*60;
  192. $tmparray=dol_getdate($time,true);
  193. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  194. }
  195. /** Return next day
  196. *
  197. * @param int $day Day
  198. * @param int $month Month
  199. * @param int $year Year
  200. * @return array Next year,month,day
  201. */
  202. function dol_get_next_day($day, $month, $year)
  203. {
  204. $time=dol_mktime(12,0,0,$month,$day,$year,1,0);
  205. $time+=24*60*60;
  206. $tmparray=dol_getdate($time,true);
  207. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  208. }
  209. /** Return previous month
  210. *
  211. * @param int $month Month
  212. * @param int $year Year
  213. * @return array Previous year,month
  214. */
  215. function dol_get_prev_month($month, $year)
  216. {
  217. if ($month == 1)
  218. {
  219. $prev_month = 12;
  220. $prev_year = $year - 1;
  221. }
  222. else
  223. {
  224. $prev_month = $month-1;
  225. $prev_year = $year;
  226. }
  227. return array('year' => $prev_year, 'month' => $prev_month);
  228. }
  229. /** Return next month
  230. *
  231. * @param int $month Month
  232. * @param int $year Year
  233. * @return array Next year,month
  234. */
  235. function dol_get_next_month($month, $year)
  236. {
  237. if ($month == 12)
  238. {
  239. $next_month = 1;
  240. $next_year = $year + 1;
  241. }
  242. else
  243. {
  244. $next_month = $month + 1;
  245. $next_year = $year;
  246. }
  247. return array('year' => $next_year, 'month' => $next_month);
  248. }
  249. /** Return previous week
  250. *
  251. * @param int $day Day
  252. * @param int $week Week
  253. * @param int $month Month
  254. * @param int $year Year
  255. * @return array Previous year,month,day
  256. */
  257. function dol_get_prev_week($day, $week, $month, $year)
  258. {
  259. $tmparray = dol_get_first_day_week($day, $month, $year);
  260. $time=dol_mktime(12,0,0,$month,$tmparray['first_day'],$year,1,0);
  261. $time-=24*60*60*7;
  262. $tmparray=dol_getdate($time,true);
  263. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  264. }
  265. /** Return next week
  266. *
  267. * @param int $day Day
  268. * @param int $week Week
  269. * @param int $month Month
  270. * @param int $year Year
  271. * @return array Next year,month,day
  272. */
  273. function dol_get_next_week($day, $week, $month, $year)
  274. {
  275. $tmparray = dol_get_first_day_week($day, $month, $year);
  276. $time=dol_mktime(12,0,0,$month,$tmparray['first_day'],$year,1,0);
  277. $time+=24*60*60*7;
  278. $tmparray=dol_getdate($time,true);
  279. return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
  280. }
  281. /** Return GMT time for first day of a month or year
  282. *
  283. * @param int $year Year
  284. * @param int $month Month
  285. * @param boolean $gm False = Return date to compare with server TZ, True to compare with GM date.
  286. * 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
  287. * 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
  288. * @return timestamp Date for first day
  289. */
  290. function dol_get_first_day($year,$month=1,$gm=false)
  291. {
  292. return dol_mktime(0,0,0,$month,1,$year,$gm);
  293. }
  294. /** Return GMT time for last day of a month or year
  295. *
  296. * @param int $year Year
  297. * @param int $month Month
  298. * @param boolean $gm False = Return date to compare with server TZ, True to compare with GM date.
  299. * @return timestamp Date for first day
  300. */
  301. function dol_get_last_day($year,$month=12,$gm=false)
  302. {
  303. if ($month == 12)
  304. {
  305. $month = 1;
  306. $year += 1;
  307. }
  308. else
  309. {
  310. $month += 1;
  311. }
  312. // On se deplace au debut du mois suivant, et on retire un jour
  313. $datelim=dol_mktime(23,59,59,$month,1,$year,$gm);
  314. $datelim -= (3600 * 24);
  315. return $datelim;
  316. }
  317. /** Return first day of week for a date
  318. *
  319. * @param int $day Day
  320. * @param int $month Month
  321. * @param int $year Year
  322. * @param int $gm False = Return date to compare with server TZ, True to compare with GM date.
  323. * @return array year,month, week,first_day,prev_year,prev_month,prev_day
  324. */
  325. function dol_get_first_day_week($day,$month,$year,$gm=false)
  326. {
  327. global $conf;
  328. $date = dol_mktime(0,0,0,$month,$day,$year,$gm);
  329. //Checking conf of start week
  330. $start_week = (isset($conf->global->MAIN_START_WEEK)?$conf->global->MAIN_START_WEEK:1);
  331. $tmparray = dol_getdate($date,true);
  332. //Calculate days to count
  333. $days = $start_week - $tmparray['wday'];
  334. if ($days>=1) $days=7-$days;
  335. $days = abs($days);
  336. $seconds = $days*24*60*60;
  337. //Get first day of week
  338. $tmpday = date($tmparray[0])-$seconds;
  339. $tmpday = date("d",$tmpday);
  340. //Check first day of week is form this month or not
  341. if ($tmpday>$day)
  342. {
  343. $prev_month = $month-1;
  344. $prev_year = $year;
  345. if ($prev_month==0)
  346. {
  347. $prev_month = 12;
  348. $prev_year = $year-1;
  349. }
  350. }
  351. else
  352. {
  353. $prev_month = $month;
  354. $prev_year = $year;
  355. }
  356. //Get first day of next week
  357. $tmptime=dol_mktime(12,0,0,$month,$tmpday,$year,1,0);
  358. $tmptime-=24*60*60*7;
  359. $tmparray=dol_getdate($tmptime,true);
  360. $prev_day = $tmparray['mday'];
  361. //Check first day of week is form this month or not
  362. if ($prev_day>$tmpday)
  363. {
  364. $prev_month = $month-1;
  365. $prev_year = $year;
  366. if ($prev_month==0)
  367. {
  368. $prev_month = 12;
  369. $prev_year = $year-1;
  370. }
  371. }
  372. $week = date("W",dol_mktime(0,0,0,$month,$tmpday,$year,$gm));
  373. return array('year' => $year, 'month' => $month, 'week' => $week, 'first_day' => $tmpday, 'prev_year' => $prev_year, 'prev_month' => $prev_month, 'prev_day' => $prev_day);
  374. }
  375. /**
  376. * Fonction retournant le nombre de jour fieries samedis et dimanches entre 2 dates entrees en timestamp
  377. * Called by function num_open_day
  378. *
  379. * @param timestamp $timestampStart Timestamp de debut
  380. * @param timestamp $timestampEnd Timestamp de fin
  381. * @param string $countrycode Country code
  382. * @return int Nombre de jours feries
  383. */
  384. function num_public_holiday($timestampStart, $timestampEnd, $countrycode='FR')
  385. {
  386. $nbFerie = 0;
  387. while ($timestampStart != $timestampEnd)
  388. {
  389. $ferie=false;
  390. $countryfound=0;
  391. $jour = date("d", $timestampStart);
  392. $mois = date("m", $timestampStart);
  393. $annee = date("Y", $timestampStart);
  394. if ($countrycode == 'FR')
  395. {
  396. $countryfound=1;
  397. // Definition des dates feriees fixes
  398. if($jour == 1 && $mois == 1) $ferie=true; // 1er janvier
  399. if($jour == 1 && $mois == 5) $ferie=true; // 1er mai
  400. if($jour == 8 && $mois == 5) $ferie=true; // 5 mai
  401. if($jour == 14 && $mois == 7) $ferie=true; // 14 juillet
  402. if($jour == 15 && $mois == 8) $ferie=true; // 15 aout
  403. if($jour == 1 && $mois == 11) $ferie=true; // 1 novembre
  404. if($jour == 11 && $mois == 11) $ferie=true; // 11 novembre
  405. if($jour == 25 && $mois == 12) $ferie=true; // 25 decembre
  406. // Calcul du jour de paques
  407. $date_paques = easter_date($annee);
  408. $jour_paques = date("d", $date_paques);
  409. $mois_paques = date("m", $date_paques);
  410. if($jour_paques == $jour && $mois_paques == $mois) $ferie=true;
  411. // Paques
  412. // Calcul du jour de l ascension (38 jours apres Paques)
  413. $date_ascension = mktime(
  414. date("H", $date_paques),
  415. date("i", $date_paques),
  416. date("s", $date_paques),
  417. date("m", $date_paques),
  418. date("d", $date_paques) + 38,
  419. date("Y", $date_paques)
  420. );
  421. $jour_ascension = date("d", $date_ascension);
  422. $mois_ascension = date("m", $date_ascension);
  423. if($jour_ascension == $jour && $mois_ascension == $mois) $ferie=true;
  424. //Ascension
  425. // Calcul de Pentecote (11 jours apres Paques)
  426. $date_pentecote = mktime(
  427. date("H", $date_ascension),
  428. date("i", $date_ascension),
  429. date("s", $date_ascension),
  430. date("m", $date_ascension),
  431. date("d", $date_ascension) + 11,
  432. date("Y", $date_ascension)
  433. );
  434. $jour_pentecote = date("d", $date_pentecote);
  435. $mois_pentecote = date("m", $date_pentecote);
  436. if($jour_pentecote == $jour && $mois_pentecote == $mois) $ferie=true;
  437. //Pentecote
  438. // Calul des samedis et dimanches
  439. $jour_julien = unixtojd($timestampStart);
  440. $jour_semaine = jddayofweek($jour_julien, 0);
  441. if($jour_semaine == 0 || $jour_semaine == 6) $ferie=true;
  442. //Samedi (6) et dimanche (0)
  443. }
  444. // Pentecoste and Ascensione in Italy go to the sunday after: isn't holiday.
  445. // Pentecoste is 50 days after Easter, Ascensione 40
  446. if ($countrycode == 'IT')
  447. {
  448. $countryfound=1;
  449. // Definition des dates feriees fixes
  450. if($jour == 1 && $mois == 1) $ferie=true; // Capodanno
  451. if($jour == 6 && $mois == 1) $ferie=true; // Epifania
  452. if($jour == 25 && $mois == 4) $ferie=true; // Anniversario Liberazione
  453. if($jour == 1 && $mois == 5) $ferie=true; // Festa del Lavoro
  454. if($jour == 2 && $mois == 6) $ferie=true; // Festa della Repubblica
  455. if($jour == 15 && $mois == 8) $ferie=true; // Ferragosto
  456. if($jour == 1 && $mois == 11) $ferie=true; // Tutti i Santi
  457. if($jour == 8 && $mois == 12) $ferie=true; // Immacolata Concezione
  458. if($jour == 25 && $mois == 12) $ferie=true; // 25 decembre
  459. if($jour == 26 && $mois == 12) $ferie=true; // Santo Stefano
  460. // Calcul du jour de paques
  461. $date_paques = easter_date($annee);
  462. $jour_paques = date("d", $date_paques);
  463. $mois_paques = date("m", $date_paques);
  464. if($jour_paques == $jour && $mois_paques == $mois) $ferie=true;
  465. // Paques
  466. // Calul des samedis et dimanches
  467. $jour_julien = unixtojd($timestampStart);
  468. $jour_semaine = jddayofweek($jour_julien, 0);
  469. if($jour_semaine == 0 || $jour_semaine == 6) $ferie=true;
  470. //Samedi (6) et dimanche (0)
  471. }
  472. // Cas pays non defini
  473. if (! $countryfound)
  474. {
  475. // Calul des samedis et dimanches
  476. $jour_julien = unixtojd($timestampStart);
  477. $jour_semaine = jddayofweek($jour_julien, 0);
  478. if($jour_semaine == 0 || $jour_semaine == 6) $ferie=true;
  479. //Samedi (6) et dimanche (0)
  480. }
  481. // On incremente compteur
  482. if ($ferie) $nbFerie++;
  483. // Incrementation du nombre de jour (on avance dans la boucle)
  484. $jour++;
  485. $timestampStart=mktime(0,0,0,$mois,$jour,$annee);
  486. }
  487. return $nbFerie;
  488. }
  489. /**
  490. * Fonction retournant le nombre de jour entre deux dates
  491. *
  492. * @param timestamp $timestampStart Timestamp de debut
  493. * @param timestamp $timestampEnd Timestamp de fin
  494. * @param int $lastday On prend en compte le dernier jour, 0: non, 1:oui
  495. * @return int Nombre de jours
  496. */
  497. function num_between_day($timestampStart, $timestampEnd, $lastday=0)
  498. {
  499. if ($timestampStart < $timestampEnd)
  500. {
  501. if ($lastday == 1)
  502. {
  503. $bit = 0;
  504. }
  505. else
  506. {
  507. $bit = 1;
  508. }
  509. $nbjours = round(($timestampEnd - $timestampStart)/(60*60*24)-$bit);
  510. }
  511. return $nbjours;
  512. }
  513. /**
  514. * Fonction retournant le nombre de jour entre deux dates sans les jours feries (jours ouvres)
  515. *
  516. * @param timestamp $timestampStart Timestamp de debut
  517. * @param timestamp $timestampEnd Timestamp de fin
  518. * @param int $inhour 0: sort le nombre de jour , 1: sort le nombre d'heure (72 max)
  519. * @param int $lastday On prend en compte le dernier jour, 0: non, 1:oui
  520. * @return int Nombre de jours ou d'heures
  521. */
  522. function num_open_day($timestampStart, $timestampEnd,$inhour=0,$lastday=0)
  523. {
  524. global $langs;
  525. if ($timestampStart < $timestampEnd)
  526. {
  527. $bit = 0;
  528. if ($lastday == 1) $bit = 1;
  529. $nbOpenDay = num_between_day($timestampStart, $timestampEnd, $bit) - num_public_holiday($timestampStart, $timestampEnd);
  530. $nbOpenDay.= " ".$langs->trans("Days");
  531. if ($inhour == 1 && $nbOpenDay <= 3) $nbOpenDay = $nbOpenDay*24 . $langs->trans("HourShort");
  532. return $nbOpenDay;
  533. }
  534. else
  535. {
  536. return $langs->trans("Error");
  537. }
  538. }
  539. /**
  540. * Return array of translated months or selected month
  541. *
  542. * @param int $selected -1 to return array of all months or motnh to select
  543. * @return mixed Month string or array if selected < 0
  544. */
  545. function monthArrayOrSelected($selected=0)
  546. {
  547. global $langs;
  548. $month = array (
  549. 1 => $langs->trans("January"),
  550. 2 => $langs->trans("February"),
  551. 3 => $langs->trans("March"),
  552. 4 => $langs->trans("April"),
  553. 5 => $langs->trans("May"),
  554. 6 => $langs->trans("June"),
  555. 7 => $langs->trans("July"),
  556. 8 => $langs->trans("August"),
  557. 9 => $langs->trans("September"),
  558. 10 => $langs->trans("October"),
  559. 11 => $langs->trans("November"),
  560. 12 => $langs->trans("December")
  561. );
  562. if ($selected >=0)
  563. {
  564. $return='';
  565. foreach ($month as $key => $val)
  566. {
  567. if ($selected == $key)
  568. {
  569. $return = $val;
  570. break;
  571. }
  572. }
  573. return $return;
  574. }
  575. else
  576. {
  577. return $month;
  578. }
  579. }
  580. ?>