dolibarr 24.0.0-beta
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-2026 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
9 * Copyright (C) 2026 Joachim Küter <git-jk@bloxera.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 * or see https://www.gnu.org/
24 */
25
37function get_tz_array()
38{
39 $tzarray = array(
40 -11 => "Pacific/Pago_Pago",
41 -10 => "Pacific/Honolulu",
42 -9 => "America/Anchorage",
43 -8 => "America/Los_Angeles",
44 -7 => "America/Dawson_Creek",
45 -6 => "America/Chicago",
46 -5 => "America/Bogota",
47 -4 => "America/Asuncion",
48 -3 => "America/Araguaina",
49 -2 => "America/Noronha",
50 -1 => "Atlantic/Azores",
51 0 => "Europe/London",
52 1 => "Europe/Paris",
53 2 => "Europe/Helsinki",
54 3 => "Europe/Moscow",
55 4 => "Asia/Dubai",
56 5 => "Asia/Karachi",
57 6 => "Indian/Chagos",
58 7 => "Asia/Jakarta",
59 8 => "Asia/Hong_Kong",
60 9 => "Asia/Tokyo",
61 10 => "Australia/Sydney",
62 11 => "Pacific/Noumea",
63 12 => "Pacific/Auckland",
64 13 => "Pacific/Fakaofo",
65 14 => "Pacific/Kiritimati"
66 );
67 return $tzarray;
68}
69
70
77{
78 return @date_default_timezone_get();
79}
80
87function getServerTimeZoneInt($refgmtdate = 'now')
88{
89 if (method_exists('DateTimeZone', 'getOffset')) {
90 // Method 1 (include daylight)
91 $gmtnow = dol_now('gmt');
92 $yearref = dol_print_date($gmtnow, '%Y');
93 $monthref = dol_print_date($gmtnow, '%m');
94 $dayref = dol_print_date($gmtnow, '%d');
95 if ($refgmtdate == 'now') {
96 $newrefgmtdate = $yearref.'-'.$monthref.'-'.$dayref;
97 } elseif ($refgmtdate == 'summer') {
98 $newrefgmtdate = $yearref.'-08-01';
99 } else {
100 $newrefgmtdate = $yearref.'-01-01';
101 }
102 $newrefgmtdate .= 'T00:00:00+00:00';
103 $localtz = new DateTimeZone(getServerTimeZoneString());
104 $localdt = new DateTime($newrefgmtdate, $localtz);
105 $tmp = -1 * $localtz->getOffset($localdt);
106 //print $refgmtdate.'='.$tmp;
107 } else {
108 $tmp = 0;
109 dol_print_error(null, 'PHP version must be 5.3+');
110 }
111 $tz = round(($tmp < 0 ? 1 : -1) * abs($tmp / 3600));
112 return $tz;
113}
114
115
126function dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth = 0)
127{
128 if (empty($duration_value)) {
129 return $time;
130 }
131 if ($duration_unit == 's') {
132 return $time + (int) ($duration_value);
133 }
134 if ($duration_unit == 'i' || $duration_unit == 'mn' || $duration_unit == 'min') {
135 return $time + (int) (60 * $duration_value);
136 }
137 if ($duration_unit == 'h') {
138 return $time + (int) (3600 * $duration_value);
139 }
140 if ($duration_unit == 'w') {
141 return $time + (int) (3600 * 24 * 7 * $duration_value);
142 }
143
144 $deltastring = 'P';
145
146 $sub = false;
147
148 if ($duration_value > 0) {
149 $deltastring .= abs($duration_value);
150 $sub = false;
151 }
152 if ($duration_value < 0) {
153 $deltastring .= abs($duration_value);
154 $sub = true;
155 }
156 if ($duration_unit == 'd') {
157 $deltastring .= "D";
158 }
159 if ($duration_unit == 'm') {
160 $deltastring .= "M";
161 }
162 if ($duration_unit == 'y') {
163 $deltastring .= "Y";
164 }
165
166 $date = new DateTime();
167 if (!function_exists('getDolGlobalString') || !getDolGlobalString('MAIN_DATE_IN_MEMORY_ARE_NOT_GMT')) { // Add function_exists to allow usage of this function with minimal context
168 $date->setTimezone(new DateTimeZone('UTC'));
169 }
170
171
172 $date->setTimestamp((int) $time);
173 $interval = new DateInterval($deltastring);
174
175 if ($sub) {
176 $date->sub($interval);
177 } else {
178 $date->add($interval);
179 }
180
181 // 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)
182 if ($ruleforendofmonth == 1 && $duration_unit == 'm') {
183 $timeyear = (int) dol_print_date($time, '%Y');
184 $timemonth = (int) dol_print_date($time, '%m');
185 $timetotalmonths = (($timeyear * 12) + $timemonth);
186
187 $monthsexpected = ($timetotalmonths + $duration_value);
188
189 $newtime = $date->getTimestamp();
190
191 $newtimeyear = (int) dol_print_date($newtime, '%Y');
192 $newtimemonth = (int) dol_print_date($newtime, '%m');
193 $newtimetotalmonths = (($newtimeyear * 12) + $newtimemonth);
194
195 if ($monthsexpected < $newtimetotalmonths || $monthsexpected > $newtimetotalmonths) { // If months differs
196 $newtimehours = (int) dol_print_date($newtime, '%H');
197 $newtimemins = (int) dol_print_date($newtime, '%M');
198 $newtimesecs = (int) dol_print_date($newtime, '%S');
199
200 $datelim = dol_mktime($newtimehours, $newtimemins, $newtimesecs, $newtimemonth, 1, $newtimeyear); // Take first day
201 $datelim -= (3600 * 24); // Remove 1 day to get the last day of month
202
203 $date->setTimestamp($datelim); // Set new date
204 }
205 }
206 return $date->getTimestamp();
207}
208
209
219function convertTime2Seconds($iHours = 0, $iMinutes = 0, $iSeconds = 0)
220{
221 $iResult = ((int) $iHours * 3600) + ((int) $iMinutes * 60) + (int) $iSeconds;
222 return $iResult;
223}
224
225
248function convertSecondToTime($iSecond, $format = 'all', $lengthOfDay = 86400, $lengthOfWeek = 7)
249{
250 global $langs;
251
252 if (empty($lengthOfDay)) {
253 $lengthOfDay = 86400; // 1 day = 24 hours
254 }
255 if (empty($lengthOfWeek)) {
256 $lengthOfWeek = 7; // 1 week = 7 days
257 }
258 $nbHbyDay = $lengthOfDay / 3600;
259
260 $sTime = '';
261
262 if ($format == 'all' || $format == 'allwithouthour' || $format == 'allhour' || $format == 'allhourmin' || $format == 'allhourminsec') {
263 if ((int) $iSecond === 0) {
264 return '0'; // This is to avoid having 0 return a 12:00 AM for en_US
265 }
266
267 $sDay = 0;
268 $sWeek = 0;
269
270 if ($iSecond >= $lengthOfDay) {
271 for ($i = $iSecond; $i >= $lengthOfDay; $i -= $lengthOfDay) {
272 $sDay++;
273 $iSecond -= $lengthOfDay;
274 }
275 $dayTranslate = $langs->trans("Day");
276 if ($iSecond >= ($lengthOfDay * 2)) {
277 $dayTranslate = $langs->trans("Days");
278 }
279 }
280
281 if ($lengthOfWeek < 7) {
282 if ($sDay) {
283 if ($sDay >= $lengthOfWeek) {
284 $sWeek = (int) (($sDay - $sDay % $lengthOfWeek) / $lengthOfWeek);
285 $sDay %= $lengthOfWeek;
286 $weekTranslate = $langs->trans("DurationWeek");
287 if ($sWeek >= 2) {
288 $weekTranslate = $langs->trans("DurationWeeks");
289 }
290 $sTime .= $sWeek.' '.$weekTranslate.' ';
291 }
292 }
293 }
294 if ($sDay > 0) {
295 $dayTranslate = $langs->trans("Day");
296 if ($sDay > 1) {
297 $dayTranslate = $langs->trans("Days");
298 }
299 $sTime .= $sDay.' '.$langs->trans("d").' ';
300 }
301
302 if ($format == 'all') {
303 if ($iSecond || empty($sDay)) {
304 $sTime .= dol_print_date($iSecond, 'hourduration', true);
305 }
306 } elseif ($format == 'allhourminsec') {
307 return sprintf("%02d", ($sWeek * $lengthOfWeek * $nbHbyDay + $sDay * $nbHbyDay + (int) floor($iSecond / 3600))).':'.sprintf("%02d", ((int) floor(($iSecond % 3600) / 60))).':'.sprintf("%02d", ((int) ($iSecond % 60)));
308 } elseif ($format == 'allhourmin') {
309 return sprintf("%02d", ($sWeek * $lengthOfWeek * $nbHbyDay + $sDay * $nbHbyDay + (int) floor($iSecond / 3600))).':'.sprintf("%02d", ((int) floor(($iSecond % 3600) / 60)));
310 } elseif ($format == 'allhour') {
311 return sprintf("%02d", ($sWeek * $lengthOfWeek * $nbHbyDay + $sDay * $nbHbyDay + (int) floor($iSecond / 3600)));
312 }
313 } elseif ($format == 'hour') { // only hour part
314 $sTime = dol_print_date($iSecond, '%H', true);
315 } elseif ($format == 'fullhour') {
316 if (!empty($iSecond)) {
317 $iSecond /= 3600;
318 } else {
319 $iSecond = 0;
320 }
321 $sTime = $iSecond;
322 } elseif ($format == 'min') { // only min part
323 $sTime = dol_print_date($iSecond, '%M', true);
324 } elseif ($format == 'sec') { // only sec part
325 $sTime = dol_print_date($iSecond, '%S', true);
326 } elseif ($format == 'month') { // only month part
327 $sTime = dol_print_date($iSecond, '%m', true);
328 } elseif ($format == 'year') { // only year part
329 $sTime = dol_print_date($iSecond, '%Y', true);
330 }
331 return trim((string) $sTime);
332}
333
334
342function convertDurationtoHour($duration_value, $duration_unit)
343{
344 $result = 0;
345
346 if ($duration_unit == 's') {
347 $result = $duration_value / 3600;
348 }
349 if ($duration_unit == 'i' || $duration_unit == 'mn' || $duration_unit == 'min') {
350 $result = $duration_value / 60;
351 }
352 if ($duration_unit == 'h') {
353 $result = $duration_value;
354 }
355 if ($duration_unit == 'd') {
356 $result = $duration_value * 24;
357 }
358 if ($duration_unit == 'w') {
359 $result = $duration_value * 24 * 7;
360 }
361 if ($duration_unit == 'm') {
362 $result = $duration_value * 730.484;
363 }
364 if ($duration_unit == 'y') {
365 $result = $duration_value * 365 * 24;
366 }
367
368 return $result;
369}
370
386function dolSqlDateFilter($datefield, $day_date, $month_date, $year_date, $excludefirstand = 0, $gm = false)
387{
388 global $db;
389 $sqldate = '';
390
391 $day_date = intval($day_date);
392 $month_date = intval($month_date);
393 $year_date = intval($year_date);
394
395 if ($month_date > 0) {
396 if ($month_date > 12) { // protection for bad value of month
397 return " AND 1 = 2";
398 }
399 if ($year_date > 0 && empty($day_date)) {
400 $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, $month_date, $gm));
401 $sqldate .= "' AND '".$db->idate(dol_get_last_day($year_date, $month_date, $gm))."'";
402 } elseif ($year_date > 0 && !empty($day_date)) {
403 $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month_date, $day_date, $year_date, $gm));
404 $sqldate .= "' AND '".$db->idate(dol_mktime(23, 59, 59, $month_date, $day_date, $year_date, $gm))."'";
405 } else {
406 // This case is not reliable on TZ, but we should not need it.
407 $sqldate .= ($excludefirstand ? "" : " AND ")." date_format( ".$datefield.", '%c') = '".$db->escape((string) $month_date)."'";
408 }
409 } elseif ($year_date > 0) {
410 $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, 1, $gm));
411 $sqldate .= "' AND '".$db->idate(dol_get_last_day($year_date, 12, $gm))."'";
412 }
413 return $sqldate;
414}
415
435function dol_stringtotime($string, $gm = 1)
436{
437 $reg = array();
438 // Convert date with format DD/MM/YYY HH:MM:SS. This part of code should not be used.
439 if (preg_match('/^([0-9]+)\/([0-9]+)\/([0-9]+)\s?([0-9]+)?:?([0-9]+)?:?([0-9]+)?/i', $string, $reg)) {
440 dol_syslog("dol_stringtotime call to function with deprecated parameter format", LOG_WARNING);
441 // Date est au format 'DD/MM/YY' ou 'DD/MM/YY HH:MM:SS'
442 // Date est au format 'DD/MM/YYYY' ou 'DD/MM/YYYY HH:MM:SS'
443 $sday = (int) $reg[1];
444 $smonth = (int) $reg[2];
445 $syear = (int) $reg[3];
446 $shour = (int) $reg[4];
447 $smin = (int) $reg[5];
448 $ssec = (int) $reg[6];
449 if ($syear < 50) {
450 $syear += 1900;
451 }
452 if ($syear >= 50 && $syear < 100) {
453 $syear += 2000;
454 }
455 $string = sprintf("%04d%02d%02d%02d%02d%02d", $syear, $smonth, $sday, $shour, $smin, $ssec);
456 } 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)
457 || 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
458 || 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
459 ) {
460 $syear = (int) $reg[1];
461 $smonth = (int) $reg[2];
462 $sday = (int) $reg[3];
463 $shour = (int) $reg[4];
464 $smin = (int) $reg[5];
465 $ssec = (int) $reg[6];
466 $string = sprintf("%04d%02d%02d%02d%02d%02d", $syear, $smonth, $sday, $shour, $smin, $ssec);
467 }
468
469 $string = preg_replace('/([^0-9])/i', '', $string);
470 $tmp = $string.'000000';
471 // Clean $gm
472 if ($gm === 1) {
473 $gm = 'gmt';
474 } elseif (empty($gm) || $gm === 'tzserver') {
475 $gm = 'tzserver';
476 }
477
478 $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);
479
480 return $date;
481}
482
483
492function dol_get_prev_day($day, $month, $year)
493{
494 $time = dol_mktime(12, 0, 0, $month, $day, $year, 1, 0);
495 $time -= 24 * 60 * 60;
496 $tmparray = dol_getdate($time, true);
497 return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
498}
499
508function dol_get_next_day($day, $month, $year)
509{
510 $time = dol_mktime(12, 0, 0, $month, $day, $year, 1, 0);
511 $time += 24 * 60 * 60;
512 $tmparray = dol_getdate($time, true);
513 return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
514}
515
523function dol_get_prev_month($month, $year)
524{
525 if ($month == 1) {
526 $prev_month = 12;
527 $prev_year = $year - 1;
528 } else {
529 $prev_month = $month - 1;
530 $prev_year = $year;
531 }
532 return array('year' => $prev_year, 'month' => $prev_month);
533}
534
542function dol_get_next_month($month, $year)
543{
544 if ($month == 12) {
545 $next_month = 1;
546 $next_year = $year + 1;
547 } else {
548 $next_month = $month + 1;
549 $next_year = $year;
550 }
551 return array('year' => $next_year, 'month' => $next_month);
552}
553
563function dol_get_prev_week($day, $week, $month, $year)
564{
565 $tmparray = dol_get_first_day_week($day, $month, $year);
566
567 $time = dol_mktime(12, 0, 0, $month, $tmparray['first_day'], $year, 1, 0);
568 $time -= 24 * 60 * 60 * 7;
569 $tmparray = dol_getdate($time, true);
570 return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
571}
572
582function dol_get_next_week($day, $week, $month, $year)
583{
584 $tmparray = dol_get_first_day_week($day, $month, $year);
585
586 $time = dol_mktime(12, 0, 0, $tmparray['first_month'], $tmparray['first_day'], $tmparray['first_year'], 1, 0);
587 $time += 24 * 60 * 60 * 7;
588 $tmparray = dol_getdate($time, true);
589
590 return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
591}
592
604function dol_get_first_day($year, $month = 1, $gm = false)
605{
606 if ($year > 9999) {
607 return '';
608 }
609 return dol_mktime(0, 0, 0, $month, 1, $year, $gm);
610}
611
612
623function dol_get_last_day($year, $month = 12, $gm = false)
624{
625 if ($year > 9999) {
626 return '';
627 }
628 if ($month == 12) {
629 $month = 1;
630 $year += 1;
631 } else {
632 $month += 1;
633 }
634
635 // On se deplace au debut du mois suivant, et on retire un jour
636 $datelim = dol_mktime(23, 59, 59, $month, 1, $year, $gm);
637 $datelim -= (3600 * 24);
638
639 return $datelim;
640}
641
650function dol_get_last_hour($date, $gm = 'tzserver')
651{
652 $tmparray = dol_getdate($date, false, ($gm == 'gmt' ? 'gmt' : ''));
653 return dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year'], $gm);
654}
655
664function dol_get_first_hour($date, $gm = 'tzserver')
665{
666 $tmparray = dol_getdate($date, false, ($gm == 'gmt' ? 'gmt' : ''));
667 return dol_mktime(0, 0, 0, $tmparray['mon'], $tmparray['mday'], $tmparray['year'], $gm);
668}
669
679function dol_get_first_day_week($day, $month, $year, $gm = false)
680{
681 //$day=2; $month=2; $year=2015;
682 $date = dol_mktime(0, 0, 0, $month, $day, $year, $gm);
683
684 //Checking conf of start week
685 $start_week = getDolGlobalInt('MAIN_START_WEEK', 1);
686
687 $tmparray = dol_getdate($date, true); // detail of current day
688
689 //Calculate days = offset from current day
690 $days = $start_week - $tmparray['wday'];
691 if ($days >= 1) {
692 $days = 7 - $days;
693 }
694 $days = abs($days);
695 $seconds = $days * 24 * 60 * 60;
696 //print 'start_week='.$start_week.' tmparray[wday]='.$tmparray['wday'].' day offset='.$days.' seconds offset='.$seconds.'<br>';
697
698 //Get first day of week
699 $tmpdaytms = (int) date((string) $tmparray['0']) - $seconds; // $tmparray[0] is day of parameters
700 $tmpday = idate("d", $tmpdaytms);
701
702 //Check first day of week is in same month than current day or not
703 if ($tmpday > $day) {
704 $prev_month = $month - 1;
705 $prev_year = $year;
706
707 if ($prev_month == 0) {
708 $prev_month = 12;
709 $prev_year = $year - 1;
710 }
711 } else {
712 $prev_month = $month;
713 $prev_year = $year;
714 }
715 $tmpmonth = $prev_month;
716 $tmpyear = $prev_year;
717
718 //Get first day of next week
719 $tmptime = dol_mktime(12, 0, 0, $month, $tmpday, $year, 1, 0);
720 $tmptime -= 24 * 60 * 60 * 7;
721 $tmparray = dol_getdate($tmptime, true);
722 $prev_day = $tmparray['mday'];
723
724 //Check prev day of week is in same month than first day or not
725 if ($prev_day > $tmpday) {
726 $prev_month = $month - 1;
727 $prev_year = $year;
728
729 if ($prev_month == 0) {
730 $prev_month = 12;
731 $prev_year = $year - 1;
732 }
733 }
734
735 $week = date("W", dol_mktime(0, 0, 0, $tmpmonth, $tmpday, $tmpyear, $gm));
736
737 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);
738}
739
747function getGMTEasterDatetime($year)
748{
749 $base = new DateTime("$year-03-21", new DateTimeZone("UTC"));
750 $days = easter_days($year); // Return number of days between 21 march and easter day.
751 $tmp = $base->add(new DateInterval("P{$days}D"));
752 return $tmp->getTimestamp();
753}
754
771function num_public_holiday($timestampStart, $timestampEnd, $countryCodeOrId = '', $lastday = 0, $includesaturday = -1, $includesunday = -1, $includefriday = -1, $includemonday = -1)
772{
773 global $conf, $db, $mysoc;
774
775 $nbFerie = 0;
776
777 // Check to ensure we use correct parameters
778 if (($timestampEnd - $timestampStart) % 86400 != 0) {
779 return 'Error Dates must use same hours and must be GMT dates';
780 }
781
782 if (empty($countryCodeOrId)) {
783 $countryCodeOrId = $mysoc->country_code;
784 }
785 if ($includemonday < 0) {
786 $includemonday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_MONDAY', 0);
787 }
788 if ($includefriday < 0) {
789 $includefriday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_FRIDAY', 0);
790 }
791 if ($includesaturday < 0) {
792 $includesaturday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY', 1);
793 }
794 if ($includesunday < 0) {
795 $includesunday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY', 1);
796 }
797
798 if (is_numeric($countryCodeOrId)) {
799 $country_id = $countryCodeOrId;
800 } else {
801 $country_id = dol_getIdFromCode($db, $countryCodeOrId, 'c_country', 'code', 'rowid');
802 }
803
804 if (empty($conf->cache['arrayOfActivePublicHolidays_'.$country_id])) {
805 // Loop on public holiday defined into hrm_public_holiday for the day, month and year analyzed
806 $tmpArrayOfPublicHolidays = array();
807 $sql = "SELECT id, code, entity, fk_country, dayrule, year, month, day, active";
808 $sql .= " FROM ".MAIN_DB_PREFIX."c_hrm_public_holiday";
809 $sql .= " WHERE active = 1 and fk_country IN (0".($country_id > 0 ? ", ".$country_id : 0).")";
810 $sql .= " AND entity IN (0," .getEntity('holiday') .")";
811
812 $resql = $db->query($sql);
813 if ($resql) {
814 $num_rows = $db->num_rows($resql);
815 $i = 0;
816 while ($i < $num_rows) {
817 $obj = $db->fetch_object($resql);
818 $tmpArrayOfPublicHolidays[$obj->id] = array('dayrule' => $obj->dayrule, 'year' => $obj->year, 'month' => $obj->month, 'day' => $obj->day);
819 $i++;
820 }
821 } else {
822 dol_syslog($db->lasterror(), LOG_ERR);
823 return 'Error sql '.$db->lasterror();
824 }
825
826 //var_dump($tmpArrayOfPublicHolidays);
827 $conf->cache['arrayOfActivePublicHolidays_'.$country_id] = $tmpArrayOfPublicHolidays;
828 }
829
830 $arrayOfPublicHolidays = $conf->cache['arrayOfActivePublicHolidays_'.$country_id];
831
832 $i = 0;
833 while ((($lastday == 0 && $timestampStart < $timestampEnd) || ($lastday && $timestampStart <= $timestampEnd))
834 && ($i < 50000)) { // Loop end when equals (Test on i is a security loop to avoid infinite loop)
835 $ferie = false;
836 $specialdayrule = array();
837
838 $jour = (int) gmdate("d", $timestampStart);
839 $mois = (int) gmdate("m", $timestampStart);
840 $annee = (int) gmdate("Y", $timestampStart);
841 $jour_semaine = (int) gmdate("w", $timestampStart); // sunday = 0, monday = 1, ...
842
843 //print "jour=".$jour." month=".$mois." year=".$annee." includesaturday=".$includesaturday." includesunday=".$includesunday."\n";
844 foreach ($arrayOfPublicHolidays as $entrypublicholiday) {
845 if (!empty($entrypublicholiday['dayrule']) && $entrypublicholiday['dayrule'] != 'date') { // For example 'easter', '...'
846 $specialdayrule[$entrypublicholiday['dayrule']] = $entrypublicholiday['dayrule'];
847 } else {
848 $match = 1;
849 if (!empty($entrypublicholiday['year']) && $entrypublicholiday['year'] != $annee) {
850 $match = 0;
851 }
852 if ($entrypublicholiday['month'] != $mois) {
853 $match = 0;
854 }
855 if ($entrypublicholiday['day'] != $jour) {
856 $match = 0;
857 }
858
859 if ($match) {
860 $ferie = true;
861 }
862 }
863
864 $i++;
865 }
866 //var_dump($specialdayrule)."\n";
867 //print "ferie=".$ferie."\n";
868
869 if (!$ferie) {
870 // Special dayrules
871 if (in_array('easter', $specialdayrule)) {
872 // Calculation for easter date
873 $date_paques = getGMTEasterDatetime($annee);
874 $jour_paques = gmdate("d", $date_paques);
875 $mois_paques = gmdate("m", $date_paques);
876 if ($jour_paques == $jour && $mois_paques == $mois) {
877 $ferie = true;
878 }
879 // Easter (sunday)
880 }
881
882 if (in_array('eastermonday', $specialdayrule)) {
883 // Calculation for the monday of easter date
884 $date_paques = getGMTEasterDatetime($annee);
885 //print 'PPP'.$date_paques.' '.dol_print_date($date_paques, 'dayhour', 'gmt')." ";
886 $date_lundi_paques = $date_paques + (3600 * 24);
887 $jour_lundi_paques = gmdate("d", $date_lundi_paques);
888 $mois_lundi_paques = gmdate("m", $date_lundi_paques);
889 if ($jour_lundi_paques == $jour && $mois_lundi_paques == $mois) {
890 $ferie = true;
891 }
892 // Easter (monday)
893 //print 'annee='.$annee.' $jour='.$jour.' $mois='.$mois.' $jour_lundi_paques='.$jour_lundi_paques.' $mois_lundi_paques='.$mois_lundi_paques."\n";
894 }
895
896 //Good Friday
897 if (in_array('goodfriday', $specialdayrule)) {
898 // Pulls the date of Easter
899 $easter = getGMTEasterDatetime($annee);
900
901 // Calculates the date of Good Friday based on Easter
902 $date_good_friday = $easter - (2 * 3600 * 24);
903 $dom_good_friday = gmdate("d", $date_good_friday);
904 $month_good_friday = gmdate("m", $date_good_friday);
905
906 if ($dom_good_friday == $jour && $month_good_friday == $mois) {
907 $ferie = true;
908 }
909 }
910
911 if (in_array('ascension', $specialdayrule)) {
912 // Calcul du jour de l'ascension (39 days after easter day)
913 $date_paques = getGMTEasterDatetime($annee);
914 $date_ascension = $date_paques + (3600 * 24 * 39);
915 $jour_ascension = gmdate("d", $date_ascension);
916 $mois_ascension = gmdate("m", $date_ascension);
917 if ($jour_ascension == $jour && $mois_ascension == $mois) {
918 $ferie = true;
919 }
920 // Ascension (thursday)
921 }
922
923 if (in_array('pentecost', $specialdayrule)) {
924 // Calculation of "Pentecote" (49 days after easter day)
925 $date_paques = getGMTEasterDatetime($annee);
926 $date_pentecote = $date_paques + (3600 * 24 * 49);
927 $jour_pentecote = gmdate("d", $date_pentecote);
928 $mois_pentecote = gmdate("m", $date_pentecote);
929 if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
930 $ferie = true;
931 }
932 // "Pentecote" (sunday)
933 }
934 if (in_array('pentecotemonday', $specialdayrule)) {
935 // Calculation of "Pentecote" (49 days after easter day)
936 $date_paques = getGMTEasterDatetime($annee);
937 $date_pentecote = $date_paques + (3600 * 24 * 50);
938 $jour_pentecote = gmdate("d", $date_pentecote);
939 $mois_pentecote = gmdate("m", $date_pentecote);
940 if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
941 $ferie = true;
942 }
943 // "Pentecote" (monday)
944 }
945
946 if (in_array('viernessanto', $specialdayrule)) {
947 // Viernes Santo
948 $date_paques = getGMTEasterDatetime($annee);
949 $date_viernes = $date_paques - (3600 * 24 * 2);
950 $jour_viernes = gmdate("d", $date_viernes);
951 $mois_viernes = gmdate("m", $date_viernes);
952 if ($jour_viernes == $jour && $mois_viernes == $mois) {
953 $ferie = true;
954 }
955 //Viernes Santo
956 }
957
958 if (in_array('fronleichnam', $specialdayrule)) {
959 // Fronleichnam (60 days after easter sunday)
960 $date_paques = getGMTEasterDatetime($annee);
961 $date_fronleichnam = $date_paques + (3600 * 24 * 60);
962 $jour_fronleichnam = gmdate("d", $date_fronleichnam);
963 $mois_fronleichnam = gmdate("m", $date_fronleichnam);
964 if ($jour_fronleichnam == $jour && $mois_fronleichnam == $mois) {
965 $ferie = true;
966 }
967 // Fronleichnam
968 }
969
970 if (in_array('genevafast', $specialdayrule)) {
971 // Geneva fast in Switzerland (Thursday after the first sunday in September)
972 $date_1sunsept = strtotime('next thursday', strtotime('next sunday', mktime(0, 0, 0, 9, 1, $annee)));
973 $jour_1sunsept = date("d", $date_1sunsept);
974 $mois_1sunsept = date("m", $date_1sunsept);
975 if ($jour_1sunsept == $jour && $mois_1sunsept == $mois) {
976 $ferie = true;
977 }
978 // Geneva fast in Switzerland
979 }
980 }
981 //print "ferie afterspe=".$ferie."\n";
982
983 // If we have to include Friday, Saturday and Sunday
984 if (!$ferie) {
985 if ($includefriday || $includesaturday || $includesunday || $includemonday) {
986 //Monday (1), Friday (5), Saturday (6) and Sunday (0)
987 if ($includefriday && $jour_semaine == 5) {
988 $ferie = true;
989 }
990 if ($includesaturday && $jour_semaine == 6) {
991 $ferie = true;
992 }
993 if ($includesunday && $jour_semaine == 0) {
994 $ferie = true;
995 }
996 if ($includemonday && $jour_semaine == 1) {
997 $ferie = true;
998 }
999 }
1000 }
1001 //print "ferie afterincludexxxday=".$ferie."\n";
1002
1003 // We increase the counter of non working day
1004 if ($ferie) {
1005 $nbFerie++;
1006 }
1007
1008 // Increase number of days (on go up into loop)
1009 //var_dump("before ".$jour.' '.$mois.' '.$annee.' '.$timestampStart);
1010 $timestampStart = dol_time_plus_duree($timestampStart, 1, 'd');
1011 //var_dump("after ".$jour.' '.$mois.' '.$annee.' '.$timestampStart);
1012
1013 $i++;
1014 }
1015
1016 //print "nbFerie=".$nbFerie."\n";
1017 return $nbFerie;
1018}
1019
1036function listPublicHoliday($timestampStart, $timestampEnd, $countryCodeOrId = '', $lastday = 0, $excludesaturday = -1, $excludesunday = -1, $excludefriday = -1, $excludemonday = -1)
1037{
1038 global $conf, $db, $mysoc;
1039
1040 // Check to ensure we use correct parameters
1041 if (($timestampEnd - $timestampStart) % 86400 != 0) {
1042 return 'Error Dates must use same hours and must be GMT dates';
1043 }
1044
1045 if (empty($countryCodeOrId)) {
1046 $countryCodeOrId = $mysoc->country_code;
1047 }
1048 if ($excludemonday < 0) {
1049 $excludemonday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_MONDAY', 0);
1050 }
1051 if ($excludefriday < 0) {
1052 $excludefriday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_FRIDAY', 0);
1053 }
1054 if ($excludesaturday < 0) {
1055 $excludesaturday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY', 1);
1056 }
1057 if ($excludesunday < 0) {
1058 $excludesunday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY', 1);
1059 }
1060
1061 if (is_numeric($countryCodeOrId)) {
1062 $country_id = $countryCodeOrId;
1063 } else {
1064 $country_id = dol_getIdFromCode($db, $countryCodeOrId, 'c_country', 'code', 'rowid');
1065 }
1066
1067 if (empty($conf->cache['arrayOfActivePublicHolidays_' . $country_id])) {
1068 // Loop on public holiday defined into hrm_public_holiday for the day, month and year analyzed
1069 $tmpArrayOfPublicHolidays = array();
1070 $sql = "SELECT id, code, entity, fk_country, dayrule, year, month, day, active";
1071 $sql .= " FROM " . MAIN_DB_PREFIX . "c_hrm_public_holiday";
1072 $sql .= " WHERE active = 1 and fk_country IN (0" . ($country_id > 0 ? ", " . $country_id : 0) . ")";
1073 $sql .= " AND entity IN (0," . getEntity('holiday') . ")";
1074
1075 $resql = $db->query($sql);
1076 if ($resql) {
1077 $num_rows = $db->num_rows($resql);
1078 $i = 0;
1079 while ($i < $num_rows) {
1080 $obj = $db->fetch_object($resql);
1081 $tmpArrayOfPublicHolidays[$obj->id] = array('dayrule' => $obj->dayrule, 'year' => $obj->year, 'month' => $obj->month, 'day' => $obj->day);
1082 $i++;
1083 }
1084 } else {
1085 dol_syslog($db->lasterror(), LOG_ERR);
1086 return 'Error sql ' . $db->lasterror();
1087 }
1088
1089 //var_dump($tmpArrayOfPublicHolidays);
1090 $conf->cache['arrayOfActivePublicHolidays_' . $country_id] = $tmpArrayOfPublicHolidays;
1091 }
1092
1093 $arrayOfPublicHolidays = $conf->cache['arrayOfActivePublicHolidays_' . $country_id];
1094 $listFeries = [];
1095 $i = 0;
1096 while ((($lastday == 0 && $timestampStart < $timestampEnd) || ($lastday && $timestampStart <= $timestampEnd))
1097 && ($i < 50000)) { // Loop end when equals (Test on i is a security loop to avoid infinite loop)
1098 $nonWorkingDay = false;
1099 $ferie = false;
1100 $specialdayrule = array();
1101
1102 $jour = (int) gmdate("d", $timestampStart);
1103 $mois = (int) gmdate("m", $timestampStart);
1104 $annee = (int) gmdate("Y", $timestampStart);
1105
1106 // If we have to exclude Friday, Saturday and Sunday
1107 if ($excludefriday || $excludesaturday || $excludesunday) {
1108 $jour_julien = unixtojd($timestampStart);
1109 $jour_semaine = jddayofweek($jour_julien, 0);
1110 if ($excludefriday) { //Friday (5), Saturday (6) and Sunday (0)
1111 if ($jour_semaine == 5) {
1112 $nonWorkingDay = true;
1113 }
1114 }
1115 if ($excludesaturday) { //Friday (5), Saturday (6) and Sunday (0)
1116 if ($jour_semaine == 6) {
1117 $nonWorkingDay = true;
1118 }
1119 }
1120 if ($excludesunday) { //Friday (5), Saturday (6) and Sunday (0)
1121 if ($jour_semaine == 0) {
1122 $nonWorkingDay = true;
1123 }
1124 }
1125 }
1126 //print "ferie=".$nonWorkingDay."\n";
1127
1128 if (!$nonWorkingDay) {
1129 //print "jour=".$jour." month=".$mois." year=".$annee." includesaturday=".$excludesaturday." includesunday=".$excludesunday."\n";
1130 foreach ($arrayOfPublicHolidays as $entrypublicholiday) {
1131 if (!empty($entrypublicholiday['dayrule']) && $entrypublicholiday['dayrule'] != 'date') { // For example 'easter', '...'
1132 $specialdayrule[$entrypublicholiday['dayrule']] = $entrypublicholiday['dayrule'];
1133 } else {
1134 $match = 1;
1135 if (!empty($entrypublicholiday['year']) && $entrypublicholiday['year'] != $annee) {
1136 $match = 0;
1137 }
1138 if ($entrypublicholiday['month'] != $mois) {
1139 $match = 0;
1140 }
1141 if ($entrypublicholiday['day'] != $jour) {
1142 $match = 0;
1143 }
1144
1145 if ($match) {
1146 $ferie = true;
1147 $listFeries[] = $timestampStart;
1148 }
1149 }
1150
1151 $i++;
1152 }
1153 //var_dump($specialdayrule)."\n";
1154 //print "ferie=".$nonWorkingDay."\n";
1155 }
1156
1157 if (!$nonWorkingDay && !$ferie) {
1158 // Special dayrules
1159 if (in_array('easter', $specialdayrule)) {
1160 // Calculation for easter date
1161 $date_paques = getGMTEasterDatetime($annee);
1162 $jour_paques = gmdate("d", $date_paques);
1163 $mois_paques = gmdate("m", $date_paques);
1164 if ($jour_paques == $jour && $mois_paques == $mois) {
1165 $ferie = true;
1166 $listFeries[] = $timestampStart;
1167 }
1168 // Easter (sunday)
1169 }
1170
1171 if (in_array('eastermonday', $specialdayrule)) {
1172 // Calculation for the monday of easter date
1173 $date_paques = getGMTEasterDatetime($annee);
1174 //print 'PPP'.$date_paques.' '.dol_print_date($date_paques, 'dayhour', 'gmt')." ";
1175 $date_lundi_paques = $date_paques + (3600 * 24);
1176 $jour_lundi_paques = gmdate("d", $date_lundi_paques);
1177 $mois_lundi_paques = gmdate("m", $date_lundi_paques);
1178 if ($jour_lundi_paques == $jour && $mois_lundi_paques == $mois) {
1179 $ferie = true;
1180 $listFeries[] = $timestampStart;
1181 }
1182 // Easter (monday)
1183 //print 'annee='.$annee.' $jour='.$jour.' $mois='.$mois.' $jour_lundi_paques='.$jour_lundi_paques.' $mois_lundi_paques='.$mois_lundi_paques."\n";
1184 }
1185
1186 //Good Friday
1187 if (in_array('goodfriday', $specialdayrule)) {
1188 // Pulls the date of Easter
1189 $easter = getGMTEasterDatetime($annee);
1190
1191 // Calculates the date of Good Friday based on Easter
1192 $date_good_friday = $easter - (2 * 3600 * 24);
1193 $dom_good_friday = gmdate("d", $date_good_friday);
1194 $month_good_friday = gmdate("m", $date_good_friday);
1195
1196 if ($dom_good_friday == $jour && $month_good_friday == $mois) {
1197 $ferie = true;
1198 $listFeries[] = $timestampStart;
1199 }
1200 }
1201
1202 if (in_array('ascension', $specialdayrule)) {
1203 // Calcul du jour de l'ascension (39 days after easter day)
1204 $date_paques = getGMTEasterDatetime($annee);
1205 $date_ascension = $date_paques + (3600 * 24 * 39);
1206 $jour_ascension = gmdate("d", $date_ascension);
1207 $mois_ascension = gmdate("m", $date_ascension);
1208 if ($jour_ascension == $jour && $mois_ascension == $mois) {
1209 $ferie = true;
1210 $listFeries[] = $timestampStart;
1211 }
1212 // Ascension (thursday)
1213 }
1214
1215 if (in_array('pentecost', $specialdayrule)) {
1216 // Calculation of "Pentecote" (49 days after easter day)
1217 $date_paques = getGMTEasterDatetime($annee);
1218 $date_pentecote = $date_paques + (3600 * 24 * 49);
1219 $jour_pentecote = gmdate("d", $date_pentecote);
1220 $mois_pentecote = gmdate("m", $date_pentecote);
1221 if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
1222 $ferie = true;
1223 $listFeries[] = $timestampStart;
1224 }
1225 // "Pentecote" (sunday)
1226 }
1227
1228 if (in_array('pentecotemonday', $specialdayrule)) {
1229 // Calculation of "Pentecote" (49 days after easter day)
1230 $date_paques = getGMTEasterDatetime($annee);
1231 $date_pentecote = $date_paques + (3600 * 24 * 50);
1232 $jour_pentecote = gmdate("d", $date_pentecote);
1233 $mois_pentecote = gmdate("m", $date_pentecote);
1234 if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
1235 $ferie = true;
1236 $listFeries[] = $timestampStart;
1237 }
1238 // "Pentecote" (monday)
1239 }
1240
1241 if (in_array('viernessanto', $specialdayrule)) {
1242 // Viernes Santo
1243 $date_paques = getGMTEasterDatetime($annee);
1244 $date_viernes = $date_paques - (3600 * 24 * 2);
1245 $jour_viernes = gmdate("d", $date_viernes);
1246 $mois_viernes = gmdate("m", $date_viernes);
1247 if ($jour_viernes == $jour && $mois_viernes == $mois) {
1248 $ferie = true;
1249 $listFeries[] = $timestampStart;
1250 }
1251 //Viernes Santo
1252 }
1253
1254 if (in_array('fronleichnam', $specialdayrule)) {
1255 // Fronleichnam (60 days after easter sunday)
1256 $date_paques = getGMTEasterDatetime($annee);
1257 $date_fronleichnam = $date_paques + (3600 * 24 * 60);
1258 $jour_fronleichnam = gmdate("d", $date_fronleichnam);
1259 $mois_fronleichnam = gmdate("m", $date_fronleichnam);
1260 if ($jour_fronleichnam == $jour && $mois_fronleichnam == $mois) {
1261 $ferie = true;
1262 $listFeries[] = $timestampStart;
1263 }
1264 // Fronleichnam
1265 }
1266
1267 if (in_array('genevafast', $specialdayrule)) {
1268 // Geneva fast in Switzerland (Thursday after the first sunday in September)
1269 $date_1sunsept = strtotime('next thursday', strtotime('next sunday', mktime(0, 0, 0, 9, 1, $annee)));
1270 $jour_1sunsept = date("d", $date_1sunsept);
1271 $mois_1sunsept = date("m", $date_1sunsept);
1272 if ($jour_1sunsept == $jour && $mois_1sunsept == $mois) {
1273 $ferie = true;
1274 $listFeries[] = $timestampStart;
1275 }
1276 // Geneva fast in Switzerland
1277 }
1278 }
1279 //print "ferie=".$nonWorkingDay."\n";
1280
1281 // Increase number of days (on go up into loop)
1282 $timestampStart = dol_time_plus_duree($timestampStart, 1, 'd');
1283 //var_dump($jour.' '.$mois.' '.$annee.' '.$timestampStart);
1284
1285 $i++;
1286 }
1287
1288 //print "nbFerie=".$nbFerie."\n";
1289 return $listFeries;
1290}
1291
1302function num_between_day($timestampStart, $timestampEnd, $lastday = 0)
1303{
1304 if ($timestampStart <= $timestampEnd) {
1305 if ($lastday == 1) {
1306 $bit = 0;
1307 } else {
1308 $bit = 1;
1309 }
1310 $nbjours = (int) floor(($timestampEnd - $timestampStart) / (60 * 60 * 24)) + 1 - $bit;
1311 } else {
1312 $nbjours = 0;
1313 }
1314 //print ($timestampEnd - $timestampStart) - $lastday;
1315 return $nbjours;
1316}
1317
1334function num_open_day($timestampStart, $timestampEnd, $inhour = 0, $lastday = 0, $halfday = 0, $countryCodeOrId = '', $user_id = 0)
1335{
1336 global $langs, $mysoc, $hookmanager;
1337
1338 if (empty($countryCodeOrId) || $countryCodeOrId < 0) {
1339 $countryCodeOrId = $mysoc->country_code;
1340 }
1341
1342 dol_syslog('num_open_day timestampStart='.$timestampStart.' timestampEnd='.$timestampEnd.' bit='.$lastday.' countryCodeOrId='.$countryCodeOrId);
1343
1344 // Check parameters
1345 if (!is_int($timestampStart) && !is_float($timestampStart)) {
1346 return 'ErrorBadParameter_num_open_day';
1347 }
1348 if (!is_int($timestampEnd) && !is_float($timestampEnd)) {
1349 return 'ErrorBadParameter_num_open_day';
1350 }
1351
1352 $nbOpenDay = 0; // expressed in days; inhour conversion happens at the end
1353
1354 if ($timestampStart < $timestampEnd) {
1355 // --- 1. Calculate Gross Working Days ---
1356 // Gross working days = total days in range - non-working days (weekends & public holidays).
1357 $a = num_between_day($timestampStart, $timestampEnd, $lastday);
1358 $b = num_public_holiday($timestampStart, $timestampEnd, $countryCodeOrId, $lastday);
1359 $nbOpenDay = $a - $b;
1360
1361 // --- 2. Apply Contextual Half-Day Deductions ---
1362 $halfday = (int) $halfday; // Ensure $halfday is an integer for reliable comparisons.
1363
1364 // Check if start/end days are working days just ONCE to optimize performance
1365 // by avoiding redundant calls to the potentially slow num_public_holiday() function.
1366 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1367 $isStartDayWorking = (num_public_holiday($timestampStart, $timestampStart, $countryCodeOrId, 1) == 0);
1368 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1369 $isEndDayWorking = (num_public_holiday($timestampEnd, $timestampEnd, $countryCodeOrId, 1) == 0);
1370
1371 // Deduct 0.5 if the leave starts in the afternoon of a working day.
1372 if (($halfday == -1 || $halfday == 2) && $isStartDayWorking) {
1373 $nbOpenDay -= 0.5;
1374 }
1375
1376 // Deduct 0.5 if the leave ends in the morning of a different, working day.
1377 if (($halfday == 1 || $halfday == 2) && date('Y-m-d', $timestampStart) != date('Y-m-d', $timestampEnd) && $isEndDayWorking) {
1378 $nbOpenDay -= 0.5;
1379 }
1380 } elseif ($timestampStart == $timestampEnd) {
1381 $isSingleDayHoliday = false;
1382 if ($lastday) {
1383 $numholidays = num_public_holiday($timestampStart, $timestampEnd, $countryCodeOrId, $lastday);
1384 if ($numholidays == 1) {
1385 $isSingleDayHoliday = true;
1386 }
1387 }
1388 if (!$isSingleDayHoliday) {
1389 $nbOpenDay = $lastday - 0.5 * abs((int) $halfday);
1390 }
1391 } else {
1392 return $langs->trans("Error");
1393 }
1394
1395 // --- 3. Allow modules to adjust the result based on the user (e.g. per-employee
1396 // individual workdays, bridge days, fixed half-days). Only fires when caller
1397 // opts in by passing a non-zero $user_id, so existing call sites are unaffected.
1398 if ($user_id > 0 && is_object($hookmanager)) {
1399 $parameters = array(
1400 'timestampStart' => $timestampStart,
1401 'timestampEnd' => $timestampEnd,
1402 'inhour' => $inhour,
1403 'lastday' => $lastday,
1404 'halfday' => $halfday,
1405 'countryCodeOrId' => $countryCodeOrId,
1406 'user_id' => $user_id,
1407 'nbOpenDay' => $nbOpenDay,
1408 );
1409 $action = '';
1410 $reshook = $hookmanager->executeHooks('numOpenDay', $parameters, $hookmanager, $action);
1411 if ($reshook > 0 && isset($hookmanager->resArray['nbOpenDay'])) {
1412 $nbOpenDay = $hookmanager->resArray['nbOpenDay'];
1413 }
1414 }
1415
1416 if ($inhour == 1) {
1417 return (int) ($nbOpenDay * 24);
1418 }
1419 return $nbOpenDay;
1420}
1421
1422
1423
1432function monthArray($outputlangs, $short = 0)
1433{
1434 $montharray = array(
1435 1 => $outputlangs->trans("Month01"),
1436 2 => $outputlangs->trans("Month02"),
1437 3 => $outputlangs->trans("Month03"),
1438 4 => $outputlangs->trans("Month04"),
1439 5 => $outputlangs->trans("Month05"),
1440 6 => $outputlangs->trans("Month06"),
1441 7 => $outputlangs->trans("Month07"),
1442 8 => $outputlangs->trans("Month08"),
1443 9 => $outputlangs->trans("Month09"),
1444 10 => $outputlangs->trans("Month10"),
1445 11 => $outputlangs->trans("Month11"),
1446 12 => $outputlangs->trans("Month12")
1447 );
1448
1449 if (!empty($short)) {
1450 $montharray = array(
1451 1 => $outputlangs->trans("MonthShort01"),
1452 2 => $outputlangs->trans("MonthShort02"),
1453 3 => $outputlangs->trans("MonthShort03"),
1454 4 => $outputlangs->trans("MonthShort04"),
1455 5 => $outputlangs->trans("MonthShort05"),
1456 6 => $outputlangs->trans("MonthShort06"),
1457 7 => $outputlangs->trans("MonthShort07"),
1458 8 => $outputlangs->trans("MonthShort08"),
1459 9 => $outputlangs->trans("MonthShort09"),
1460 10 => $outputlangs->trans("MonthShort10"),
1461 11 => $outputlangs->trans("MonthShort11"),
1462 12 => $outputlangs->trans("MonthShort12")
1463 );
1464 }
1465
1466 return $montharray;
1467}
1468
1476function getWeekNumbersOfMonth($month, $year)
1477{
1478 $nb_days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
1479 $TWeek = array();
1480 for ($day = 1; $day < $nb_days; $day++) {
1481 $week_number = getWeekNumber($day, $month, $year);
1482 $TWeek[$week_number] = $week_number;
1483 }
1484 return $TWeek;
1485}
1486
1494function getFirstDayOfEachWeek($TWeek, $year)
1495{
1496 $TFirstDayOfWeek = array();
1497 foreach ($TWeek as $weekNb) {
1498 if (in_array('01', $TWeek) && in_array('52', $TWeek) && $weekNb == '01') {
1499 $year++; //Si on a la 1re semaine et la semaine 52 c'est qu'on change d'année
1500 }
1501 $TFirstDayOfWeek[$weekNb] = date('d', strtotime($year.'W'.$weekNb));
1502 }
1503 return $TFirstDayOfWeek;
1504}
1505
1513function getLastDayOfEachWeek($TWeek, $year)
1514{
1515 $TLastDayOfWeek = array();
1516 foreach ($TWeek as $weekNb) {
1517 $TLastDayOfWeek[$weekNb] = date('d', strtotime($year.'W'.$weekNb.'+6 days'));
1518 }
1519 return $TLastDayOfWeek;
1520}
1521
1530function getWeekNumber($day, $month, $year)
1531{
1532 $date = new DateTime($year.'-'.$month.'-'.$day);
1533 $week = $date->format("W");
1534 return $week;
1535}
global $mysoc
dol_get_prev_month($month, $year)
Return previous month.
Definition date.lib.php:523
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:664
dol_get_next_day($day, $month, $year)
Return next day.
Definition date.lib.php:508
dol_get_next_week($day, $week, $month, $year)
Return next week.
Definition date.lib.php:582
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:386
getServerTimeZoneString()
Return server timezone string.
Definition date.lib.php:76
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:650
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:87
dol_get_first_day_week($day, $month, $year, $gm=false)
Return first day of week for a date.
Definition date.lib.php:679
getGMTEasterDatetime($year)
Return the easter day in GMT time.
Definition date.lib.php:747
num_open_day($timestampStart, $timestampEnd, $inhour=0, $lastday=0, $halfday=0, $countryCodeOrId='', $user_id=0)
Function to return number of working days (and text of units) between two dates (working days)
convertDurationtoHour($duration_value, $duration_unit)
Convert duration to hour.
Definition date.lib.php:342
get_tz_array()
Return an array with timezone values.
Definition date.lib.php:37
dol_get_prev_day($day, $month, $year)
Return previous day.
Definition date.lib.php:492
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:604
getFirstDayOfEachWeek($TWeek, $year)
Return array of first day of weeks.
dol_get_next_month($month, $year)
Return next month.
Definition date.lib.php:542
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:771
convertTime2Seconds($iHours=0, $iMinutes=0, $iSeconds=0)
Convert hours and minutes into seconds.
Definition date.lib.php:219
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:126
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:248
dol_get_prev_week($day, $week, $month, $year)
Return previous week.
Definition date.lib.php:563
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:435
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:623
monthArray($outputlangs, $short=0)
Return array of translated months or selected month.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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.
print $langs trans('Date')." left Ref 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 Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:487