dolibarr 23.0.4
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-2024 Charlene Benke <charlene@patas-monkey.com>
7 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 * or see https://www.gnu.org/
23 */
24
36function get_tz_array()
37{
38 $tzarray = array(
39 -11 => "Pacific/Pago_Pago",
40 -10 => "Pacific/Honolulu",
41 -9 => "America/Anchorage",
42 -8 => "America/Los_Angeles",
43 -7 => "America/Dawson_Creek",
44 -6 => "America/Chicago",
45 -5 => "America/Bogota",
46 -4 => "America/Asuncion",
47 -3 => "America/Araguaina",
48 -2 => "America/Noronha",
49 -1 => "Atlantic/Azores",
50 0 => "Europe/London",
51 1 => "Europe/Paris",
52 2 => "Europe/Helsinki",
53 3 => "Europe/Moscow",
54 4 => "Asia/Dubai",
55 5 => "Asia/Karachi",
56 6 => "Indian/Chagos",
57 7 => "Asia/Jakarta",
58 8 => "Asia/Hong_Kong",
59 9 => "Asia/Tokyo",
60 10 => "Australia/Sydney",
61 11 => "Pacific/Noumea",
62 12 => "Pacific/Auckland",
63 13 => "Pacific/Fakaofo",
64 14 => "Pacific/Kiritimati"
65 );
66 return $tzarray;
67}
68
69
76{
77 return @date_default_timezone_get();
78}
79
86function getServerTimeZoneInt($refgmtdate = 'now')
87{
88 if (method_exists('DateTimeZone', 'getOffset')) {
89 // Method 1 (include daylight)
90 $gmtnow = dol_now('gmt');
91 $yearref = dol_print_date($gmtnow, '%Y');
92 $monthref = dol_print_date($gmtnow, '%m');
93 $dayref = dol_print_date($gmtnow, '%d');
94 if ($refgmtdate == 'now') {
95 $newrefgmtdate = $yearref.'-'.$monthref.'-'.$dayref;
96 } elseif ($refgmtdate == 'summer') {
97 $newrefgmtdate = $yearref.'-08-01';
98 } else {
99 $newrefgmtdate = $yearref.'-01-01';
100 }
101 $newrefgmtdate .= 'T00:00:00+00:00';
102 $localtz = new DateTimeZone(getServerTimeZoneString());
103 $localdt = new DateTime($newrefgmtdate, $localtz);
104 $tmp = -1 * $localtz->getOffset($localdt);
105 //print $refgmtdate.'='.$tmp;
106 } else {
107 $tmp = 0;
108 dol_print_error(null, 'PHP version must be 5.3+');
109 }
110 $tz = round(($tmp < 0 ? 1 : -1) * abs($tmp / 3600));
111 return $tz;
112}
113
114
128function dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth = 0, $tz = '')
129{
130 if (empty($duration_value)) {
131 return $time;
132 }
133 if ($duration_unit == 's') {
134 return $time + (int) ($duration_value);
135 }
136 if ($duration_unit == 'i' || $duration_unit == 'mn' || $duration_unit == 'min') {
137 return $time + (int) (60 * $duration_value);
138 }
139 if ($duration_unit == 'h') {
140 return $time + (int) (3600 * $duration_value);
141 }
142 if ($duration_unit == 'w') {
143 return $time + (int) (3600 * 24 * 7 * $duration_value);
144 }
145
146 $deltastring = 'P';
147
148 $sub = false;
149
150 if ($duration_value > 0) {
151 $deltastring .= abs($duration_value);
152 $sub = false;
153 }
154 if ($duration_value < 0) {
155 $deltastring .= abs($duration_value);
156 $sub = true;
157 }
158 if ($duration_unit == 'd') {
159 $deltastring .= "D";
160 }
161 if ($duration_unit == 'm') {
162 $deltastring .= "M";
163 }
164 if ($duration_unit == 'y') {
165 $deltastring .= "Y";
166 }
167
168 $date = new DateTime();
169 if ($tz == 'gmt') {
170 $date->setTimezone(new DateTimeZone('UTC'));
171 } elseif ($tz !== '') {
172 $date->setTimezone(new DateTimeZone($tz == 'tzserver' ? date_default_timezone_get() : $tz));
173 } elseif (!function_exists('getDolGlobalString') || !getDolGlobalString('MAIN_DATE_IN_MEMORY_ARE_NOT_GMT')) { // Add function_exists to allow usage of this function with minimal context
174 $date->setTimezone(new DateTimeZone('UTC'));
175 }
176
177
178 $date->setTimestamp((int) $time);
179 $interval = new DateInterval($deltastring);
180
181 if ($sub) {
182 $date->sub($interval);
183 } else {
184 $date->add($interval);
185 }
186
187 // Change the behavior of PHP over data-interval when the result of this function is Feb 29 (non-leap years), 30 or Feb 31 (so php returns March 1, 2 or 3 respectively)
188 if ($ruleforendofmonth == 1 && $duration_unit == 'm' && $tz !== '') {
189 // Months are compared in the same timezone than the one used to add the delay
190 $origin = new DateTime('@'.((int) $time));
191 $origin->setTimezone($date->getTimezone());
192 $monthsexpected = ((int) $origin->format('Y') * 12) + (int) $origin->format('n') + (int) $duration_value;
193 if (((int) $date->format('Y') * 12) + (int) $date->format('n') != $monthsexpected) {
194 $date->modify('last day of previous month'); // Keep the time of the day
195 }
196 } elseif ($ruleforendofmonth == 1 && $duration_unit == 'm') {
197 $timeyear = (int) dol_print_date($time, '%Y');
198 $timemonth = (int) dol_print_date($time, '%m');
199 $timetotalmonths = (($timeyear * 12) + $timemonth);
200
201 $monthsexpected = ($timetotalmonths + $duration_value);
202
203 $newtime = $date->getTimestamp();
204
205 $newtimeyear = (int) dol_print_date($newtime, '%Y');
206 $newtimemonth = (int) dol_print_date($newtime, '%m');
207 $newtimetotalmonths = (($newtimeyear * 12) + $newtimemonth);
208
209 if ($monthsexpected < $newtimetotalmonths || $monthsexpected > $newtimetotalmonths) { // If months differs
210 $newtimehours = (int) dol_print_date($newtime, '%H');
211 $newtimemins = (int) dol_print_date($newtime, '%M');
212 $newtimesecs = (int) dol_print_date($newtime, '%S');
213
214 $datelim = dol_mktime($newtimehours, $newtimemins, $newtimesecs, $newtimemonth, 1, $newtimeyear); // Take first day
215 $datelim -= (3600 * 24); // Remove 1 day to get the last day of month
216
217 $date->setTimestamp($datelim); // Set new date
218 }
219 }
220 return $date->getTimestamp();
221}
222
223
233function convertTime2Seconds($iHours = 0, $iMinutes = 0, $iSeconds = 0)
234{
235 $iResult = ((int) $iHours * 3600) + ((int) $iMinutes * 60) + (int) $iSeconds;
236 return $iResult;
237}
238
239
262function convertSecondToTime($iSecond, $format = 'all', $lengthOfDay = 86400, $lengthOfWeek = 7)
263{
264 global $langs;
265
266 if (empty($lengthOfDay)) {
267 $lengthOfDay = 86400; // 1 day = 24 hours
268 }
269 if (empty($lengthOfWeek)) {
270 $lengthOfWeek = 7; // 1 week = 7 days
271 }
272 $nbHbyDay = $lengthOfDay / 3600;
273
274 $sTime = '';
275
276 if ($format == 'all' || $format == 'allwithouthour' || $format == 'allhour' || $format == 'allhourmin' || $format == 'allhourminsec') {
277 if ((int) $iSecond === 0) {
278 return '0'; // This is to avoid having 0 return a 12:00 AM for en_US
279 }
280
281 $sDay = 0;
282 $sWeek = 0;
283
284 if ($iSecond >= $lengthOfDay) {
285 for ($i = $iSecond; $i >= $lengthOfDay; $i -= $lengthOfDay) {
286 $sDay++;
287 $iSecond -= $lengthOfDay;
288 }
289 $dayTranslate = $langs->trans("Day");
290 if ($iSecond >= ($lengthOfDay * 2)) {
291 $dayTranslate = $langs->trans("Days");
292 }
293 }
294
295 if ($lengthOfWeek < 7) {
296 if ($sDay) {
297 if ($sDay >= $lengthOfWeek) {
298 $sWeek = (int) (($sDay - $sDay % $lengthOfWeek) / $lengthOfWeek);
299 $sDay %= $lengthOfWeek;
300 $weekTranslate = $langs->trans("DurationWeek");
301 if ($sWeek >= 2) {
302 $weekTranslate = $langs->trans("DurationWeeks");
303 }
304 $sTime .= $sWeek.' '.$weekTranslate.' ';
305 }
306 }
307 }
308 if ($sDay > 0) {
309 $dayTranslate = $langs->trans("Day");
310 if ($sDay > 1) {
311 $dayTranslate = $langs->trans("Days");
312 }
313 $sTime .= $sDay.' '.$langs->trans("d").' ';
314 }
315
316 if ($format == 'all') {
317 if ($iSecond || empty($sDay)) {
318 $sTime .= dol_print_date($iSecond, 'hourduration', true);
319 }
320 } elseif ($format == 'allhourminsec') {
321 return sprintf("%02d", ($sWeek * $lengthOfWeek * $nbHbyDay + $sDay * $nbHbyDay + (int) floor($iSecond / 3600))).':'.sprintf("%02d", ((int) floor(($iSecond % 3600) / 60))).':'.sprintf("%02d", ((int) ($iSecond % 60)));
322 } elseif ($format == 'allhourmin') {
323 return sprintf("%02d", ($sWeek * $lengthOfWeek * $nbHbyDay + $sDay * $nbHbyDay + (int) floor($iSecond / 3600))).':'.sprintf("%02d", ((int) floor(($iSecond % 3600) / 60)));
324 } elseif ($format == 'allhour') {
325 return sprintf("%02d", ($sWeek * $lengthOfWeek * $nbHbyDay + $sDay * $nbHbyDay + (int) floor($iSecond / 3600)));
326 }
327 } elseif ($format == 'hour') { // only hour part
328 $sTime = dol_print_date($iSecond, '%H', true);
329 } elseif ($format == 'fullhour') {
330 if (!empty($iSecond)) {
331 $iSecond /= 3600;
332 } else {
333 $iSecond = 0;
334 }
335 $sTime = $iSecond;
336 } elseif ($format == 'min') { // only min part
337 $sTime = dol_print_date($iSecond, '%M', true);
338 } elseif ($format == 'sec') { // only sec part
339 $sTime = dol_print_date($iSecond, '%S', true);
340 } elseif ($format == 'month') { // only month part
341 $sTime = dol_print_date($iSecond, '%m', true);
342 } elseif ($format == 'year') { // only year part
343 $sTime = dol_print_date($iSecond, '%Y', true);
344 }
345 return trim((string) $sTime);
346}
347
348
356function convertDurationtoHour($duration_value, $duration_unit)
357{
358 $result = 0;
359
360 if ($duration_unit == 's') {
361 $result = $duration_value / 3600;
362 }
363 if ($duration_unit == 'i' || $duration_unit == 'mn' || $duration_unit == 'min') {
364 $result = $duration_value / 60;
365 }
366 if ($duration_unit == 'h') {
367 $result = $duration_value;
368 }
369 if ($duration_unit == 'd') {
370 $result = $duration_value * 24;
371 }
372 if ($duration_unit == 'w') {
373 $result = $duration_value * 24 * 7;
374 }
375 if ($duration_unit == 'm') {
376 $result = $duration_value * 730.484;
377 }
378 if ($duration_unit == 'y') {
379 $result = $duration_value * 365 * 24;
380 }
381
382 return $result;
383}
384
400function dolSqlDateFilter($datefield, $day_date, $month_date, $year_date, $excludefirstand = 0, $gm = false)
401{
402 global $db;
403 $sqldate = '';
404
405 $day_date = intval($day_date);
406 $month_date = intval($month_date);
407 $year_date = intval($year_date);
408
409 if ($month_date > 0) {
410 if ($month_date > 12) { // protection for bad value of month
411 return " AND 1 = 2";
412 }
413 if ($year_date > 0 && empty($day_date)) {
414 $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, $month_date, $gm));
415 $sqldate .= "' AND '".$db->idate(dol_get_last_day($year_date, $month_date, $gm))."'";
416 } elseif ($year_date > 0 && !empty($day_date)) {
417 $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month_date, $day_date, $year_date, $gm));
418 $sqldate .= "' AND '".$db->idate(dol_mktime(23, 59, 59, $month_date, $day_date, $year_date, $gm))."'";
419 } else {
420 // This case is not reliable on TZ, but we should not need it.
421 $sqldate .= ($excludefirstand ? "" : " AND ")." date_format( ".$datefield.", '%c') = '".$db->escape((string) $month_date)."'";
422 }
423 } elseif ($year_date > 0) {
424 $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, 1, $gm));
425 $sqldate .= "' AND '".$db->idate(dol_get_last_day($year_date, 12, $gm))."'";
426 }
427 return $sqldate;
428}
429
449function dol_stringtotime($string, $gm = 1)
450{
451 $reg = array();
452 // Convert date with format DD/MM/YYY HH:MM:SS. This part of code should not be used.
453 if (preg_match('/^([0-9]+)\/([0-9]+)\/([0-9]+)\s?([0-9]+)?:?([0-9]+)?:?([0-9]+)?/i', $string, $reg)) {
454 dol_syslog("dol_stringtotime call to function with deprecated parameter format", LOG_WARNING);
455 // Date est au format 'DD/MM/YY' ou 'DD/MM/YY HH:MM:SS'
456 // Date est au format 'DD/MM/YYYY' ou 'DD/MM/YYYY HH:MM:SS'
457 $sday = (int) $reg[1];
458 $smonth = (int) $reg[2];
459 $syear = (int) $reg[3];
460 $shour = (int) $reg[4];
461 $smin = (int) $reg[5];
462 $ssec = (int) $reg[6];
463 if ($syear < 50) {
464 $syear += 1900;
465 }
466 if ($syear >= 50 && $syear < 100) {
467 $syear += 2000;
468 }
469 $string = sprintf("%04d%02d%02d%02d%02d%02d", $syear, $smonth, $sday, $shour, $smin, $ssec);
470 } 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)
471 || 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
472 || 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
473 ) {
474 $syear = (int) $reg[1];
475 $smonth = (int) $reg[2];
476 $sday = (int) $reg[3];
477 $shour = (int) $reg[4];
478 $smin = (int) $reg[5];
479 $ssec = (int) $reg[6];
480 $string = sprintf("%04d%02d%02d%02d%02d%02d", $syear, $smonth, $sday, $shour, $smin, $ssec);
481 }
482
483 $string = preg_replace('/([^0-9])/i', '', $string);
484 $tmp = $string.'000000';
485 // Clean $gm
486 if ($gm === 1) {
487 $gm = 'gmt';
488 } elseif (empty($gm) || $gm === 'tzserver') {
489 $gm = 'tzserver';
490 }
491
492 $date = dol_mktime((int) substr($tmp, 8, 2), (int) substr($tmp, 10, 2), (int) substr($tmp, 12, 2), (int) substr($tmp, 4, 2), (int) substr($tmp, 6, 2), (int) substr($tmp, 0, 4), $gm);
493
494 return $date;
495}
496
497
506function dol_get_prev_day($day, $month, $year)
507{
508 $time = dol_mktime(12, 0, 0, $month, $day, $year, 1, 0);
509 $time -= 24 * 60 * 60;
510 $tmparray = dol_getdate($time, true);
511 return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
512}
513
522function dol_get_next_day($day, $month, $year)
523{
524 $time = dol_mktime(12, 0, 0, $month, $day, $year, 1, 0);
525 $time += 24 * 60 * 60;
526 $tmparray = dol_getdate($time, true);
527 return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
528}
529
537function dol_get_prev_month($month, $year)
538{
539 if ($month == 1) {
540 $prev_month = 12;
541 $prev_year = $year - 1;
542 } else {
543 $prev_month = $month - 1;
544 $prev_year = $year;
545 }
546 return array('year' => $prev_year, 'month' => $prev_month);
547}
548
556function dol_get_next_month($month, $year)
557{
558 if ($month == 12) {
559 $next_month = 1;
560 $next_year = $year + 1;
561 } else {
562 $next_month = $month + 1;
563 $next_year = $year;
564 }
565 return array('year' => $next_year, 'month' => $next_month);
566}
567
577function dol_get_prev_week($day, $week, $month, $year)
578{
579 $tmparray = dol_get_first_day_week($day, $month, $year);
580
581 $time = dol_mktime(12, 0, 0, $month, $tmparray['first_day'], $year, 1, 0);
582 $time -= 24 * 60 * 60 * 7;
583 $tmparray = dol_getdate($time, true);
584 return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
585}
586
596function dol_get_next_week($day, $week, $month, $year)
597{
598 $tmparray = dol_get_first_day_week($day, $month, $year);
599
600 $time = dol_mktime(12, 0, 0, $tmparray['first_month'], $tmparray['first_day'], $tmparray['first_year'], 1, 0);
601 $time += 24 * 60 * 60 * 7;
602 $tmparray = dol_getdate($time, true);
603
604 return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
605}
606
618function dol_get_first_day($year, $month = 1, $gm = false)
619{
620 if ($year > 9999) {
621 return '';
622 }
623 return dol_mktime(0, 0, 0, $month, 1, $year, $gm);
624}
625
626
637function dol_get_last_day($year, $month = 12, $gm = false)
638{
639 if ($year > 9999) {
640 return '';
641 }
642 if ($month == 12) {
643 $month = 1;
644 $year += 1;
645 } else {
646 $month += 1;
647 }
648
649 // On se deplace au debut du mois suivant, et on retire un jour
650 $datelim = dol_mktime(23, 59, 59, $month, 1, $year, $gm);
651 $datelim -= (3600 * 24);
652
653 return $datelim;
654}
655
664function dol_get_last_hour($date, $gm = 'tzserver')
665{
666 $tmparray = dol_getdate($date, false, ($gm == 'gmt' ? 'gmt' : ''));
667 return dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year'], $gm);
668}
669
678function dol_get_first_hour($date, $gm = 'tzserver')
679{
680 $tmparray = dol_getdate($date, false, ($gm == 'gmt' ? 'gmt' : ''));
681 return dol_mktime(0, 0, 0, $tmparray['mon'], $tmparray['mday'], $tmparray['year'], $gm);
682}
683
693function dol_get_first_day_week($day, $month, $year, $gm = false)
694{
695 //$day=2; $month=2; $year=2015;
696 $date = dol_mktime(0, 0, 0, $month, $day, $year, $gm);
697
698 //Checking conf of start week
699 $start_week = getDolGlobalInt('MAIN_START_WEEK', 1);
700
701 $tmparray = dol_getdate($date, true); // detail of current day
702
703 //Calculate days = offset from current day
704 $days = $start_week - $tmparray['wday'];
705 if ($days >= 1) {
706 $days = 7 - $days;
707 }
708 $days = abs($days);
709 $seconds = $days * 24 * 60 * 60;
710 //print 'start_week='.$start_week.' tmparray[wday]='.$tmparray['wday'].' day offset='.$days.' seconds offset='.$seconds.'<br>';
711
712 //Get first day of week
713 $tmpdaytms = (int) date((string) $tmparray['0']) - $seconds; // $tmparray[0] is day of parameters
714 $tmpday = idate("d", $tmpdaytms);
715
716 //Check first day of week is in same month than current day or not
717 if ($tmpday > $day) {
718 $prev_month = $month - 1;
719 $prev_year = $year;
720
721 if ($prev_month == 0) {
722 $prev_month = 12;
723 $prev_year = $year - 1;
724 }
725 } else {
726 $prev_month = $month;
727 $prev_year = $year;
728 }
729 $tmpmonth = $prev_month;
730 $tmpyear = $prev_year;
731
732 //Get first day of next week
733 $tmptime = dol_mktime(12, 0, 0, $month, $tmpday, $year, 1, 0);
734 $tmptime -= 24 * 60 * 60 * 7;
735 $tmparray = dol_getdate($tmptime, true);
736 $prev_day = $tmparray['mday'];
737
738 //Check prev day of week is in same month than first day or not
739 if ($prev_day > $tmpday) {
740 $prev_month = $month - 1;
741 $prev_year = $year;
742
743 if ($prev_month == 0) {
744 $prev_month = 12;
745 $prev_year = $year - 1;
746 }
747 }
748
749 $week = date("W", dol_mktime(0, 0, 0, $tmpmonth, $tmpday, $tmpyear, $gm));
750
751 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);
752}
753
761function getGMTEasterDatetime($year)
762{
763 $base = new DateTime("$year-03-21", new DateTimeZone("UTC"));
764 $days = easter_days($year); // Return number of days between 21 march and easter day.
765 $tmp = $base->add(new DateInterval("P{$days}D"));
766 return $tmp->getTimestamp();
767}
768
785function num_public_holiday($timestampStart, $timestampEnd, $countryCodeOrId = '', $lastday = 0, $includesaturday = -1, $includesunday = -1, $includefriday = -1, $includemonday = -1)
786{
787 global $conf, $db, $mysoc;
788
789 $nbFerie = 0;
790
791 // Check to ensure we use correct parameters
792 if (($timestampEnd - $timestampStart) % 86400 != 0) {
793 return 'Error Dates must use same hours and must be GMT dates';
794 }
795
796 if (empty($countryCodeOrId)) {
797 $countryCodeOrId = $mysoc->country_code;
798 }
799 if ($includemonday < 0) {
800 $includemonday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_MONDAY', 0);
801 }
802 if ($includefriday < 0) {
803 $includefriday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_FRIDAY', 0);
804 }
805 if ($includesaturday < 0) {
806 $includesaturday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY', 1);
807 }
808 if ($includesunday < 0) {
809 $includesunday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY', 1);
810 }
811
812 if (is_numeric($countryCodeOrId)) {
813 $country_id = $countryCodeOrId;
814 } else {
815 $country_id = dol_getIdFromCode($db, $countryCodeOrId, 'c_country', 'code', 'rowid');
816 }
817
818 if (empty($conf->cache['arrayOfActivePublicHolidays_'.$country_id])) {
819 // Loop on public holiday defined into hrm_public_holiday for the day, month and year analyzed
820 $tmpArrayOfPublicHolidays = array();
821 $sql = "SELECT id, code, entity, fk_country, dayrule, year, month, day, active";
822 $sql .= " FROM ".MAIN_DB_PREFIX."c_hrm_public_holiday";
823 $sql .= " WHERE active = 1 and fk_country IN (0".($country_id > 0 ? ", ".$country_id : 0).")";
824 $sql .= " AND entity IN (0," .getEntity('holiday') .")";
825
826 $resql = $db->query($sql);
827 if ($resql) {
828 $num_rows = $db->num_rows($resql);
829 $i = 0;
830 while ($i < $num_rows) {
831 $obj = $db->fetch_object($resql);
832 $tmpArrayOfPublicHolidays[$obj->id] = array('dayrule' => $obj->dayrule, 'year' => $obj->year, 'month' => $obj->month, 'day' => $obj->day);
833 $i++;
834 }
835 } else {
836 dol_syslog($db->lasterror(), LOG_ERR);
837 return 'Error sql '.$db->lasterror();
838 }
839
840 //var_dump($tmpArrayOfPublicHolidays);
841 $conf->cache['arrayOfActivePublicHolidays_'.$country_id] = $tmpArrayOfPublicHolidays;
842 }
843
844 $arrayOfPublicHolidays = $conf->cache['arrayOfActivePublicHolidays_'.$country_id];
845
846 $i = 0;
847 while ((($lastday == 0 && $timestampStart < $timestampEnd) || ($lastday && $timestampStart <= $timestampEnd))
848 && ($i < 50000)) { // Loop end when equals (Test on i is a security loop to avoid infinite loop)
849 $ferie = false;
850 $specialdayrule = array();
851
852 $jour = (int) gmdate("d", $timestampStart);
853 $mois = (int) gmdate("m", $timestampStart);
854 $annee = (int) gmdate("Y", $timestampStart);
855 $jour_semaine = (int) gmdate("w", $timestampStart); // sunday = 0, monday = 1, ...
856
857 //print "jour=".$jour." month=".$mois." year=".$annee." includesaturday=".$includesaturday." includesunday=".$includesunday."\n";
858 foreach ($arrayOfPublicHolidays as $entrypublicholiday) {
859 if (!empty($entrypublicholiday['dayrule']) && $entrypublicholiday['dayrule'] != 'date') { // For example 'easter', '...'
860 $specialdayrule[$entrypublicholiday['dayrule']] = $entrypublicholiday['dayrule'];
861 } else {
862 $match = 1;
863 if (!empty($entrypublicholiday['year']) && $entrypublicholiday['year'] != $annee) {
864 $match = 0;
865 }
866 if ($entrypublicholiday['month'] != $mois) {
867 $match = 0;
868 }
869 if ($entrypublicholiday['day'] != $jour) {
870 $match = 0;
871 }
872
873 if ($match) {
874 $ferie = true;
875 }
876 }
877
878 $i++;
879 }
880 //var_dump($specialdayrule)."\n";
881 //print "ferie=".$ferie."\n";
882
883 if (!$ferie) {
884 // Special dayrules
885 if (in_array('easter', $specialdayrule)) {
886 // Calculation for easter date
887 $date_paques = getGMTEasterDatetime($annee);
888 $jour_paques = gmdate("d", $date_paques);
889 $mois_paques = gmdate("m", $date_paques);
890 if ($jour_paques == $jour && $mois_paques == $mois) {
891 $ferie = true;
892 }
893 // Easter (sunday)
894 }
895
896 if (in_array('eastermonday', $specialdayrule)) {
897 // Calculation for the monday of easter date
898 $date_paques = getGMTEasterDatetime($annee);
899 //print 'PPP'.$date_paques.' '.dol_print_date($date_paques, 'dayhour', 'gmt')." ";
900 $date_lundi_paques = $date_paques + (3600 * 24);
901 $jour_lundi_paques = gmdate("d", $date_lundi_paques);
902 $mois_lundi_paques = gmdate("m", $date_lundi_paques);
903 if ($jour_lundi_paques == $jour && $mois_lundi_paques == $mois) {
904 $ferie = true;
905 }
906 // Easter (monday)
907 //print 'annee='.$annee.' $jour='.$jour.' $mois='.$mois.' $jour_lundi_paques='.$jour_lundi_paques.' $mois_lundi_paques='.$mois_lundi_paques."\n";
908 }
909
910 //Good Friday
911 if (in_array('goodfriday', $specialdayrule)) {
912 // Pulls the date of Easter
913 $easter = getGMTEasterDatetime($annee);
914
915 // Calculates the date of Good Friday based on Easter
916 $date_good_friday = $easter - (2 * 3600 * 24);
917 $dom_good_friday = gmdate("d", $date_good_friday);
918 $month_good_friday = gmdate("m", $date_good_friday);
919
920 if ($dom_good_friday == $jour && $month_good_friday == $mois) {
921 $ferie = true;
922 }
923 }
924
925 if (in_array('ascension', $specialdayrule)) {
926 // Calcul du jour de l'ascension (39 days after easter day)
927 $date_paques = getGMTEasterDatetime($annee);
928 $date_ascension = $date_paques + (3600 * 24 * 39);
929 $jour_ascension = gmdate("d", $date_ascension);
930 $mois_ascension = gmdate("m", $date_ascension);
931 if ($jour_ascension == $jour && $mois_ascension == $mois) {
932 $ferie = true;
933 }
934 // Ascension (thursday)
935 }
936
937 if (in_array('pentecost', $specialdayrule)) {
938 // Calculation of "Pentecote" (49 days after easter day)
939 $date_paques = getGMTEasterDatetime($annee);
940 $date_pentecote = $date_paques + (3600 * 24 * 49);
941 $jour_pentecote = gmdate("d", $date_pentecote);
942 $mois_pentecote = gmdate("m", $date_pentecote);
943 if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
944 $ferie = true;
945 }
946 // "Pentecote" (sunday)
947 }
948 if (in_array('pentecotemonday', $specialdayrule)) {
949 // Calculation of "Pentecote" (49 days after easter day)
950 $date_paques = getGMTEasterDatetime($annee);
951 $date_pentecote = $date_paques + (3600 * 24 * 50);
952 $jour_pentecote = gmdate("d", $date_pentecote);
953 $mois_pentecote = gmdate("m", $date_pentecote);
954 if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
955 $ferie = true;
956 }
957 // "Pentecote" (monday)
958 }
959
960 if (in_array('viernessanto', $specialdayrule)) {
961 // Viernes Santo
962 $date_paques = getGMTEasterDatetime($annee);
963 $date_viernes = $date_paques - (3600 * 24 * 2);
964 $jour_viernes = gmdate("d", $date_viernes);
965 $mois_viernes = gmdate("m", $date_viernes);
966 if ($jour_viernes == $jour && $mois_viernes == $mois) {
967 $ferie = true;
968 }
969 //Viernes Santo
970 }
971
972 if (in_array('fronleichnam', $specialdayrule)) {
973 // Fronleichnam (60 days after easter sunday)
974 $date_paques = getGMTEasterDatetime($annee);
975 $date_fronleichnam = $date_paques + (3600 * 24 * 60);
976 $jour_fronleichnam = gmdate("d", $date_fronleichnam);
977 $mois_fronleichnam = gmdate("m", $date_fronleichnam);
978 if ($jour_fronleichnam == $jour && $mois_fronleichnam == $mois) {
979 $ferie = true;
980 }
981 // Fronleichnam
982 }
983
984 if (in_array('genevafast', $specialdayrule)) {
985 // Geneva fast in Switzerland (Thursday after the first sunday in September)
986 $date_1sunsept = strtotime('next thursday', strtotime('next sunday', mktime(0, 0, 0, 9, 1, $annee)));
987 $jour_1sunsept = date("d", $date_1sunsept);
988 $mois_1sunsept = date("m", $date_1sunsept);
989 if ($jour_1sunsept == $jour && $mois_1sunsept == $mois) {
990 $ferie = true;
991 }
992 // Geneva fast in Switzerland
993 }
994 }
995 //print "ferie afterspe=".$ferie."\n";
996
997 // If we have to include Friday, Saturday and Sunday
998 if (!$ferie) {
999 if ($includefriday || $includesaturday || $includesunday || $includemonday) {
1000 //Monday (1), Friday (5), Saturday (6) and Sunday (0)
1001 if ($includefriday && $jour_semaine == 5) {
1002 $ferie = true;
1003 }
1004 if ($includesaturday && $jour_semaine == 6) {
1005 $ferie = true;
1006 }
1007 if ($includesunday && $jour_semaine == 0) {
1008 $ferie = true;
1009 }
1010 if ($includemonday && $jour_semaine == 1) {
1011 $ferie = true;
1012 }
1013 }
1014 }
1015 //print "ferie afterincludexxxday=".$ferie."\n";
1016
1017 // We increase the counter of non working day
1018 if ($ferie) {
1019 $nbFerie++;
1020 }
1021
1022 // Increase number of days (on go up into loop)
1023 // Advance by exactly one GMT day. We can use += instead of dol_time_plus_duree() here because
1024 // inputs of this function are GMT dates (checked above), so a day
1025 // is always exactly 86400 seconds.
1026 //var_dump("before ".$jour.' '.$mois.' '.$annee.' '.$timestampStart);
1027 $timestampStart += 86400;
1028 //var_dump("after ".$jour.' '.$mois.' '.$annee.' '.$timestampStart);
1029
1030 $i++;
1031 }
1032
1033 //print "nbFerie=".$nbFerie."\n";
1034 return $nbFerie;
1035}
1036
1053function listPublicHoliday($timestampStart, $timestampEnd, $countryCodeOrId = '', $lastday = 0, $excludesaturday = -1, $excludesunday = -1, $excludefriday = -1, $excludemonday = -1)
1054{
1055 global $conf, $db, $mysoc;
1056
1057 // Check to ensure we use correct parameters
1058 if (($timestampEnd - $timestampStart) % 86400 != 0) {
1059 return 'Error Dates must use same hours and must be GMT dates';
1060 }
1061
1062 if (empty($countryCodeOrId)) {
1063 $countryCodeOrId = $mysoc->country_code;
1064 }
1065 if ($excludemonday < 0) {
1066 $excludemonday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_MONDAY', 0);
1067 }
1068 if ($excludefriday < 0) {
1069 $excludefriday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_FRIDAY', 0);
1070 }
1071 if ($excludesaturday < 0) {
1072 $excludesaturday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY', 1);
1073 }
1074 if ($excludesunday < 0) {
1075 $excludesunday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY', 1);
1076 }
1077
1078 if (is_numeric($countryCodeOrId)) {
1079 $country_id = $countryCodeOrId;
1080 } else {
1081 $country_id = dol_getIdFromCode($db, $countryCodeOrId, 'c_country', 'code', 'rowid');
1082 }
1083
1084 if (empty($conf->cache['arrayOfActivePublicHolidays_' . $country_id])) {
1085 // Loop on public holiday defined into hrm_public_holiday for the day, month and year analyzed
1086 $tmpArrayOfPublicHolidays = array();
1087 $sql = "SELECT id, code, entity, fk_country, dayrule, year, month, day, active";
1088 $sql .= " FROM " . MAIN_DB_PREFIX . "c_hrm_public_holiday";
1089 $sql .= " WHERE active = 1 and fk_country IN (0" . ($country_id > 0 ? ", " . $country_id : 0) . ")";
1090 $sql .= " AND entity IN (0," . getEntity('holiday') . ")";
1091
1092 $resql = $db->query($sql);
1093 if ($resql) {
1094 $num_rows = $db->num_rows($resql);
1095 $i = 0;
1096 while ($i < $num_rows) {
1097 $obj = $db->fetch_object($resql);
1098 $tmpArrayOfPublicHolidays[$obj->id] = array('dayrule' => $obj->dayrule, 'year' => $obj->year, 'month' => $obj->month, 'day' => $obj->day);
1099 $i++;
1100 }
1101 } else {
1102 dol_syslog($db->lasterror(), LOG_ERR);
1103 return 'Error sql ' . $db->lasterror();
1104 }
1105
1106 //var_dump($tmpArrayOfPublicHolidays);
1107 $conf->cache['arrayOfActivePublicHolidays_' . $country_id] = $tmpArrayOfPublicHolidays;
1108 }
1109
1110 $arrayOfPublicHolidays = $conf->cache['arrayOfActivePublicHolidays_' . $country_id];
1111 $listFeries = [];
1112 $i = 0;
1113 while ((($lastday == 0 && $timestampStart < $timestampEnd) || ($lastday && $timestampStart <= $timestampEnd))
1114 && ($i < 50000)) { // Loop end when equals (Test on i is a security loop to avoid infinite loop)
1115 $nonWorkingDay = false;
1116 $ferie = false;
1117 $specialdayrule = array();
1118
1119 $jour = (int) gmdate("d", $timestampStart);
1120 $mois = (int) gmdate("m", $timestampStart);
1121 $annee = (int) gmdate("Y", $timestampStart);
1122
1123 // If we have to exclude Friday, Saturday and Sunday
1124 if ($excludefriday || $excludesaturday || $excludesunday) {
1125 $jour_julien = unixtojd($timestampStart);
1126 $jour_semaine = jddayofweek($jour_julien, 0);
1127 if ($excludefriday) { //Friday (5), Saturday (6) and Sunday (0)
1128 if ($jour_semaine == 5) {
1129 $nonWorkingDay = true;
1130 }
1131 }
1132 if ($excludesaturday) { //Friday (5), Saturday (6) and Sunday (0)
1133 if ($jour_semaine == 6) {
1134 $nonWorkingDay = true;
1135 }
1136 }
1137 if ($excludesunday) { //Friday (5), Saturday (6) and Sunday (0)
1138 if ($jour_semaine == 0) {
1139 $nonWorkingDay = true;
1140 }
1141 }
1142 }
1143 //print "ferie=".$nonWorkingDay."\n";
1144
1145 if (!$nonWorkingDay) {
1146 //print "jour=".$jour." month=".$mois." year=".$annee." includesaturday=".$excludesaturday." includesunday=".$excludesunday."\n";
1147 foreach ($arrayOfPublicHolidays as $entrypublicholiday) {
1148 if (!empty($entrypublicholiday['dayrule']) && $entrypublicholiday['dayrule'] != 'date') { // For example 'easter', '...'
1149 $specialdayrule[$entrypublicholiday['dayrule']] = $entrypublicholiday['dayrule'];
1150 } else {
1151 $match = 1;
1152 if (!empty($entrypublicholiday['year']) && $entrypublicholiday['year'] != $annee) {
1153 $match = 0;
1154 }
1155 if ($entrypublicholiday['month'] != $mois) {
1156 $match = 0;
1157 }
1158 if ($entrypublicholiday['day'] != $jour) {
1159 $match = 0;
1160 }
1161
1162 if ($match) {
1163 $ferie = true;
1164 $listFeries[] = $timestampStart;
1165 }
1166 }
1167
1168 $i++;
1169 }
1170 //var_dump($specialdayrule)."\n";
1171 //print "ferie=".$nonWorkingDay."\n";
1172 }
1173
1174 if (!$nonWorkingDay && !$ferie) {
1175 // Special dayrules
1176 if (in_array('easter', $specialdayrule)) {
1177 // Calculation for easter date
1178 $date_paques = getGMTEasterDatetime($annee);
1179 $jour_paques = gmdate("d", $date_paques);
1180 $mois_paques = gmdate("m", $date_paques);
1181 if ($jour_paques == $jour && $mois_paques == $mois) {
1182 $ferie = true;
1183 $listFeries[] = $timestampStart;
1184 }
1185 // Easter (sunday)
1186 }
1187
1188 if (in_array('eastermonday', $specialdayrule)) {
1189 // Calculation for the monday of easter date
1190 $date_paques = getGMTEasterDatetime($annee);
1191 //print 'PPP'.$date_paques.' '.dol_print_date($date_paques, 'dayhour', 'gmt')." ";
1192 $date_lundi_paques = $date_paques + (3600 * 24);
1193 $jour_lundi_paques = gmdate("d", $date_lundi_paques);
1194 $mois_lundi_paques = gmdate("m", $date_lundi_paques);
1195 if ($jour_lundi_paques == $jour && $mois_lundi_paques == $mois) {
1196 $ferie = true;
1197 $listFeries[] = $timestampStart;
1198 }
1199 // Easter (monday)
1200 //print 'annee='.$annee.' $jour='.$jour.' $mois='.$mois.' $jour_lundi_paques='.$jour_lundi_paques.' $mois_lundi_paques='.$mois_lundi_paques."\n";
1201 }
1202
1203 //Good Friday
1204 if (in_array('goodfriday', $specialdayrule)) {
1205 // Pulls the date of Easter
1206 $easter = getGMTEasterDatetime($annee);
1207
1208 // Calculates the date of Good Friday based on Easter
1209 $date_good_friday = $easter - (2 * 3600 * 24);
1210 $dom_good_friday = gmdate("d", $date_good_friday);
1211 $month_good_friday = gmdate("m", $date_good_friday);
1212
1213 if ($dom_good_friday == $jour && $month_good_friday == $mois) {
1214 $ferie = true;
1215 $listFeries[] = $timestampStart;
1216 }
1217 }
1218
1219 if (in_array('ascension', $specialdayrule)) {
1220 // Calcul du jour de l'ascension (39 days after easter day)
1221 $date_paques = getGMTEasterDatetime($annee);
1222 $date_ascension = $date_paques + (3600 * 24 * 39);
1223 $jour_ascension = gmdate("d", $date_ascension);
1224 $mois_ascension = gmdate("m", $date_ascension);
1225 if ($jour_ascension == $jour && $mois_ascension == $mois) {
1226 $ferie = true;
1227 $listFeries[] = $timestampStart;
1228 }
1229 // Ascension (thursday)
1230 }
1231
1232 if (in_array('pentecost', $specialdayrule)) {
1233 // Calculation of "Pentecote" (49 days after easter day)
1234 $date_paques = getGMTEasterDatetime($annee);
1235 $date_pentecote = $date_paques + (3600 * 24 * 49);
1236 $jour_pentecote = gmdate("d", $date_pentecote);
1237 $mois_pentecote = gmdate("m", $date_pentecote);
1238 if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
1239 $ferie = true;
1240 $listFeries[] = $timestampStart;
1241 }
1242 // "Pentecote" (sunday)
1243 }
1244
1245 if (in_array('pentecotemonday', $specialdayrule)) {
1246 // Calculation of "Pentecote" (49 days after easter day)
1247 $date_paques = getGMTEasterDatetime($annee);
1248 $date_pentecote = $date_paques + (3600 * 24 * 50);
1249 $jour_pentecote = gmdate("d", $date_pentecote);
1250 $mois_pentecote = gmdate("m", $date_pentecote);
1251 if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
1252 $ferie = true;
1253 $listFeries[] = $timestampStart;
1254 }
1255 // "Pentecote" (monday)
1256 }
1257
1258 if (in_array('viernessanto', $specialdayrule)) {
1259 // Viernes Santo
1260 $date_paques = getGMTEasterDatetime($annee);
1261 $date_viernes = $date_paques - (3600 * 24 * 2);
1262 $jour_viernes = gmdate("d", $date_viernes);
1263 $mois_viernes = gmdate("m", $date_viernes);
1264 if ($jour_viernes == $jour && $mois_viernes == $mois) {
1265 $ferie = true;
1266 $listFeries[] = $timestampStart;
1267 }
1268 //Viernes Santo
1269 }
1270
1271 if (in_array('fronleichnam', $specialdayrule)) {
1272 // Fronleichnam (60 days after easter sunday)
1273 $date_paques = getGMTEasterDatetime($annee);
1274 $date_fronleichnam = $date_paques + (3600 * 24 * 60);
1275 $jour_fronleichnam = gmdate("d", $date_fronleichnam);
1276 $mois_fronleichnam = gmdate("m", $date_fronleichnam);
1277 if ($jour_fronleichnam == $jour && $mois_fronleichnam == $mois) {
1278 $ferie = true;
1279 $listFeries[] = $timestampStart;
1280 }
1281 // Fronleichnam
1282 }
1283
1284 if (in_array('genevafast', $specialdayrule)) {
1285 // Geneva fast in Switzerland (Thursday after the first sunday in September)
1286 $date_1sunsept = strtotime('next thursday', strtotime('next sunday', mktime(0, 0, 0, 9, 1, $annee)));
1287 $jour_1sunsept = date("d", $date_1sunsept);
1288 $mois_1sunsept = date("m", $date_1sunsept);
1289 if ($jour_1sunsept == $jour && $mois_1sunsept == $mois) {
1290 $ferie = true;
1291 $listFeries[] = $timestampStart;
1292 }
1293 // Geneva fast in Switzerland
1294 }
1295 }
1296 //print "ferie=".$nonWorkingDay."\n";
1297
1298 // Increase number of days (on go up into loop)
1299 $timestampStart = dol_time_plus_duree($timestampStart, 1, 'd');
1300 //var_dump($jour.' '.$mois.' '.$annee.' '.$timestampStart);
1301
1302 $i++;
1303 }
1304
1305 //print "nbFerie=".$nbFerie."\n";
1306 return $listFeries;
1307}
1308
1319function num_between_day($timestampStart, $timestampEnd, $lastday = 0)
1320{
1321 if ($timestampStart <= $timestampEnd) {
1322 if ($lastday == 1) {
1323 $bit = 0;
1324 } else {
1325 $bit = 1;
1326 }
1327 $nbjours = (int) floor(($timestampEnd - $timestampStart) / (60 * 60 * 24)) + 1 - $bit;
1328 } else {
1329 $nbjours = 0;
1330 }
1331 //print ($timestampEnd - $timestampStart) - $lastday;
1332 return $nbjours;
1333}
1334
1347function num_open_day($timestampStart, $timestampEnd, $inhour = 0, $lastday = 0, $halfday = 0, $countryCodeOrId = '')
1348{
1349 global $langs, $mysoc;
1350
1351 if (empty($countryCodeOrId) || $countryCodeOrId < 0) {
1352 $countryCodeOrId = $mysoc->country_code;
1353 }
1354
1355 dol_syslog('num_open_day timestampStart='.$timestampStart.' timestampEnd='.$timestampEnd.' bit='.$lastday.' countryCodeOrId='.$countryCodeOrId);
1356
1357 // Check parameters
1358 if (!is_int($timestampStart) && !is_float($timestampStart)) {
1359 return 'ErrorBadParameter_num_open_day';
1360 }
1361 if (!is_int($timestampEnd) && !is_float($timestampEnd)) {
1362 return 'ErrorBadParameter_num_open_day';
1363 }
1364
1365 if ($timestampStart < $timestampEnd) {
1366 // --- 1. Calculate Gross Working Days ---
1367 // Gross working days = total days in range - non-working days (weekends & public holidays).
1368 $a = num_between_day($timestampStart, $timestampEnd, $lastday);
1369 $b = num_public_holiday($timestampStart, $timestampEnd, $countryCodeOrId, $lastday);
1370 $nbOpenDay = $a - $b;
1371
1372 // --- 2. Apply Contextual Half-Day Deductions ---
1373 $halfday = (int) $halfday; // Ensure $halfday is an integer for reliable comparisons.
1374
1375 // Check if start/end days are working days just ONCE to optimize performance
1376 // by avoiding redundant calls to the potentially slow num_public_holiday() function.
1377 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1378 $isStartDayWorking = (num_public_holiday($timestampStart, $timestampStart, $countryCodeOrId, 1) == 0);
1379 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1380 $isEndDayWorking = (num_public_holiday($timestampEnd, $timestampEnd, $countryCodeOrId, 1) == 0);
1381
1382 // Deduct 0.5 if the leave starts in the afternoon of a working day.
1383 if (($halfday == -1 || $halfday == 2) && $isStartDayWorking) {
1384 $nbOpenDay -= 0.5;
1385 }
1386
1387 // Deduct 0.5 if the leave ends in the morning of a different, working day.
1388 if (($halfday == 1 || $halfday == 2) && date('Y-m-d', $timestampStart) != date('Y-m-d', $timestampEnd) && $isEndDayWorking) {
1389 $nbOpenDay -= 0.5;
1390 }
1391
1392 // --- 3. Return Final Value ---
1393 if ($inhour == 1) {
1394 return $nbOpenDay * 24;
1395 }
1396
1397 return $nbOpenDay;
1398 } elseif ($timestampStart == $timestampEnd) {
1399 $numholidays = 0;
1400 if ($lastday) {
1401 $numholidays = num_public_holiday($timestampStart, $timestampEnd, $countryCodeOrId, $lastday);
1402 if ($numholidays == 1) {
1403 return 0;
1404 }
1405 }
1406
1407 $nbOpenDay = $lastday;
1408
1409 if ($inhour == 1) {
1410 $nbOpenDay *= 24;
1411 }
1412 return $nbOpenDay - (($inhour == 1 ? 12 : 0.5) * abs($halfday));
1413 } else {
1414 return $langs->trans("Error");
1415 }
1416}
1417
1418
1419
1428function monthArray($outputlangs, $short = 0)
1429{
1430 $montharray = array(
1431 1 => $outputlangs->trans("Month01"),
1432 2 => $outputlangs->trans("Month02"),
1433 3 => $outputlangs->trans("Month03"),
1434 4 => $outputlangs->trans("Month04"),
1435 5 => $outputlangs->trans("Month05"),
1436 6 => $outputlangs->trans("Month06"),
1437 7 => $outputlangs->trans("Month07"),
1438 8 => $outputlangs->trans("Month08"),
1439 9 => $outputlangs->trans("Month09"),
1440 10 => $outputlangs->trans("Month10"),
1441 11 => $outputlangs->trans("Month11"),
1442 12 => $outputlangs->trans("Month12")
1443 );
1444
1445 if (!empty($short)) {
1446 $montharray = array(
1447 1 => $outputlangs->trans("MonthShort01"),
1448 2 => $outputlangs->trans("MonthShort02"),
1449 3 => $outputlangs->trans("MonthShort03"),
1450 4 => $outputlangs->trans("MonthShort04"),
1451 5 => $outputlangs->trans("MonthShort05"),
1452 6 => $outputlangs->trans("MonthShort06"),
1453 7 => $outputlangs->trans("MonthShort07"),
1454 8 => $outputlangs->trans("MonthShort08"),
1455 9 => $outputlangs->trans("MonthShort09"),
1456 10 => $outputlangs->trans("MonthShort10"),
1457 11 => $outputlangs->trans("MonthShort11"),
1458 12 => $outputlangs->trans("MonthShort12")
1459 );
1460 }
1461
1462 return $montharray;
1463}
1464
1472function getWeekNumbersOfMonth($month, $year)
1473{
1474 $nb_days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
1475 $TWeek = array();
1476 for ($day = 1; $day <= $nb_days; $day++) {
1477 $week_number = getWeekNumber($day, $month, $year);
1478 $TWeek[$week_number] = $week_number;
1479 }
1480 return $TWeek;
1481}
1482
1490function getFirstDayOfEachWeek($TWeek, $year)
1491{
1492 $TFirstDayOfWeek = array();
1493 // When a month overlaps 2 years, the weeks 52/53 of previous year and the week 01 of next year are present together in $TWeek.
1494 // If $TWeek starts with week 52 or 53 (a January), these first weeks belong to previous year.
1495 // If $TWeek ends with week 01 (a December), this last week belongs to next year.
1496 $startwithweeksofpreviousyear = ((int) reset($TWeek) >= 52 && in_array('01', $TWeek));
1497 foreach ($TWeek as $weekNb) {
1498 $yeartouse = $year;
1499 if ($startwithweeksofpreviousyear) {
1500 if ((int) $weekNb >= 52) {
1501 $yeartouse = $year - 1;
1502 }
1503 } elseif ($weekNb == '01' && in_array('52', $TWeek)) {
1504 $yeartouse = $year + 1;
1505 }
1506 $TFirstDayOfWeek[$weekNb] = date('d', strtotime($yeartouse.'W'.$weekNb));
1507 }
1508 return $TFirstDayOfWeek;
1509}
1510
1518function getLastDayOfEachWeek($TWeek, $year)
1519{
1520 $TLastDayOfWeek = array();
1521 // Same rule as in getFirstDayOfEachWeek() to find the year each week belongs to
1522 $startwithweeksofpreviousyear = ((int) reset($TWeek) >= 52 && in_array('01', $TWeek));
1523 foreach ($TWeek as $weekNb) {
1524 $yeartouse = $year;
1525 if ($startwithweeksofpreviousyear) {
1526 if ((int) $weekNb >= 52) {
1527 $yeartouse = $year - 1;
1528 }
1529 } elseif ($weekNb == '01' && in_array('52', $TWeek)) {
1530 $yeartouse = $year + 1;
1531 }
1532 $TLastDayOfWeek[$weekNb] = date('d', strtotime($yeartouse.'W'.$weekNb.'+6 days'));
1533 }
1534 return $TLastDayOfWeek;
1535}
1536
1545function getWeekNumber($day, $month, $year)
1546{
1547 $date = new DateTime($year.'-'.$month.'-'.$day);
1548 $week = $date->format("W");
1549 return $week;
1550}
global $mysoc
dol_get_prev_month($month, $year)
Return previous month.
Definition date.lib.php:537
listPublicHoliday($timestampStart, $timestampEnd, $countryCodeOrId='', $lastday=0, $excludesaturday=-1, $excludesunday=-1, $excludefriday=-1, $excludemonday=-1)
Return the list of public holidays including Friday, Saturday and Sunday (or not) between 2 dates in ...
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:678
dol_get_next_day($day, $month, $year)
Return next day.
Definition date.lib.php:522
dol_get_next_week($day, $week, $month, $year)
Return next week.
Definition date.lib.php:596
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:400
getServerTimeZoneString()
Return server timezone string.
Definition date.lib.php:75
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:664
getLastDayOfEachWeek($TWeek, $year)
Return array of last day of weeks.
getWeekNumbersOfMonth($month, $year)
Return array of week numbers.
getServerTimeZoneInt($refgmtdate='now')
Return server timezone int.
Definition date.lib.php:86
dol_get_first_day_week($day, $month, $year, $gm=false)
Return first day of week for a date.
Definition date.lib.php:693
getGMTEasterDatetime($year)
Return the easter day in GMT time.
Definition date.lib.php:761
convertDurationtoHour($duration_value, $duration_unit)
Convert duration to hour.
Definition date.lib.php:356
get_tz_array()
Return an array with timezone values.
Definition date.lib.php:36
dol_get_prev_day($day, $month, $year)
Return previous day.
Definition date.lib.php:506
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:618
getFirstDayOfEachWeek($TWeek, $year)
Return array of first day of weeks.
dol_get_next_month($month, $year)
Return next month.
Definition date.lib.php:556
getWeekNumber($day, $month, $year)
Return week number.
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...
num_public_holiday($timestampStart, $timestampEnd, $countryCodeOrId='', $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:785
convertTime2Seconds($iHours=0, $iMinutes=0, $iSeconds=0)
Convert hours and minutes into seconds.
Definition date.lib.php:233
num_open_day($timestampStart, $timestampEnd, $inhour=0, $lastday=0, $halfday=0, $countryCodeOrId='')
Function to return number of working days (and text of units) between two dates (working days)
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:262
dol_get_prev_week($day, $week, $month, $year)
Return previous week.
Definition date.lib.php:577
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:449
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:637
monthArray($outputlangs, $short=0)
Return array of translated months or selected month.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0, $tz='')
Add a delay to a date.
Definition date.lib.php:128
dol_now($mode='gmt')
Return date for now.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
if(getDolGlobalString( 'TAKEPOS_SHOW_CUSTOMER')) print $langs trans('Date')." left Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:469