dolibarr  16.0.5
date.lib.php
Go to the documentation of this file.
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  * Copyright (C) 2018 Charlene Benke <charlie@patas-monkey.com>
7 *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  * or see https://www.gnu.org/
21  */
22 
34 function get_tz_array()
35 {
36  $tzarray = array(
37  -11=>"Pacific/Midway",
38  -10=>"Pacific/Fakaofo",
39  -9=>"America/Anchorage",
40  -8=>"America/Los_Angeles",
41  -7=>"America/Dawson_Creek",
42  -6=>"America/Chicago",
43  -5=>"America/Bogota",
44  -4=>"America/Anguilla",
45  -3=>"America/Araguaina",
46  -2=>"America/Noronha",
47  -1=>"Atlantic/Azores",
48  0=>"Africa/Abidjan",
49  1=>"Europe/Paris",
50  2=>"Europe/Helsinki",
51  3=>"Europe/Moscow",
52  4=>"Asia/Dubai",
53  5=>"Asia/Karachi",
54  6=>"Indian/Chagos",
55  7=>"Asia/Jakarta",
56  8=>"Asia/Hong_Kong",
57  9=>"Asia/Tokyo",
58  10=>"Australia/Sydney",
59  11=>"Pacific/Noumea",
60  12=>"Pacific/Auckland",
61  13=>"Pacific/Enderbury"
62  );
63  return $tzarray;
64 }
65 
66 
73 {
74  return @date_default_timezone_get();
75 }
76 
83 function getServerTimeZoneInt($refgmtdate = 'now')
84 {
85  if (method_exists('DateTimeZone', 'getOffset')) {
86  // Method 1 (include daylight)
87  $gmtnow = dol_now('gmt');
88  $yearref = dol_print_date($gmtnow, '%Y');
89  $monthref = dol_print_date($gmtnow, '%m');
90  $dayref = dol_print_date($gmtnow, '%d');
91  if ($refgmtdate == 'now') {
92  $newrefgmtdate = $yearref.'-'.$monthref.'-'.$dayref;
93  } elseif ($refgmtdate == 'summer') {
94  $newrefgmtdate = $yearref.'-08-01';
95  } else {
96  $newrefgmtdate = $yearref.'-01-01';
97  }
98  $newrefgmtdate .= 'T00:00:00+00:00';
99  $localtz = new DateTimeZone(getServerTimeZoneString());
100  $localdt = new DateTime($newrefgmtdate, $localtz);
101  $tmp = -1 * $localtz->getOffset($localdt);
102  //print $refgmtdate.'='.$tmp;
103  } else {
104  $tmp = 0;
105  dol_print_error('', 'PHP version must be 5.3+');
106  }
107  $tz = round(($tmp < 0 ? 1 : -1) * abs($tmp / 3600));
108  return $tz;
109 }
110 
111 
121 function dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth = 0)
122 {
123  global $conf;
124 
125  if ($duration_value == 0) {
126  return $time;
127  }
128  if ($duration_unit == 'i') {
129  return $time + (60 * $duration_value);
130  }
131  if ($duration_unit == 'h') {
132  return $time + (3600 * $duration_value);
133  }
134  if ($duration_unit == 'w') {
135  return $time + (3600 * 24 * 7 * $duration_value);
136  }
137 
138  $deltastring = 'P';
139 
140  if ($duration_value > 0) {
141  $deltastring .= abs($duration_value);
142  $sub = false;
143  }
144  if ($duration_value < 0) {
145  $deltastring .= abs($duration_value);
146  $sub = true;
147  }
148  if ($duration_unit == 'd') {
149  $deltastring .= "D";
150  }
151  if ($duration_unit == 'm') {
152  $deltastring .= "M";
153  }
154  if ($duration_unit == 'y') {
155  $deltastring .= "Y";
156  }
157 
158  $date = new DateTime();
159  if (!empty($conf->global->MAIN_DATE_IN_MEMORY_ARE_GMT)) {
160  $date->setTimezone(new DateTimeZone('UTC'));
161  }
162  $date->setTimestamp($time);
163  $interval = new DateInterval($deltastring);
164 
165  if ($sub) {
166  $date->sub($interval);
167  } else {
168  $date->add($interval);
169  }
170  //Change the behavior of PHP over data-interval when the result of this function is Feb 29 (non-leap years), 30 or Feb 31 (php returns March 1, 2 or 3 respectively)
171  if ($ruleforendofmonth == 1 && $duration_unit == 'm') {
172  $timeyear = dol_print_date($time, '%Y');
173  $timemonth = dol_print_date($time, '%m');
174  $timetotalmonths = (($timeyear * 12) + $timemonth);
175 
176  $monthsexpected = ($timetotalmonths + $duration_value);
177 
178  $newtime = $date->getTimestamp();
179 
180  $newtimeyear = dol_print_date($newtime, '%Y');
181  $newtimemonth = dol_print_date($newtime, '%m');
182  $newtimetotalmonths = (($newtimeyear * 12) + $newtimemonth);
183 
184  if ($monthsexpected < $newtimetotalmonths) {
185  $newtimehours = dol_print_date($newtime, '%H');
186  $newtimemins = dol_print_date($newtime, '%M');
187  $newtimesecs = dol_print_date($newtime, '%S');
188 
189  $datelim = dol_mktime($newtimehours, $newtimemins, $newtimesecs, $newtimemonth, 1, $newtimeyear);
190  $datelim -= (3600 * 24);
191 
192  $date->setTimestamp($datelim);
193  }
194  }
195  return $date->getTimestamp();
196 }
197 
198 
208 function convertTime2Seconds($iHours = 0, $iMinutes = 0, $iSeconds = 0)
209 {
210  $iResult = ($iHours * 3600) + ($iMinutes * 60) + $iSeconds;
211  return $iResult;
212 }
213 
214 
236 function convertSecondToTime($iSecond, $format = 'all', $lengthOfDay = 86400, $lengthOfWeek = 7)
237 {
238  global $langs;
239 
240  if (empty($lengthOfDay)) {
241  $lengthOfDay = 86400; // 1 day = 24 hours
242  }
243  if (empty($lengthOfWeek)) {
244  $lengthOfWeek = 7; // 1 week = 7 days
245  }
246 
247  if ($format == 'all' || $format == 'allwithouthour' || $format == 'allhour' || $format == 'allhourmin' || $format == 'allhourminsec') {
248  if ((int) $iSecond === 0) {
249  return '0'; // This is to avoid having 0 return a 12:00 AM for en_US
250  }
251 
252  $sTime = '';
253  $sDay = 0;
254  $sWeek = 0;
255 
256  if ($iSecond >= $lengthOfDay) {
257  for ($i = $iSecond; $i >= $lengthOfDay; $i -= $lengthOfDay) {
258  $sDay++;
259  $iSecond -= $lengthOfDay;
260  }
261  $dayTranslate = $langs->trans("Day");
262  if ($iSecond >= ($lengthOfDay * 2)) {
263  $dayTranslate = $langs->trans("Days");
264  }
265  }
266 
267  if ($lengthOfWeek < 7) {
268  if ($sDay) {
269  if ($sDay >= $lengthOfWeek) {
270  $sWeek = (int) (($sDay - $sDay % $lengthOfWeek) / $lengthOfWeek);
271  $sDay = $sDay % $lengthOfWeek;
272  $weekTranslate = $langs->trans("DurationWeek");
273  if ($sWeek >= 2) {
274  $weekTranslate = $langs->trans("DurationWeeks");
275  }
276  $sTime .= $sWeek.' '.$weekTranslate.' ';
277  }
278  }
279  }
280  if ($sDay > 0) {
281  $dayTranslate = $langs->trans("Day");
282  if ($sDay > 1) {
283  $dayTranslate = $langs->trans("Days");
284  }
285  $sTime .= $sDay.' '.strtolower(dol_substr($dayTranslate, 0, 1)).'. ';
286  }
287 
288  if ($format == 'all') {
289  if ($iSecond || empty($sDay)) {
290  $sTime .= dol_print_date($iSecond, 'hourduration', true);
291  }
292  } elseif ($format == 'allhourminsec') {
293  return sprintf("%02d", ($sWeek * $lengthOfWeek * 24 + $sDay * 24 + (int) floor($iSecond / 3600))).':'.sprintf("%02d", ((int) floor(($iSecond % 3600) / 60))).':'.sprintf("%02d", ((int) ($iSecond % 60)));
294  } elseif ($format == 'allhourmin') {
295  return sprintf("%02d", ($sWeek * $lengthOfWeek * 24 + $sDay * 24 + (int) floor($iSecond / 3600))).':'.sprintf("%02d", ((int) floor(($iSecond % 3600) / 60)));
296  } elseif ($format == 'allhour') {
297  return sprintf("%02d", ($sWeek * $lengthOfWeek * 24 + $sDay * 24 + (int) floor($iSecond / 3600)));
298  }
299  } elseif ($format == 'hour') { // only hour part
300  $sTime = dol_print_date($iSecond, '%H', true);
301  } elseif ($format == 'fullhour') {
302  if (!empty($iSecond)) {
303  $iSecond = $iSecond / 3600;
304  } else {
305  $iSecond = 0;
306  }
307  $sTime = $iSecond;
308  } elseif ($format == 'min') { // only min part
309  $sTime = dol_print_date($iSecond, '%M', true);
310  } elseif ($format == 'sec') { // only sec part
311  $sTime = dol_print_date($iSecond, '%S', true);
312  } elseif ($format == 'month') { // only month part
313  $sTime = dol_print_date($iSecond, '%m', true);
314  } elseif ($format == 'year') { // only year part
315  $sTime = dol_print_date($iSecond, '%Y', true);
316  }
317  return trim($sTime);
318 }
319 
320 
334 function dolSqlDateFilter($datefield, $day_date, $month_date, $year_date, $excludefirstand = 0, $gm = false)
335 {
336  global $db;
337  $sqldate = '';
338 
339  $day_date = intval($day_date);
340  $month_date = intval($month_date);
341  $year_date = intval($year_date);
342 
343  if ($month_date > 0) {
344  if ($month_date > 12) { // protection for bad value of month
345  return " AND 1 = 2";
346  }
347  if ($year_date > 0 && empty($day_date)) {
348  $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, $month_date, $gm));
349  $sqldate .= "' AND '".$db->idate(dol_get_last_day($year_date, $month_date, $gm))."'";
350  } elseif ($year_date > 0 && !empty($day_date)) {
351  $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month_date, $day_date, $year_date, $gm));
352  $sqldate .= "' AND '".$db->idate(dol_mktime(23, 59, 59, $month_date, $day_date, $year_date, $gm))."'";
353  } else {
354  // This case is not reliable on TZ, but we should not need it.
355  $sqldate .= ($excludefirstand ? "" : " AND ")." date_format( ".$datefield.", '%c') = '".$db->escape($month_date)."'";
356  }
357  } elseif ($year_date > 0) {
358  $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, 1, $gm));
359  $sqldate .= "' AND '".$db->idate(dol_get_last_day($year_date, 12, $gm))."'";
360  }
361  return $sqldate;
362 }
363 
383 function dol_stringtotime($string, $gm = 1)
384 {
385  $reg = array();
386  // Convert date with format DD/MM/YYY HH:MM:SS. This part of code should not be used.
387  if (preg_match('/^([0-9]+)\/([0-9]+)\/([0-9]+)\s?([0-9]+)?:?([0-9]+)?:?([0-9]+)?/i', $string, $reg)) {
388  dol_syslog("dol_stringtotime call to function with deprecated parameter format", LOG_WARNING);
389  // Date est au format 'DD/MM/YY' ou 'DD/MM/YY HH:MM:SS'
390  // Date est au format 'DD/MM/YYYY' ou 'DD/MM/YYYY HH:MM:SS'
391  $sday = $reg[1];
392  $smonth = $reg[2];
393  $syear = $reg[3];
394  $shour = $reg[4];
395  $smin = $reg[5];
396  $ssec = $reg[6];
397  if ($syear < 50) {
398  $syear += 1900;
399  }
400  if ($syear >= 50 && $syear < 100) {
401  $syear += 2000;
402  }
403  $string = sprintf("%04d%02d%02d%02d%02d%02d", $syear, $smonth, $sday, $shour, $smin, $ssec);
404  } elseif (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)
405  || 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
406  || 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
407  ) {
408  $syear = $reg[1];
409  $smonth = $reg[2];
410  $sday = $reg[3];
411  $shour = $reg[4];
412  $smin = $reg[5];
413  $ssec = $reg[6];
414  $string = sprintf("%04d%02d%02d%02d%02d%02d", $syear, $smonth, $sday, $shour, $smin, $ssec);
415  }
416 
417  $string = preg_replace('/([^0-9])/i', '', $string);
418  $tmp = $string.'000000';
419  // Clean $gm
420  if ($gm === 1) {
421  $gm = 'gmt';
422  } elseif (empty($gm) || $gm === 'tzserver') {
423  $gm = 'tzserver';
424  }
425 
426  $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);
427  return $date;
428 }
429 
430 
439 function dol_get_prev_day($day, $month, $year)
440 {
441  $time = dol_mktime(12, 0, 0, $month, $day, $year, 1, 0);
442  $time -= 24 * 60 * 60;
443  $tmparray = dol_getdate($time, true);
444  return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
445 }
446 
455 function dol_get_next_day($day, $month, $year)
456 {
457  $time = dol_mktime(12, 0, 0, $month, $day, $year, 1, 0);
458  $time += 24 * 60 * 60;
459  $tmparray = dol_getdate($time, true);
460  return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
461 }
462 
470 function dol_get_prev_month($month, $year)
471 {
472  if ($month == 1) {
473  $prev_month = 12;
474  $prev_year = $year - 1;
475  } else {
476  $prev_month = $month - 1;
477  $prev_year = $year;
478  }
479  return array('year' => $prev_year, 'month' => $prev_month);
480 }
481 
489 function dol_get_next_month($month, $year)
490 {
491  if ($month == 12) {
492  $next_month = 1;
493  $next_year = $year + 1;
494  } else {
495  $next_month = $month + 1;
496  $next_year = $year;
497  }
498  return array('year' => $next_year, 'month' => $next_month);
499 }
500 
510 function dol_get_prev_week($day, $week, $month, $year)
511 {
512  $tmparray = dol_get_first_day_week($day, $month, $year);
513 
514  $time = dol_mktime(12, 0, 0, $month, $tmparray['first_day'], $year, 1, 0);
515  $time -= 24 * 60 * 60 * 7;
516  $tmparray = dol_getdate($time, true);
517  return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
518 }
519 
529 function dol_get_next_week($day, $week, $month, $year)
530 {
531  $tmparray = dol_get_first_day_week($day, $month, $year);
532 
533  $time = dol_mktime(12, 0, 0, $tmparray['first_month'], $tmparray['first_day'], $tmparray['first_year'], 1, 0);
534  $time += 24 * 60 * 60 * 7;
535  $tmparray = dol_getdate($time, true);
536 
537  return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
538 }
539 
551 function dol_get_first_day($year, $month = 1, $gm = false)
552 {
553  if ($year > 9999) {
554  return '';
555  }
556  return dol_mktime(0, 0, 0, $month, 1, $year, $gm);
557 }
558 
559 
570 function dol_get_last_day($year, $month = 12, $gm = false)
571 {
572  if ($year > 9999) {
573  return '';
574  }
575  if ($month == 12) {
576  $month = 1;
577  $year += 1;
578  } else {
579  $month += 1;
580  }
581 
582  // On se deplace au debut du mois suivant, et on retire un jour
583  $datelim = dol_mktime(23, 59, 59, $month, 1, $year, $gm);
584  $datelim -= (3600 * 24);
585 
586  return $datelim;
587 }
588 
597 function dol_get_last_hour($date, $gm = 'tzserver')
598 {
599  $tmparray = dol_getdate($date, false, ($gm == 'gmt' ? 'gmt' : ''));
600  return dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year'], $gm);
601 }
602 
611 function dol_get_first_hour($date, $gm = 'tzserver')
612 {
613  $tmparray = dol_getdate($date, false, ($gm == 'gmt' ? 'gmt' : ''));
614  return dol_mktime(0, 0, 0, $tmparray['mon'], $tmparray['mday'], $tmparray['year'], $gm);
615 }
616 
626 function dol_get_first_day_week($day, $month, $year, $gm = false)
627 {
628  global $conf;
629 
630  //$day=2; $month=2; $year=2015;
631  $date = dol_mktime(0, 0, 0, $month, $day, $year, $gm);
632 
633  //Checking conf of start week
634  $start_week = (isset($conf->global->MAIN_START_WEEK) ? $conf->global->MAIN_START_WEEK : 1);
635 
636  $tmparray = dol_getdate($date, true); // detail of current day
637 
638  //Calculate days = offset from current day
639  $days = $start_week - $tmparray['wday'];
640  if ($days >= 1) {
641  $days = 7 - $days;
642  }
643  $days = abs($days);
644  $seconds = $days * 24 * 60 * 60;
645  //print 'start_week='.$start_week.' tmparray[wday]='.$tmparray['wday'].' day offset='.$days.' seconds offset='.$seconds.'<br>';
646 
647  //Get first day of week
648  $tmpdaytms = date($tmparray[0]) - $seconds; // $tmparray[0] is day of parameters
649  $tmpday = date("d", $tmpdaytms);
650 
651  //Check first day of week is in same month than current day or not
652  if ($tmpday > $day) {
653  $prev_month = $month - 1;
654  $prev_year = $year;
655 
656  if ($prev_month == 0) {
657  $prev_month = 12;
658  $prev_year = $year - 1;
659  }
660  } else {
661  $prev_month = $month;
662  $prev_year = $year;
663  }
664  $tmpmonth = $prev_month;
665  $tmpyear = $prev_year;
666 
667  //Get first day of next week
668  $tmptime = dol_mktime(12, 0, 0, $month, $tmpday, $year, 1, 0);
669  $tmptime -= 24 * 60 * 60 * 7;
670  $tmparray = dol_getdate($tmptime, true);
671  $prev_day = $tmparray['mday'];
672 
673  //Check prev day of week is in same month than first day or not
674  if ($prev_day > $tmpday) {
675  $prev_month = $month - 1;
676  $prev_year = $year;
677 
678  if ($prev_month == 0) {
679  $prev_month = 12;
680  $prev_year = $year - 1;
681  }
682  }
683 
684  $week = date("W", dol_mktime(0, 0, 0, $tmpmonth, $tmpday, $tmpyear, $gm));
685 
686  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);
687 }
688 
696 function getGMTEasterDatetime($year)
697 {
698  $base = new DateTime("$year-03-21", new DateTimeZone("UTC"));
699  $days = easter_days($year); // Return number of days between 21 march and easter day.
700  $tmp = $base->add(new DateInterval("P{$days}D"));
701  return $tmp->getTimestamp();
702 }
703 
720 function num_public_holiday($timestampStart, $timestampEnd, $country_code = '', $lastday = 0, $includesaturday = -1, $includesunday = -1, $includefriday = -1, $includemonday = -1)
721 {
722  global $db, $conf, $mysoc;
723 
724  $nbFerie = 0;
725 
726  // Check to ensure we use correct parameters
727  if ((($timestampEnd - $timestampStart) % 86400) != 0) {
728  return 'Error Dates must use same hours and must be GMT dates';
729  }
730 
731  if (empty($country_code)) {
732  $country_code = $mysoc->country_code;
733  }
734  if ($includemonday < 0) {
735  $includemonday = (isset($conf->global->MAIN_NON_WORKING_DAYS_INCLUDE_MONDAY) ? $conf->global->MAIN_NON_WORKING_DAYS_INCLUDE_MONDAY : 0);
736  }
737  if ($includefriday < 0) {
738  $includefriday = (isset($conf->global->MAIN_NON_WORKING_DAYS_INCLUDE_FRIDAY) ? $conf->global->MAIN_NON_WORKING_DAYS_INCLUDE_FRIDAY : 0);
739  }
740  if ($includesaturday < 0) {
741  $includesaturday = (isset($conf->global->MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY) ? $conf->global->MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY : 1);
742  }
743  if ($includesunday < 0) {
744  $includesunday = (isset($conf->global->MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY) ? $conf->global->MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY : 1);
745  }
746 
747  $country_id = dol_getIdFromCode($db, $country_code, 'c_country', 'code', 'rowid');
748 
749  $i = 0;
750  while ((($lastday == 0 && $timestampStart < $timestampEnd) || ($lastday && $timestampStart <= $timestampEnd))
751  && ($i < 50000)) { // Loop end when equals (Test on i is a security loop to avoid infinite loop)
752  $ferie = false;
753  $specialdayrule = array();
754 
755  $jour = gmdate("d", $timestampStart);
756  $mois = gmdate("m", $timestampStart);
757  $annee = gmdate("Y", $timestampStart);
758 
759  //print "jour=".$jour." month=".$mois." year=".$annee." includesaturday=".$includesaturday." includesunday=".$includesunday."\n";
760 
761  // Loop on public holiday defined into hrm_public_holiday for the day, month and year analyzed
762  // TODO Execute this request first and store results into an array, then reuse this array.
763  $sql = "SELECT code, entity, fk_country, dayrule, year, month, day, active";
764  $sql .= " FROM ".MAIN_DB_PREFIX."c_hrm_public_holiday";
765  $sql .= " WHERE active = 1 and fk_country IN (0".($country_id > 0 ? ", ".$country_id : 0).")";
766  $sql .= " AND entity IN (0," .getEntity('holiday') .")";
767 
768  $resql = $db->query($sql);
769  if ($resql) {
770  $num_rows = $db->num_rows($resql);
771  $i = 0;
772  while ($i < $num_rows) {
773  $obj = $db->fetch_object($resql);
774 
775  if (!empty($obj->dayrule) && $obj->dayrule != 'date') { // For example 'easter', '...'
776  $specialdayrule[$obj->dayrule] = $obj->dayrule;
777  } else {
778  $match = 1;
779  if (!empty($obj->year) && $obj->year != $annee) {
780  $match = 0;
781  }
782  if ($obj->month != $mois) {
783  $match = 0;
784  }
785  if ($obj->day != $jour) {
786  $match = 0;
787  }
788 
789  if ($match) {
790  $ferie = true;
791  }
792  }
793 
794  $i++;
795  }
796  } else {
797  dol_syslog($db->lasterror(), LOG_ERR);
798  return 'Error sql '.$db->lasterror();
799  }
800  //var_dump($specialdayrule)."\n";
801  //print "ferie=".$ferie."\n";
802 
803  if (!$ferie) {
804  // Special dayrules
805  if (in_array('easter', $specialdayrule)) {
806  // Calculation for easter date
807  $date_paques = getGMTEasterDatetime($annee);
808  $jour_paques = gmdate("d", $date_paques);
809  $mois_paques = gmdate("m", $date_paques);
810  if ($jour_paques == $jour && $mois_paques == $mois) {
811  $ferie = true;
812  }
813  // Easter (sunday)
814  }
815 
816  if (in_array('eastermonday', $specialdayrule)) {
817  // Calculation for the monday of easter date
818  $date_paques = getGMTEasterDatetime($annee);
819  //print 'PPP'.$date_paques.' '.dol_print_date($date_paques, 'dayhour', 'gmt')." ";
820  $date_lundi_paques = $date_paques + (3600 * 24);
821  $jour_lundi_paques = gmdate("d", $date_lundi_paques);
822  $mois_lundi_paques = gmdate("m", $date_lundi_paques);
823  if ($jour_lundi_paques == $jour && $mois_lundi_paques == $mois) {
824  $ferie = true;
825  }
826  // Easter (monday)
827  //print 'annee='.$annee.' $jour='.$jour.' $mois='.$mois.' $jour_lundi_paques='.$jour_lundi_paques.' $mois_lundi_paques='.$mois_lundi_paques."\n";
828  }
829 
830  //Good Friday
831  if (in_array('goodfriday', $specialdayrule)) {
832  // Pulls the date of Easter
833  $easter = getGMTEasterDatetime($annee);
834 
835  // Calculates the date of Good Friday based on Easter
836  $date_good_friday = $easter - (2 * 3600 * 24);
837  $dom_good_friday = gmdate("d", $date_good_friday);
838  $month_good_friday = gmdate("m", $date_good_friday);
839 
840  if ($dom_good_friday == $jour && $month_good_friday == $mois) {
841  $ferie = true;
842  }
843  }
844 
845  if (in_array('ascension', $specialdayrule)) {
846  // Calcul du jour de l'ascension (39 days after easter day)
847  $date_paques = getGMTEasterDatetime($annee);
848  $date_ascension = $date_paques + (3600 * 24 * 39);
849  $jour_ascension = gmdate("d", $date_ascension);
850  $mois_ascension = gmdate("m", $date_ascension);
851  if ($jour_ascension == $jour && $mois_ascension == $mois) {
852  $ferie = true;
853  }
854  // Ascension (thursday)
855  }
856 
857  if (in_array('pentecote', $specialdayrule)) {
858  // Calculation of "Pentecote" (49 days after easter day)
859  $date_paques = getGMTEasterDatetime($annee);
860  $date_pentecote = $date_paques + (3600 * 24 * 49);
861  $jour_pentecote = gmdate("d", $date_pentecote);
862  $mois_pentecote = gmdate("m", $date_pentecote);
863  if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
864  $ferie = true;
865  }
866  // "Pentecote" (sunday)
867  }
868  if (in_array('pentecotemonday', $specialdayrule)) {
869  // Calculation of "Pentecote" (49 days after easter day)
870  $date_paques = getGMTEasterDatetime($annee);
871  $date_pentecote = $date_paques + (3600 * 24 * 50);
872  $jour_pentecote = gmdate("d", $date_pentecote);
873  $mois_pentecote = gmdate("m", $date_pentecote);
874  if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
875  $ferie = true;
876  }
877  // "Pentecote" (monday)
878  }
879 
880  if (in_array('viernessanto', $specialdayrule)) {
881  // Viernes Santo
882  $date_paques = getGMTEasterDatetime($annee);
883  $date_viernes = $date_paques - (3600 * 24 * 2);
884  $jour_viernes = gmdate("d", $date_viernes);
885  $mois_viernes = gmdate("m", $date_viernes);
886  if ($jour_viernes == $jour && $mois_viernes == $mois) {
887  $ferie = true;
888  }
889  //Viernes Santo
890  }
891 
892  if (in_array('fronleichnam', $specialdayrule)) {
893  // Fronleichnam (60 days after easter sunday)
894  $date_paques = getGMTEasterDatetime($annee);
895  $date_fronleichnam = $date_paques + (3600 * 24 * 60);
896  $jour_fronleichnam = gmdate("d", $date_fronleichnam);
897  $mois_fronleichnam = gmdate("m", $date_fronleichnam);
898  if ($jour_fronleichnam == $jour && $mois_fronleichnam == $mois) {
899  $ferie = true;
900  }
901  // Fronleichnam
902  }
903  }
904  //print "ferie=".$ferie."\n";
905 
906  // If we have to include Friday, Saturday and Sunday
907  if (!$ferie) {
908  if ($includefriday || $includesaturday || $includesunday) {
909  $jour_julien = unixtojd($timestampStart);
910  $jour_semaine = jddayofweek($jour_julien, 0);
911  if ($includefriday) { //Friday (5), Saturday (6) and Sunday (0)
912  if ($jour_semaine == 5) {
913  $ferie = true;
914  }
915  }
916  if ($includesaturday) { //Friday (5), Saturday (6) and Sunday (0)
917  if ($jour_semaine == 6) {
918  $ferie = true;
919  }
920  }
921  if ($includesunday) { //Friday (5), Saturday (6) and Sunday (0)
922  if ($jour_semaine == 0) {
923  $ferie = true;
924  }
925  }
926  }
927  }
928  //print "ferie=".$ferie."\n";
929 
930  // We increase the counter of non working day
931  if ($ferie) {
932  $nbFerie++;
933  }
934 
935  // Increase number of days (on go up into loop)
936  $timestampStart = dol_time_plus_duree($timestampStart, 1, 'd');
937  //var_dump($jour.' '.$mois.' '.$annee.' '.$timestampStart);
938 
939  $i++;
940  }
941 
942  //print "nbFerie=".$nbFerie."\n";
943  return $nbFerie;
944 }
945 
956 function num_between_day($timestampStart, $timestampEnd, $lastday = 0)
957 {
958  if ($timestampStart < $timestampEnd) {
959  if ($lastday == 1) {
960  $bit = 0;
961  } else {
962  $bit = 1;
963  }
964  $nbjours = (int) floor(($timestampEnd - $timestampStart) / (60 * 60 * 24)) + 1 - $bit;
965  }
966  //print ($timestampEnd - $timestampStart) - $lastday;
967  return $nbjours;
968 }
969 
982 function num_open_day($timestampStart, $timestampEnd, $inhour = 0, $lastday = 0, $halfday = 0, $country_code = '')
983 {
984  global $langs, $mysoc;
985 
986  if (empty($country_code)) {
987  $country_code = $mysoc->country_code;
988  }
989 
990  dol_syslog('num_open_day timestampStart='.$timestampStart.' timestampEnd='.$timestampEnd.' bit='.$lastday.' country_code='.$country_code);
991 
992  // Check parameters
993  if (!is_int($timestampStart) && !is_float($timestampStart)) {
994  return 'ErrorBadParameter_num_open_day';
995  }
996  if (!is_int($timestampEnd) && !is_float($timestampEnd)) {
997  return 'ErrorBadParameter_num_open_day';
998  }
999 
1000  //print 'num_open_day timestampStart='.$timestampStart.' timestampEnd='.$timestampEnd.' bit='.$lastday;
1001  if ($timestampStart < $timestampEnd) {
1002  $numdays = num_between_day($timestampStart, $timestampEnd, $lastday);
1003 
1004  $numholidays = num_public_holiday($timestampStart, $timestampEnd, $country_code, $lastday);
1005  $nbOpenDay = ($numdays - $numholidays);
1006  if ($inhour == 1 && $nbOpenDay <= 3) {
1007  $nbOpenDay = ($nbOpenDay * 24);
1008  }
1009  return $nbOpenDay - (($inhour == 1 ? 12 : 0.5) * abs($halfday));
1010  } elseif ($timestampStart == $timestampEnd) {
1011  $numholidays = 0;
1012  if ($lastday) {
1013  $numholidays = num_public_holiday($timestampStart, $timestampEnd, $country_code, $lastday);
1014  if ($numholidays == 1) {
1015  return 0;
1016  }
1017  }
1018 
1019  $nbOpenDay = $lastday;
1020 
1021  if ($inhour == 1) {
1022  $nbOpenDay = ($nbOpenDay * 24);
1023  }
1024  return $nbOpenDay - (($inhour == 1 ? 12 : 0.5) * abs($halfday));
1025  } else {
1026  return $langs->trans("Error");
1027  }
1028 }
1029 
1030 
1031 
1040 function monthArray($outputlangs, $short = 0)
1041 {
1042  $montharray = array(
1043  1 => $outputlangs->trans("Month01"),
1044  2 => $outputlangs->trans("Month02"),
1045  3 => $outputlangs->trans("Month03"),
1046  4 => $outputlangs->trans("Month04"),
1047  5 => $outputlangs->trans("Month05"),
1048  6 => $outputlangs->trans("Month06"),
1049  7 => $outputlangs->trans("Month07"),
1050  8 => $outputlangs->trans("Month08"),
1051  9 => $outputlangs->trans("Month09"),
1052  10 => $outputlangs->trans("Month10"),
1053  11 => $outputlangs->trans("Month11"),
1054  12 => $outputlangs->trans("Month12")
1055  );
1056 
1057  if (!empty($short)) {
1058  $montharray = array(
1059  1 => $outputlangs->trans("MonthShort01"),
1060  2 => $outputlangs->trans("MonthShort02"),
1061  3 => $outputlangs->trans("MonthShort03"),
1062  4 => $outputlangs->trans("MonthShort04"),
1063  5 => $outputlangs->trans("MonthShort05"),
1064  6 => $outputlangs->trans("MonthShort06"),
1065  7 => $outputlangs->trans("MonthShort07"),
1066  8 => $outputlangs->trans("MonthShort08"),
1067  9 => $outputlangs->trans("MonthShort09"),
1068  10 => $outputlangs->trans("MonthShort10"),
1069  11 => $outputlangs->trans("MonthShort11"),
1070  12 => $outputlangs->trans("MonthShort12")
1071  );
1072  }
1073 
1074  return $montharray;
1075 }
1083 function getWeekNumbersOfMonth($month, $year)
1084 {
1085  $nb_days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
1086  $TWeek = array();
1087  for ($day = 1; $day < $nb_days; $day++) {
1088  $week_number = getWeekNumber($day, $month, $year);
1089  $TWeek[$week_number] = $week_number;
1090  }
1091  return $TWeek;
1092 }
1100 function getFirstDayOfEachWeek($TWeek, $year)
1101 {
1102  $TFirstDayOfWeek = array();
1103  foreach ($TWeek as $weekNb) {
1104  if (in_array('01', $TWeek) && in_array('52', $TWeek) && $weekNb == '01') {
1105  $year++; //Si on a la 1re semaine et la semaine 52 c'est qu'on change d'annĂ©e
1106  }
1107  $TFirstDayOfWeek[$weekNb] = date('d', strtotime($year.'W'.$weekNb));
1108  }
1109  return $TFirstDayOfWeek;
1110 }
1118 function getLastDayOfEachWeek($TWeek, $year)
1119 {
1120  $TLastDayOfWeek = array();
1121  foreach ($TWeek as $weekNb) {
1122  $TLastDayOfWeek[$weekNb] = date('d', strtotime($year.'W'.$weekNb.'+6 days'));
1123  }
1124  return $TLastDayOfWeek;
1125 }
1134 function getWeekNumber($day, $month, $year)
1135 {
1136  $date = new DateTime($year.'-'.$month.'-'.$day);
1137  $week = $date->format("W");
1138  return $week;
1139 }
dol_getdate
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
Definition: functions.lib.php:2713
dol_get_last_hour
dol_get_last_hour($date, $gm='tzserver')
Return GMT time for last hour of a given GMT date (it replaces hours, min and second part to 23:59:59...
Definition: date.lib.php:597
num_open_day
num_open_day($timestampStart, $timestampEnd, $inhour=0, $lastday=0, $halfday=0, $country_code='')
Function to return number of working days (and text of units) between two dates (working days)
Definition: date.lib.php:982
get_tz_array
get_tz_array()
Return an array with timezone values.
Definition: date.lib.php:34
getWeekNumber
getWeekNumber($day, $month, $year)
Return week number.
Definition: date.lib.php:1134
dol_substr
dol_substr($string, $start, $length, $stringencoding='', $trunconbytes=0)
Make a substring.
Definition: functions.lib.php:3766
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
convertSecondToTime
convertSecondToTime($iSecond, $format='all', $lengthOfDay=86400, $lengthOfWeek=7)
Return, in clear text, value of a number of seconds in days, hours and minutes.
Definition: date.lib.php:236
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
dol_get_prev_week
dol_get_prev_week($day, $week, $month, $year)
Return previous week.
Definition: date.lib.php:510
dol_getIdFromCode
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
Definition: functions.lib.php:8535
getWeekNumbersOfMonth
getWeekNumbersOfMonth($month, $year)
Return array of week numbers.
Definition: date.lib.php:1083
getLastDayOfEachWeek
getLastDayOfEachWeek($TWeek, $year)
Return array of last day of weeks.
Definition: date.lib.php:1118
dol_get_next_week
dol_get_next_week($day, $week, $month, $year)
Return next week.
Definition: date.lib.php:529
convertTime2Seconds
convertTime2Seconds($iHours=0, $iMinutes=0, $iSeconds=0)
Convert hours and minutes into seconds.
Definition: date.lib.php:208
dol_get_first_hour
dol_get_first_hour($date, $gm='tzserver')
Return GMT time for first hour of a given GMT date (it removes hours, min and second part)
Definition: date.lib.php:611
getServerTimeZoneInt
getServerTimeZoneInt($refgmtdate='now')
Return server timezone int.
Definition: date.lib.php:83
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
num_public_holiday
num_public_holiday($timestampStart, $timestampEnd, $country_code='', $lastday=0, $includesaturday=-1, $includesunday=-1, $includefriday=-1, $includemonday=-1)
Return the number of non working days including Friday, Saturday and Sunday (or not) between 2 dates ...
Definition: date.lib.php:720
getServerTimeZoneString
getServerTimeZoneString()
Return server timezone string.
Definition: date.lib.php:72
dol_get_first_day
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:551
num_between_day
num_between_day($timestampStart, $timestampEnd, $lastday=0)
Function to return number of days between two dates (date must be UTC date !) Example: 2012-01-01 201...
Definition: date.lib.php:956
dol_get_last_day
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:570
dol_get_prev_month
dol_get_prev_month($month, $year)
Return previous month.
Definition: date.lib.php:470
getFirstDayOfEachWeek
getFirstDayOfEachWeek($TWeek, $year)
Return array of first day of weeks.
Definition: date.lib.php:1100
dol_time_plus_duree
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition: date.lib.php:121
dol_get_next_month
dol_get_next_month($month, $year)
Return next month.
Definition: date.lib.php:489
dol_get_first_day_week
dol_get_first_day_week($day, $month, $year, $gm=false)
Return first day of week for a date.
Definition: date.lib.php:626
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
dolSqlDateFilter
dolSqlDateFilter($datefield, $day_date, $month_date, $year_date, $excludefirstand=0, $gm=false)
Generate a SQL string to make a filter into a range (for second of date until last second of date).
Definition: date.lib.php:334
monthArray
monthArray($outputlangs, $short=0)
Return array of translated months or selected month.
Definition: date.lib.php:1040
getGMTEasterDatetime
getGMTEasterDatetime($year)
Return the easter day in GMT time.
Definition: date.lib.php:696
dol_mktime
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
Definition: functions.lib.php:2757
dol_stringtotime
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
Definition: date.lib.php:383
dol_get_prev_day
dol_get_prev_day($day, $month, $year)
Return previous day.
Definition: date.lib.php:439
dol_get_next_day
dol_get_next_day($day, $month, $year)
Return next day.
Definition: date.lib.php:455