dolibarr 22.0.5
date.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2011-2015 Juanjo Menent <jmenent@2byte.es>
5 * Copyright (C) 2017 Ferran Marcet <fmarcet@2byte.es>
6 * Copyright (C) 2018-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
125function dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth = 0)
126{
127 if (empty($duration_value)) {
128 return $time;
129 }
130 if ($duration_unit == 's') {
131 return $time + (int) ($duration_value);
132 }
133 if ($duration_unit == 'i' || $duration_unit == 'mn' || $duration_unit == 'min') {
134 return $time + (int) (60 * $duration_value);
135 }
136 if ($duration_unit == 'h') {
137 return $time + (int) (3600 * $duration_value);
138 }
139 if ($duration_unit == 'w') {
140 return $time + (int) (3600 * 24 * 7 * $duration_value);
141 }
142
143 $deltastring = 'P';
144
145 $sub = false;
146
147 if ($duration_value > 0) {
148 $deltastring .= abs($duration_value);
149 $sub = false;
150 }
151 if ($duration_value < 0) {
152 $deltastring .= abs($duration_value);
153 $sub = true;
154 }
155 if ($duration_unit == 'd') {
156 $deltastring .= "D";
157 }
158 if ($duration_unit == 'm') {
159 $deltastring .= "M";
160 }
161 if ($duration_unit == 'y') {
162 $deltastring .= "Y";
163 }
164
165 $date = new DateTime();
166 if (getDolGlobalString('MAIN_DATE_IN_MEMORY_ARE_GMT')) {
167 $date->setTimezone(new DateTimeZone('UTC'));
168 }
169 $date->setTimestamp((int) $time);
170 $interval = new DateInterval($deltastring);
171
172 if ($sub) {
173 $date->sub($interval);
174 } else {
175 $date->add($interval);
176 }
177 //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)
178 if ($ruleforendofmonth == 1 && $duration_unit == 'm') {
179 $timeyear = (int) dol_print_date($time, '%Y');
180 $timemonth = (int) dol_print_date($time, '%m');
181 $timetotalmonths = (($timeyear * 12) + $timemonth);
182
183 $monthsexpected = ($timetotalmonths + $duration_value);
184
185 $newtime = $date->getTimestamp();
186
187 $newtimeyear = (int) dol_print_date($newtime, '%Y');
188 $newtimemonth = (int) dol_print_date($newtime, '%m');
189 $newtimetotalmonths = (($newtimeyear * 12) + $newtimemonth);
190
191 if ($monthsexpected < $newtimetotalmonths) {
192 $newtimehours = (int) dol_print_date($newtime, '%H');
193 $newtimemins = (int) dol_print_date($newtime, '%M');
194 $newtimesecs = (int) dol_print_date($newtime, '%S');
195
196 $datelim = dol_mktime($newtimehours, $newtimemins, $newtimesecs, $newtimemonth, 1, $newtimeyear);
197 $datelim -= (3600 * 24);
198
199 $date->setTimestamp($datelim);
200 }
201 }
202 return $date->getTimestamp();
203}
204
205
215function convertTime2Seconds($iHours = 0, $iMinutes = 0, $iSeconds = 0)
216{
217 $iResult = ((int) $iHours * 3600) + ((int) $iMinutes * 60) + (int) $iSeconds;
218 return $iResult;
219}
220
221
244function convertSecondToTime($iSecond, $format = 'all', $lengthOfDay = 86400, $lengthOfWeek = 7)
245{
246 global $langs;
247
248 if (empty($lengthOfDay)) {
249 $lengthOfDay = 86400; // 1 day = 24 hours
250 }
251 if (empty($lengthOfWeek)) {
252 $lengthOfWeek = 7; // 1 week = 7 days
253 }
254 $nbHbyDay = $lengthOfDay / 3600;
255
256 $sTime = '';
257
258 if ($format == 'all' || $format == 'allwithouthour' || $format == 'allhour' || $format == 'allhourmin' || $format == 'allhourminsec') {
259 if ((int) $iSecond === 0) {
260 return '0'; // This is to avoid having 0 return a 12:00 AM for en_US
261 }
262
263 $sDay = 0;
264 $sWeek = 0;
265
266 if ($iSecond >= $lengthOfDay) {
267 for ($i = $iSecond; $i >= $lengthOfDay; $i -= $lengthOfDay) {
268 $sDay++;
269 $iSecond -= $lengthOfDay;
270 }
271 $dayTranslate = $langs->trans("Day");
272 if ($iSecond >= ($lengthOfDay * 2)) {
273 $dayTranslate = $langs->trans("Days");
274 }
275 }
276
277 if ($lengthOfWeek < 7) {
278 if ($sDay) {
279 if ($sDay >= $lengthOfWeek) {
280 $sWeek = (int) (($sDay - $sDay % $lengthOfWeek) / $lengthOfWeek);
281 $sDay %= $lengthOfWeek;
282 $weekTranslate = $langs->trans("DurationWeek");
283 if ($sWeek >= 2) {
284 $weekTranslate = $langs->trans("DurationWeeks");
285 }
286 $sTime .= $sWeek.' '.$weekTranslate.' ';
287 }
288 }
289 }
290 if ($sDay > 0) {
291 $dayTranslate = $langs->trans("Day");
292 if ($sDay > 1) {
293 $dayTranslate = $langs->trans("Days");
294 }
295 $sTime .= $sDay.' '.$langs->trans("d").' ';
296 }
297
298 if ($format == 'all') {
299 if ($iSecond || empty($sDay)) {
300 $sTime .= dol_print_date($iSecond, 'hourduration', true);
301 }
302 } elseif ($format == 'allhourminsec') {
303 return sprintf("%02d", ($sWeek * $lengthOfWeek * $nbHbyDay + $sDay * $nbHbyDay + (int) floor($iSecond / 3600))).':'.sprintf("%02d", ((int) floor(($iSecond % 3600) / 60))).':'.sprintf("%02d", ((int) ($iSecond % 60)));
304 } elseif ($format == 'allhourmin') {
305 return sprintf("%02d", ($sWeek * $lengthOfWeek * $nbHbyDay + $sDay * $nbHbyDay + (int) floor($iSecond / 3600))).':'.sprintf("%02d", ((int) floor(($iSecond % 3600) / 60)));
306 } elseif ($format == 'allhour') {
307 return sprintf("%02d", ($sWeek * $lengthOfWeek * $nbHbyDay + $sDay * $nbHbyDay + (int) floor($iSecond / 3600)));
308 }
309 } elseif ($format == 'hour') { // only hour part
310 $sTime = dol_print_date($iSecond, '%H', true);
311 } elseif ($format == 'fullhour') {
312 if (!empty($iSecond)) {
313 $iSecond /= 3600;
314 } else {
315 $iSecond = 0;
316 }
317 $sTime = $iSecond;
318 } elseif ($format == 'min') { // only min part
319 $sTime = dol_print_date($iSecond, '%M', true);
320 } elseif ($format == 'sec') { // only sec part
321 $sTime = dol_print_date($iSecond, '%S', true);
322 } elseif ($format == 'month') { // only month part
323 $sTime = dol_print_date($iSecond, '%m', true);
324 } elseif ($format == 'year') { // only year part
325 $sTime = dol_print_date($iSecond, '%Y', true);
326 }
327 return trim((string) $sTime);
328}
329
330
338function convertDurationtoHour($duration_value, $duration_unit)
339{
340 $result = 0;
341
342 if ($duration_unit == 's') {
343 $result = $duration_value / 3600;
344 }
345 if ($duration_unit == 'i' || $duration_unit == 'mn' || $duration_unit == 'min') {
346 $result = $duration_value / 60;
347 }
348 if ($duration_unit == 'h') {
349 $result = $duration_value;
350 }
351 if ($duration_unit == 'd') {
352 $result = $duration_value * 24;
353 }
354 if ($duration_unit == 'w') {
355 $result = $duration_value * 24 * 7;
356 }
357 if ($duration_unit == 'm') {
358 $result = $duration_value * 730.484;
359 }
360 if ($duration_unit == 'y') {
361 $result = $duration_value * 365 * 24;
362 }
363
364 return $result;
365}
366
382function dolSqlDateFilter($datefield, $day_date, $month_date, $year_date, $excludefirstand = 0, $gm = false)
383{
384 global $db;
385 $sqldate = '';
386
387 $day_date = intval($day_date);
388 $month_date = intval($month_date);
389 $year_date = intval($year_date);
390
391 if ($month_date > 0) {
392 if ($month_date > 12) { // protection for bad value of month
393 return " AND 1 = 2";
394 }
395 if ($year_date > 0 && empty($day_date)) {
396 $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, $month_date, $gm));
397 $sqldate .= "' AND '".$db->idate(dol_get_last_day($year_date, $month_date, $gm))."'";
398 } elseif ($year_date > 0 && !empty($day_date)) {
399 $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month_date, $day_date, $year_date, $gm));
400 $sqldate .= "' AND '".$db->idate(dol_mktime(23, 59, 59, $month_date, $day_date, $year_date, $gm))."'";
401 } else {
402 // This case is not reliable on TZ, but we should not need it.
403 $sqldate .= ($excludefirstand ? "" : " AND ")." date_format( ".$datefield.", '%c') = '".$db->escape((string) $month_date)."'";
404 }
405 } elseif ($year_date > 0) {
406 $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, 1, $gm));
407 $sqldate .= "' AND '".$db->idate(dol_get_last_day($year_date, 12, $gm))."'";
408 }
409 return $sqldate;
410}
411
431function dol_stringtotime($string, $gm = 1)
432{
433 $reg = array();
434 // Convert date with format DD/MM/YYY HH:MM:SS. This part of code should not be used.
435 if (preg_match('/^([0-9]+)\/([0-9]+)\/([0-9]+)\s?([0-9]+)?:?([0-9]+)?:?([0-9]+)?/i', $string, $reg)) {
436 dol_syslog("dol_stringtotime call to function with deprecated parameter format", LOG_WARNING);
437 // Date est au format 'DD/MM/YY' ou 'DD/MM/YY HH:MM:SS'
438 // Date est au format 'DD/MM/YYYY' ou 'DD/MM/YYYY HH:MM:SS'
439 $sday = (int) $reg[1];
440 $smonth = (int) $reg[2];
441 $syear = (int) $reg[3];
442 $shour = (int) $reg[4];
443 $smin = (int) $reg[5];
444 $ssec = (int) $reg[6];
445 if ($syear < 50) {
446 $syear += 1900;
447 }
448 if ($syear >= 50 && $syear < 100) {
449 $syear += 2000;
450 }
451 $string = sprintf("%04d%02d%02d%02d%02d%02d", $syear, $smonth, $sday, $shour, $smin, $ssec);
452 } 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)
453 || 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
454 || 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
455 ) {
456 $syear = (int) $reg[1];
457 $smonth = (int) $reg[2];
458 $sday = (int) $reg[3];
459 $shour = (int) $reg[4];
460 $smin = (int) $reg[5];
461 $ssec = (int) $reg[6];
462 $string = sprintf("%04d%02d%02d%02d%02d%02d", $syear, $smonth, $sday, $shour, $smin, $ssec);
463 }
464
465 $string = preg_replace('/([^0-9])/i', '', $string);
466 $tmp = $string.'000000';
467 // Clean $gm
468 if ($gm === 1) {
469 $gm = 'gmt';
470 } elseif (empty($gm) || $gm === 'tzserver') {
471 $gm = 'tzserver';
472 }
473
474 $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);
475
476 return $date;
477}
478
479
488function dol_get_prev_day($day, $month, $year)
489{
490 $time = dol_mktime(12, 0, 0, $month, $day, $year, 1, 0);
491 $time -= 24 * 60 * 60;
492 $tmparray = dol_getdate($time, true);
493 return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
494}
495
504function dol_get_next_day($day, $month, $year)
505{
506 $time = dol_mktime(12, 0, 0, $month, $day, $year, 1, 0);
507 $time += 24 * 60 * 60;
508 $tmparray = dol_getdate($time, true);
509 return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
510}
511
519function dol_get_prev_month($month, $year)
520{
521 if ($month == 1) {
522 $prev_month = 12;
523 $prev_year = $year - 1;
524 } else {
525 $prev_month = $month - 1;
526 $prev_year = $year;
527 }
528 return array('year' => $prev_year, 'month' => $prev_month);
529}
530
538function dol_get_next_month($month, $year)
539{
540 if ($month == 12) {
541 $next_month = 1;
542 $next_year = $year + 1;
543 } else {
544 $next_month = $month + 1;
545 $next_year = $year;
546 }
547 return array('year' => $next_year, 'month' => $next_month);
548}
549
559function dol_get_prev_week($day, $week, $month, $year)
560{
561 $tmparray = dol_get_first_day_week($day, $month, $year);
562
563 $time = dol_mktime(12, 0, 0, $month, $tmparray['first_day'], $year, 1, 0);
564 $time -= 24 * 60 * 60 * 7;
565 $tmparray = dol_getdate($time, true);
566 return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
567}
568
578function dol_get_next_week($day, $week, $month, $year)
579{
580 $tmparray = dol_get_first_day_week($day, $month, $year);
581
582 $time = dol_mktime(12, 0, 0, $tmparray['first_month'], $tmparray['first_day'], $tmparray['first_year'], 1, 0);
583 $time += 24 * 60 * 60 * 7;
584 $tmparray = dol_getdate($time, true);
585
586 return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
587}
588
600function dol_get_first_day($year, $month = 1, $gm = false)
601{
602 if ($year > 9999) {
603 return '';
604 }
605 return dol_mktime(0, 0, 0, $month, 1, $year, $gm);
606}
607
608
619function dol_get_last_day($year, $month = 12, $gm = false)
620{
621 if ($year > 9999) {
622 return '';
623 }
624 if ($month == 12) {
625 $month = 1;
626 $year += 1;
627 } else {
628 $month += 1;
629 }
630
631 // On se deplace au debut du mois suivant, et on retire un jour
632 $datelim = dol_mktime(23, 59, 59, $month, 1, $year, $gm);
633 $datelim -= (3600 * 24);
634
635 return $datelim;
636}
637
646function dol_get_last_hour($date, $gm = 'tzserver')
647{
648 $tmparray = dol_getdate($date, false, ($gm == 'gmt' ? 'gmt' : ''));
649 return dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year'], $gm);
650}
651
660function dol_get_first_hour($date, $gm = 'tzserver')
661{
662 $tmparray = dol_getdate($date, false, ($gm == 'gmt' ? 'gmt' : ''));
663 return dol_mktime(0, 0, 0, $tmparray['mon'], $tmparray['mday'], $tmparray['year'], $gm);
664}
665
675function dol_get_first_day_week($day, $month, $year, $gm = false)
676{
677 global $conf;
678
679 //$day=2; $month=2; $year=2015;
680 $date = dol_mktime(0, 0, 0, $month, $day, $year, $gm);
681
682 //Checking conf of start week
683 $start_week = (isset($conf->global->MAIN_START_WEEK) ? $conf->global->MAIN_START_WEEK : 1);
684
685 $tmparray = dol_getdate($date, true); // detail of current day
686
687 //Calculate days = offset from current day
688 $days = $start_week - $tmparray['wday'];
689 if ($days >= 1) {
690 $days = 7 - $days;
691 }
692 $days = abs($days);
693 $seconds = $days * 24 * 60 * 60;
694 //print 'start_week='.$start_week.' tmparray[wday]='.$tmparray['wday'].' day offset='.$days.' seconds offset='.$seconds.'<br>';
695
696 //Get first day of week
697 $tmpdaytms = (int) date((string) $tmparray['0']) - $seconds; // $tmparray[0] is day of parameters
698 $tmpday = idate("d", $tmpdaytms);
699
700 //Check first day of week is in same month than current day or not
701 if ($tmpday > $day) {
702 $prev_month = $month - 1;
703 $prev_year = $year;
704
705 if ($prev_month == 0) {
706 $prev_month = 12;
707 $prev_year = $year - 1;
708 }
709 } else {
710 $prev_month = $month;
711 $prev_year = $year;
712 }
713 $tmpmonth = $prev_month;
714 $tmpyear = $prev_year;
715
716 //Get first day of next week
717 $tmptime = dol_mktime(12, 0, 0, $month, $tmpday, $year, 1, 0);
718 $tmptime -= 24 * 60 * 60 * 7;
719 $tmparray = dol_getdate($tmptime, true);
720 $prev_day = $tmparray['mday'];
721
722 //Check prev day of week is in same month than first day or not
723 if ($prev_day > $tmpday) {
724 $prev_month = $month - 1;
725 $prev_year = $year;
726
727 if ($prev_month == 0) {
728 $prev_month = 12;
729 $prev_year = $year - 1;
730 }
731 }
732
733 $week = date("W", dol_mktime(0, 0, 0, $tmpmonth, $tmpday, $tmpyear, $gm));
734
735 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);
736}
737
745function getGMTEasterDatetime($year)
746{
747 $base = new DateTime("$year-03-21", new DateTimeZone("UTC"));
748 $days = easter_days($year); // Return number of days between 21 march and easter day.
749 $tmp = $base->add(new DateInterval("P{$days}D"));
750 return $tmp->getTimestamp();
751}
752
769function num_public_holiday($timestampStart, $timestampEnd, $country_code = '', $lastday = 0, $includesaturday = -1, $includesunday = -1, $includefriday = -1, $includemonday = -1)
770{
771 global $conf, $db, $mysoc;
772
773 $nbFerie = 0;
774
775 // Check to ensure we use correct parameters
776 if (($timestampEnd - $timestampStart) % 86400 != 0) {
777 return 'Error Dates must use same hours and must be GMT dates';
778 }
779
780 if (empty($country_code)) {
781 $country_code = $mysoc->country_code;
782 }
783 if ($includemonday < 0) {
784 $includemonday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_MONDAY', 0);
785 }
786 if ($includefriday < 0) {
787 $includefriday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_FRIDAY', 0);
788 }
789 if ($includesaturday < 0) {
790 $includesaturday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY', 1);
791 }
792 if ($includesunday < 0) {
793 $includesunday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY', 1);
794 }
795
796 $country_id = dol_getIdFromCode($db, $country_code, 'c_country', 'code', 'rowid');
797
798 if (empty($conf->cache['arrayOfActivePublicHolidays_'.$country_id])) {
799 // Loop on public holiday defined into hrm_public_holiday for the day, month and year analyzed
800 $tmpArrayOfPublicHolidays = array();
801 $sql = "SELECT id, code, entity, fk_country, dayrule, year, month, day, active";
802 $sql .= " FROM ".MAIN_DB_PREFIX."c_hrm_public_holiday";
803 $sql .= " WHERE active = 1 and fk_country IN (0".($country_id > 0 ? ", ".$country_id : 0).")";
804 $sql .= " AND entity IN (0," .getEntity('holiday') .")";
805
806 $resql = $db->query($sql);
807 if ($resql) {
808 $num_rows = $db->num_rows($resql);
809 $i = 0;
810 while ($i < $num_rows) {
811 $obj = $db->fetch_object($resql);
812 $tmpArrayOfPublicHolidays[$obj->id] = array('dayrule' => $obj->dayrule, 'year' => $obj->year, 'month' => $obj->month, 'day' => $obj->day);
813 $i++;
814 }
815 } else {
816 dol_syslog($db->lasterror(), LOG_ERR);
817 return 'Error sql '.$db->lasterror();
818 }
819
820 //var_dump($tmpArrayOfPublicHolidays);
821 $conf->cache['arrayOfActivePublicHolidays_'.$country_id] = $tmpArrayOfPublicHolidays;
822 }
823
824 $arrayOfPublicHolidays = $conf->cache['arrayOfActivePublicHolidays_'.$country_id];
825
826 $i = 0;
827 while ((($lastday == 0 && $timestampStart < $timestampEnd) || ($lastday && $timestampStart <= $timestampEnd))
828 && ($i < 50000)) { // Loop end when equals (Test on i is a security loop to avoid infinite loop)
829 $ferie = false;
830 $specialdayrule = array();
831
832 $jour = (int) gmdate("d", $timestampStart);
833 $mois = (int) gmdate("m", $timestampStart);
834 $annee = (int) gmdate("Y", $timestampStart);
835 $jour_semaine = (int) gmdate("w", $timestampStart); // sunday = 0, monday = 1, ...
836
837 //print "jour=".$jour." month=".$mois." year=".$annee." includesaturday=".$includesaturday." includesunday=".$includesunday."\n";
838 foreach ($arrayOfPublicHolidays as $entrypublicholiday) {
839 if (!empty($entrypublicholiday['dayrule']) && $entrypublicholiday['dayrule'] != 'date') { // For example 'easter', '...'
840 $specialdayrule[$entrypublicholiday['dayrule']] = $entrypublicholiday['dayrule'];
841 } else {
842 $match = 1;
843 if (!empty($entrypublicholiday['year']) && $entrypublicholiday['year'] != $annee) {
844 $match = 0;
845 }
846 if ($entrypublicholiday['month'] != $mois) {
847 $match = 0;
848 }
849 if ($entrypublicholiday['day'] != $jour) {
850 $match = 0;
851 }
852
853 if ($match) {
854 $ferie = true;
855 }
856 }
857
858 $i++;
859 }
860 //var_dump($specialdayrule)."\n";
861 //print "ferie=".$ferie."\n";
862
863 if (!$ferie) {
864 // Special dayrules
865 if (in_array('easter', $specialdayrule)) {
866 // Calculation for easter date
867 $date_paques = getGMTEasterDatetime($annee);
868 $jour_paques = gmdate("d", $date_paques);
869 $mois_paques = gmdate("m", $date_paques);
870 if ($jour_paques == $jour && $mois_paques == $mois) {
871 $ferie = true;
872 }
873 // Easter (sunday)
874 }
875
876 if (in_array('eastermonday', $specialdayrule)) {
877 // Calculation for the monday of easter date
878 $date_paques = getGMTEasterDatetime($annee);
879 //print 'PPP'.$date_paques.' '.dol_print_date($date_paques, 'dayhour', 'gmt')." ";
880 $date_lundi_paques = $date_paques + (3600 * 24);
881 $jour_lundi_paques = gmdate("d", $date_lundi_paques);
882 $mois_lundi_paques = gmdate("m", $date_lundi_paques);
883 if ($jour_lundi_paques == $jour && $mois_lundi_paques == $mois) {
884 $ferie = true;
885 }
886 // Easter (monday)
887 //print 'annee='.$annee.' $jour='.$jour.' $mois='.$mois.' $jour_lundi_paques='.$jour_lundi_paques.' $mois_lundi_paques='.$mois_lundi_paques."\n";
888 }
889
890 //Good Friday
891 if (in_array('goodfriday', $specialdayrule)) {
892 // Pulls the date of Easter
893 $easter = getGMTEasterDatetime($annee);
894
895 // Calculates the date of Good Friday based on Easter
896 $date_good_friday = $easter - (2 * 3600 * 24);
897 $dom_good_friday = gmdate("d", $date_good_friday);
898 $month_good_friday = gmdate("m", $date_good_friday);
899
900 if ($dom_good_friday == $jour && $month_good_friday == $mois) {
901 $ferie = true;
902 }
903 }
904
905 if (in_array('ascension', $specialdayrule)) {
906 // Calcul du jour de l'ascension (39 days after easter day)
907 $date_paques = getGMTEasterDatetime($annee);
908 $date_ascension = $date_paques + (3600 * 24 * 39);
909 $jour_ascension = gmdate("d", $date_ascension);
910 $mois_ascension = gmdate("m", $date_ascension);
911 if ($jour_ascension == $jour && $mois_ascension == $mois) {
912 $ferie = true;
913 }
914 // Ascension (thursday)
915 }
916
917 if (in_array('pentecost', $specialdayrule)) {
918 // Calculation of "Pentecote" (49 days after easter day)
919 $date_paques = getGMTEasterDatetime($annee);
920 $date_pentecote = $date_paques + (3600 * 24 * 49);
921 $jour_pentecote = gmdate("d", $date_pentecote);
922 $mois_pentecote = gmdate("m", $date_pentecote);
923 if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
924 $ferie = true;
925 }
926 // "Pentecote" (sunday)
927 }
928 if (in_array('pentecotemonday', $specialdayrule)) {
929 // Calculation of "Pentecote" (49 days after easter day)
930 $date_paques = getGMTEasterDatetime($annee);
931 $date_pentecote = $date_paques + (3600 * 24 * 50);
932 $jour_pentecote = gmdate("d", $date_pentecote);
933 $mois_pentecote = gmdate("m", $date_pentecote);
934 if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
935 $ferie = true;
936 }
937 // "Pentecote" (monday)
938 }
939
940 if (in_array('viernessanto', $specialdayrule)) {
941 // Viernes Santo
942 $date_paques = getGMTEasterDatetime($annee);
943 $date_viernes = $date_paques - (3600 * 24 * 2);
944 $jour_viernes = gmdate("d", $date_viernes);
945 $mois_viernes = gmdate("m", $date_viernes);
946 if ($jour_viernes == $jour && $mois_viernes == $mois) {
947 $ferie = true;
948 }
949 //Viernes Santo
950 }
951
952 if (in_array('fronleichnam', $specialdayrule)) {
953 // Fronleichnam (60 days after easter sunday)
954 $date_paques = getGMTEasterDatetime($annee);
955 $date_fronleichnam = $date_paques + (3600 * 24 * 60);
956 $jour_fronleichnam = gmdate("d", $date_fronleichnam);
957 $mois_fronleichnam = gmdate("m", $date_fronleichnam);
958 if ($jour_fronleichnam == $jour && $mois_fronleichnam == $mois) {
959 $ferie = true;
960 }
961 // Fronleichnam
962 }
963
964 if (in_array('genevafast', $specialdayrule)) {
965 // Geneva fast in Switzerland (Thursday after the first sunday in September)
966 $date_1sunsept = strtotime('next thursday', strtotime('next sunday', mktime(0, 0, 0, 9, 1, $annee)));
967 $jour_1sunsept = date("d", $date_1sunsept);
968 $mois_1sunsept = date("m", $date_1sunsept);
969 if ($jour_1sunsept == $jour && $mois_1sunsept == $mois) {
970 $ferie = true;
971 }
972 // Geneva fast in Switzerland
973 }
974 }
975 //print "ferie=".$ferie."\n";
976
977 // If we have to include Friday, Saturday and Sunday
978 if (!$ferie) {
979 if ($includefriday || $includesaturday || $includesunday || $includemonday) {
980 //Monday (1), Friday (5), Saturday (6) and Sunday (0)
981 if ($includefriday && $jour_semaine == 5) {
982 $ferie = true;
983 }
984 if ($includesaturday && $jour_semaine == 6) {
985 $ferie = true;
986 }
987 if ($includesunday && $jour_semaine == 0) {
988 $ferie = true;
989 }
990 if ($includemonday && $jour_semaine == 1) {
991 $ferie = true;
992 }
993 }
994 }
995 //print "ferie=".$ferie."\n";
996
997 // We increase the counter of non working day
998 if ($ferie) {
999 $nbFerie++;
1000 }
1001
1002 // Increase number of days (on go up into loop)
1003 $timestampStart = dol_time_plus_duree($timestampStart, 1, 'd');
1004 //var_dump($jour.' '.$mois.' '.$annee.' '.$timestampStart);
1005
1006 $i++;
1007 }
1008
1009 //print "nbFerie=".$nbFerie."\n";
1010 return $nbFerie;
1011}
1012
1029function listPublicHoliday($timestampStart, $timestampEnd, $country_code = '', $lastday = 0, $excludesaturday = -1, $excludesunday = -1, $excludefriday = -1, $excludemonday = -1)
1030{
1031 global $conf, $db, $mysoc;
1032
1033 // Check to ensure we use correct parameters
1034 if (($timestampEnd - $timestampStart) % 86400 != 0) {
1035 return 'Error Dates must use same hours and must be GMT dates';
1036 }
1037
1038 if (empty($country_code)) {
1039 $country_code = $mysoc->country_code;
1040 }
1041 if ($excludemonday < 0) {
1042 $excludemonday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_MONDAY', 0);
1043 }
1044 if ($excludefriday < 0) {
1045 $excludefriday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_FRIDAY', 0);
1046 }
1047 if ($excludesaturday < 0) {
1048 $excludesaturday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY', 1);
1049 }
1050 if ($excludesunday < 0) {
1051 $excludesunday = getDolGlobalInt('MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY', 1);
1052 }
1053
1054 $country_id = dol_getIdFromCode($db, $country_code, 'c_country', 'code', 'rowid');
1055
1056 if (empty($conf->cache['arrayOfActivePublicHolidays_' . $country_id])) {
1057 // Loop on public holiday defined into hrm_public_holiday for the day, month and year analyzed
1058 $tmpArrayOfPublicHolidays = array();
1059 $sql = "SELECT id, code, entity, fk_country, dayrule, year, month, day, active";
1060 $sql .= " FROM " . MAIN_DB_PREFIX . "c_hrm_public_holiday";
1061 $sql .= " WHERE active = 1 and fk_country IN (0" . ($country_id > 0 ? ", " . $country_id : 0) . ")";
1062 $sql .= " AND entity IN (0," . getEntity('holiday') . ")";
1063
1064 $resql = $db->query($sql);
1065 if ($resql) {
1066 $num_rows = $db->num_rows($resql);
1067 $i = 0;
1068 while ($i < $num_rows) {
1069 $obj = $db->fetch_object($resql);
1070 $tmpArrayOfPublicHolidays[$obj->id] = array('dayrule' => $obj->dayrule, 'year' => $obj->year, 'month' => $obj->month, 'day' => $obj->day);
1071 $i++;
1072 }
1073 } else {
1074 dol_syslog($db->lasterror(), LOG_ERR);
1075 return 'Error sql ' . $db->lasterror();
1076 }
1077
1078 //var_dump($tmpArrayOfPublicHolidays);
1079 $conf->cache['arrayOfActivePublicHolidays_' . $country_id] = $tmpArrayOfPublicHolidays;
1080 }
1081
1082 $arrayOfPublicHolidays = $conf->cache['arrayOfActivePublicHolidays_' . $country_id];
1083 $listFeries = [];
1084 $i = 0;
1085 while ((($lastday == 0 && $timestampStart < $timestampEnd) || ($lastday && $timestampStart <= $timestampEnd))
1086 && ($i < 50000)) { // Loop end when equals (Test on i is a security loop to avoid infinite loop)
1087 $nonWorkingDay = false;
1088 $ferie = false;
1089 $specialdayrule = array();
1090
1091 $jour = (int) gmdate("d", $timestampStart);
1092 $mois = (int) gmdate("m", $timestampStart);
1093 $annee = (int) gmdate("Y", $timestampStart);
1094
1095 // If we have to exclude Friday, Saturday and Sunday
1096 if ($excludefriday || $excludesaturday || $excludesunday) {
1097 $jour_julien = unixtojd($timestampStart);
1098 $jour_semaine = jddayofweek($jour_julien, 0);
1099 if ($excludefriday) { //Friday (5), Saturday (6) and Sunday (0)
1100 if ($jour_semaine == 5) {
1101 $nonWorkingDay = true;
1102 }
1103 }
1104 if ($excludesaturday) { //Friday (5), Saturday (6) and Sunday (0)
1105 if ($jour_semaine == 6) {
1106 $nonWorkingDay = true;
1107 }
1108 }
1109 if ($excludesunday) { //Friday (5), Saturday (6) and Sunday (0)
1110 if ($jour_semaine == 0) {
1111 $nonWorkingDay = true;
1112 }
1113 }
1114 }
1115 //print "ferie=".$nonWorkingDay."\n";
1116
1117 if (!$nonWorkingDay) {
1118 //print "jour=".$jour." month=".$mois." year=".$annee." includesaturday=".$excludesaturday." includesunday=".$excludesunday."\n";
1119 foreach ($arrayOfPublicHolidays as $entrypublicholiday) {
1120 if (!empty($entrypublicholiday['dayrule']) && $entrypublicholiday['dayrule'] != 'date') { // For example 'easter', '...'
1121 $specialdayrule[$entrypublicholiday['dayrule']] = $entrypublicholiday['dayrule'];
1122 } else {
1123 $match = 1;
1124 if (!empty($entrypublicholiday['year']) && $entrypublicholiday['year'] != $annee) {
1125 $match = 0;
1126 }
1127 if ($entrypublicholiday['month'] != $mois) {
1128 $match = 0;
1129 }
1130 if ($entrypublicholiday['day'] != $jour) {
1131 $match = 0;
1132 }
1133
1134 if ($match) {
1135 $ferie = true;
1136 $listFeries[] = $timestampStart;
1137 }
1138 }
1139
1140 $i++;
1141 }
1142 //var_dump($specialdayrule)."\n";
1143 //print "ferie=".$nonWorkingDay."\n";
1144 }
1145
1146 if (!$nonWorkingDay && !$ferie) {
1147 // Special dayrules
1148 if (in_array('easter', $specialdayrule)) {
1149 // Calculation for easter date
1150 $date_paques = getGMTEasterDatetime($annee);
1151 $jour_paques = gmdate("d", $date_paques);
1152 $mois_paques = gmdate("m", $date_paques);
1153 if ($jour_paques == $jour && $mois_paques == $mois) {
1154 $ferie = true;
1155 $listFeries[] = $timestampStart;
1156 }
1157 // Easter (sunday)
1158 }
1159
1160 if (in_array('eastermonday', $specialdayrule)) {
1161 // Calculation for the monday of easter date
1162 $date_paques = getGMTEasterDatetime($annee);
1163 //print 'PPP'.$date_paques.' '.dol_print_date($date_paques, 'dayhour', 'gmt')." ";
1164 $date_lundi_paques = $date_paques + (3600 * 24);
1165 $jour_lundi_paques = gmdate("d", $date_lundi_paques);
1166 $mois_lundi_paques = gmdate("m", $date_lundi_paques);
1167 if ($jour_lundi_paques == $jour && $mois_lundi_paques == $mois) {
1168 $ferie = true;
1169 $listFeries[] = $timestampStart;
1170 }
1171 // Easter (monday)
1172 //print 'annee='.$annee.' $jour='.$jour.' $mois='.$mois.' $jour_lundi_paques='.$jour_lundi_paques.' $mois_lundi_paques='.$mois_lundi_paques."\n";
1173 }
1174
1175 //Good Friday
1176 if (in_array('goodfriday', $specialdayrule)) {
1177 // Pulls the date of Easter
1178 $easter = getGMTEasterDatetime($annee);
1179
1180 // Calculates the date of Good Friday based on Easter
1181 $date_good_friday = $easter - (2 * 3600 * 24);
1182 $dom_good_friday = gmdate("d", $date_good_friday);
1183 $month_good_friday = gmdate("m", $date_good_friday);
1184
1185 if ($dom_good_friday == $jour && $month_good_friday == $mois) {
1186 $ferie = true;
1187 $listFeries[] = $timestampStart;
1188 }
1189 }
1190
1191 if (in_array('ascension', $specialdayrule)) {
1192 // Calcul du jour de l'ascension (39 days after easter day)
1193 $date_paques = getGMTEasterDatetime($annee);
1194 $date_ascension = $date_paques + (3600 * 24 * 39);
1195 $jour_ascension = gmdate("d", $date_ascension);
1196 $mois_ascension = gmdate("m", $date_ascension);
1197 if ($jour_ascension == $jour && $mois_ascension == $mois) {
1198 $ferie = true;
1199 $listFeries[] = $timestampStart;
1200 }
1201 // Ascension (thursday)
1202 }
1203
1204 if (in_array('pentecost', $specialdayrule)) {
1205 // Calculation of "Pentecote" (49 days after easter day)
1206 $date_paques = getGMTEasterDatetime($annee);
1207 $date_pentecote = $date_paques + (3600 * 24 * 49);
1208 $jour_pentecote = gmdate("d", $date_pentecote);
1209 $mois_pentecote = gmdate("m", $date_pentecote);
1210 if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
1211 $ferie = true;
1212 $listFeries[] = $timestampStart;
1213 }
1214 // "Pentecote" (sunday)
1215 }
1216
1217 if (in_array('pentecotemonday', $specialdayrule)) {
1218 // Calculation of "Pentecote" (49 days after easter day)
1219 $date_paques = getGMTEasterDatetime($annee);
1220 $date_pentecote = $date_paques + (3600 * 24 * 50);
1221 $jour_pentecote = gmdate("d", $date_pentecote);
1222 $mois_pentecote = gmdate("m", $date_pentecote);
1223 if ($jour_pentecote == $jour && $mois_pentecote == $mois) {
1224 $ferie = true;
1225 $listFeries[] = $timestampStart;
1226 }
1227 // "Pentecote" (monday)
1228 }
1229
1230 if (in_array('viernessanto', $specialdayrule)) {
1231 // Viernes Santo
1232 $date_paques = getGMTEasterDatetime($annee);
1233 $date_viernes = $date_paques - (3600 * 24 * 2);
1234 $jour_viernes = gmdate("d", $date_viernes);
1235 $mois_viernes = gmdate("m", $date_viernes);
1236 if ($jour_viernes == $jour && $mois_viernes == $mois) {
1237 $ferie = true;
1238 $listFeries[] = $timestampStart;
1239 }
1240 //Viernes Santo
1241 }
1242
1243 if (in_array('fronleichnam', $specialdayrule)) {
1244 // Fronleichnam (60 days after easter sunday)
1245 $date_paques = getGMTEasterDatetime($annee);
1246 $date_fronleichnam = $date_paques + (3600 * 24 * 60);
1247 $jour_fronleichnam = gmdate("d", $date_fronleichnam);
1248 $mois_fronleichnam = gmdate("m", $date_fronleichnam);
1249 if ($jour_fronleichnam == $jour && $mois_fronleichnam == $mois) {
1250 $ferie = true;
1251 $listFeries[] = $timestampStart;
1252 }
1253 // Fronleichnam
1254 }
1255
1256 if (in_array('genevafast', $specialdayrule)) {
1257 // Geneva fast in Switzerland (Thursday after the first sunday in September)
1258 $date_1sunsept = strtotime('next thursday', strtotime('next sunday', mktime(0, 0, 0, 9, 1, $annee)));
1259 $jour_1sunsept = date("d", $date_1sunsept);
1260 $mois_1sunsept = date("m", $date_1sunsept);
1261 if ($jour_1sunsept == $jour && $mois_1sunsept == $mois) {
1262 $ferie = true;
1263 $listFeries[] = $timestampStart;
1264 }
1265 // Geneva fast in Switzerland
1266 }
1267 }
1268 //print "ferie=".$nonWorkingDay."\n";
1269
1270 // Increase number of days (on go up into loop)
1271 $timestampStart = dol_time_plus_duree($timestampStart, 1, 'd');
1272 //var_dump($jour.' '.$mois.' '.$annee.' '.$timestampStart);
1273
1274 $i++;
1275 }
1276
1277 //print "nbFerie=".$nbFerie."\n";
1278 return $listFeries;
1279}
1280
1291function num_between_day($timestampStart, $timestampEnd, $lastday = 0)
1292{
1293 if ($timestampStart <= $timestampEnd) {
1294 if ($lastday == 1) {
1295 $bit = 0;
1296 } else {
1297 $bit = 1;
1298 }
1299 $nbjours = (int) floor(($timestampEnd - $timestampStart) / (60 * 60 * 24)) + 1 - $bit;
1300 } else {
1301 $nbjours = 0;
1302 }
1303 //print ($timestampEnd - $timestampStart) - $lastday;
1304 return $nbjours;
1305}
1306
1319function num_open_day($timestampStart, $timestampEnd, $inhour = 0, $lastday = 0, $halfday = 0, $country_code = '')
1320{
1321 global $langs, $mysoc;
1322
1323 if (empty($country_code)) {
1324 $country_code = $mysoc->country_code;
1325 }
1326
1327 dol_syslog('num_open_day timestampStart='.$timestampStart.' timestampEnd='.$timestampEnd.' bit='.$lastday.' country_code='.$country_code);
1328
1329 // Check parameters
1330 if (!is_int($timestampStart) && !is_float($timestampStart)) {
1331 return 'ErrorBadParameter_num_open_day';
1332 }
1333 if (!is_int($timestampEnd) && !is_float($timestampEnd)) {
1334 return 'ErrorBadParameter_num_open_day';
1335 }
1336
1337 if ($timestampStart < $timestampEnd) {
1338 // --- 1. Calculate Gross Working Days ---
1339 // Gross working days = total days in range - non-working days (weekends & public holidays).
1340 $nbOpenDay = num_between_day($timestampStart, $timestampEnd, $lastday) - num_public_holiday($timestampStart, $timestampEnd, $country_code, $lastday);
1341
1342 // --- 2. Apply Contextual Half-Day Deductions ---
1343 $halfday = (int) $halfday; // Ensure $halfday is an integer for reliable comparisons.
1344
1345 // Check if start/end days are working days just ONCE to optimize performance
1346 // by avoiding redundant calls to the potentially slow num_public_holiday() function.
1347 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1348 $isStartDayWorking = (num_public_holiday($timestampStart, $timestampStart, $country_code, 1) == 0);
1349 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1350 $isEndDayWorking = (num_public_holiday($timestampEnd, $timestampEnd, $country_code, 1) == 0);
1351
1352 // Deduct 0.5 if the leave starts in the afternoon of a working day.
1353 if (($halfday == -1 || $halfday == 2) && $isStartDayWorking) {
1354 $nbOpenDay -= 0.5;
1355 }
1356
1357 // Deduct 0.5 if the leave ends in the morning of a different, working day.
1358 if (($halfday == 1 || $halfday == 2) && date('Y-m-d', $timestampStart) != date('Y-m-d', $timestampEnd) && $isEndDayWorking) {
1359 $nbOpenDay -= 0.5;
1360 }
1361
1362 // --- 3. Return Final Value ---
1363 if ($inhour == 1) {
1364 return $nbOpenDay * 24;
1365 }
1366
1367 return $nbOpenDay;
1368 } elseif ($timestampStart == $timestampEnd) {
1369 $numholidays = 0;
1370 if ($lastday) {
1371 $numholidays = num_public_holiday($timestampStart, $timestampEnd, $country_code, $lastday);
1372 if ($numholidays == 1) {
1373 return 0;
1374 }
1375 }
1376
1377 $nbOpenDay = $lastday;
1378
1379 if ($inhour == 1) {
1380 $nbOpenDay *= 24;
1381 }
1382 return $nbOpenDay - (($inhour == 1 ? 12 : 0.5) * abs($halfday));
1383 } else {
1384 return $langs->trans("Error");
1385 }
1386}
1387
1388
1389
1398function monthArray($outputlangs, $short = 0)
1399{
1400 $montharray = array(
1401 1 => $outputlangs->trans("Month01"),
1402 2 => $outputlangs->trans("Month02"),
1403 3 => $outputlangs->trans("Month03"),
1404 4 => $outputlangs->trans("Month04"),
1405 5 => $outputlangs->trans("Month05"),
1406 6 => $outputlangs->trans("Month06"),
1407 7 => $outputlangs->trans("Month07"),
1408 8 => $outputlangs->trans("Month08"),
1409 9 => $outputlangs->trans("Month09"),
1410 10 => $outputlangs->trans("Month10"),
1411 11 => $outputlangs->trans("Month11"),
1412 12 => $outputlangs->trans("Month12")
1413 );
1414
1415 if (!empty($short)) {
1416 $montharray = array(
1417 1 => $outputlangs->trans("MonthShort01"),
1418 2 => $outputlangs->trans("MonthShort02"),
1419 3 => $outputlangs->trans("MonthShort03"),
1420 4 => $outputlangs->trans("MonthShort04"),
1421 5 => $outputlangs->trans("MonthShort05"),
1422 6 => $outputlangs->trans("MonthShort06"),
1423 7 => $outputlangs->trans("MonthShort07"),
1424 8 => $outputlangs->trans("MonthShort08"),
1425 9 => $outputlangs->trans("MonthShort09"),
1426 10 => $outputlangs->trans("MonthShort10"),
1427 11 => $outputlangs->trans("MonthShort11"),
1428 12 => $outputlangs->trans("MonthShort12")
1429 );
1430 }
1431
1432 return $montharray;
1433}
1434
1442function getWeekNumbersOfMonth($month, $year)
1443{
1444 $nb_days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
1445 $TWeek = array();
1446 for ($day = 1; $day < $nb_days; $day++) {
1447 $week_number = getWeekNumber($day, $month, $year);
1448 $TWeek[$week_number] = $week_number;
1449 }
1450 return $TWeek;
1451}
1452
1460function getFirstDayOfEachWeek($TWeek, $year)
1461{
1462 $TFirstDayOfWeek = array();
1463 foreach ($TWeek as $weekNb) {
1464 if (in_array('01', $TWeek) && in_array('52', $TWeek) && $weekNb == '01') {
1465 $year++; //Si on a la 1re semaine et la semaine 52 c'est qu'on change d'année
1466 }
1467 $TFirstDayOfWeek[$weekNb] = date('d', strtotime($year.'W'.$weekNb));
1468 }
1469 return $TFirstDayOfWeek;
1470}
1471
1479function getLastDayOfEachWeek($TWeek, $year)
1480{
1481 $TLastDayOfWeek = array();
1482 foreach ($TWeek as $weekNb) {
1483 $TLastDayOfWeek[$weekNb] = date('d', strtotime($year.'W'.$weekNb.'+6 days'));
1484 }
1485 return $TLastDayOfWeek;
1486}
1487
1496function getWeekNumber($day, $month, $year)
1497{
1498 $date = new DateTime($year.'-'.$month.'-'.$day);
1499 $week = $date->format("W");
1500 return $week;
1501}
dol_get_prev_month($month, $year)
Return previous month.
Definition date.lib.php:519
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:660
dol_get_next_day($day, $month, $year)
Return next day.
Definition date.lib.php:504
dol_get_next_week($day, $week, $month, $year)
Return next week.
Definition date.lib.php:578
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:382
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:646
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:675
getGMTEasterDatetime($year)
Return the easter day in GMT time.
Definition date.lib.php:745
convertDurationtoHour($duration_value, $duration_unit)
Convert duration to hour.
Definition date.lib.php:338
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:488
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:600
getFirstDayOfEachWeek($TWeek, $year)
Return array of first day of weeks.
dol_get_next_month($month, $year)
Return next month.
Definition date.lib.php:538
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...
convertTime2Seconds($iHours=0, $iMinutes=0, $iSeconds=0)
Convert hours and minutes into seconds.
Definition date.lib.php:215
num_open_day($timestampStart, $timestampEnd, $inhour=0, $lastday=0, $halfday=0, $country_code='')
Function to return number of working days (and text of units) between two dates (working days)
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:125
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:244
dol_get_prev_week($day, $week, $month, $year)
Return previous week.
Definition date.lib.php:559
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:431
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:619
monthArray($outputlangs, $short=0)
Return array of translated months or selected month.
listPublicHoliday($timestampStart, $timestampEnd, $country_code='', $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 ...
num_public_holiday($timestampStart, $timestampEnd, $country_code='', $lastday=0, $includesaturday=-1, $includesunday=-1, $includefriday=-1, $includemonday=-1)
Return the number of non working days including Friday, Saturday and Sunday (or not) between 2 dates ...
Definition date.lib.php:769
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.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79