1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: /**
15: * Various utilities for working with date/time.
16: *
17: * @category Jyxo
18: * @package Jyxo_Time
19: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
20: * @license https://github.com/jyxo/php/blob/master/license.txt
21: * @author Jakub Tománek
22: */
23: class Jyxo_Time_Util
24: {
25: /**
26: * List of Czech public holidays.
27: *
28: * @var array
29: */
30: private static $holidays = array(
31: '1.1',
32: '1.5',
33: '8.5',
34: '5.7',
35: '6.7',
36: '28.9',
37: '28.10',
38: '17.11',
39: '24.12',
40: '25.12',
41: '26.12'
42: );
43:
44: /**
45: * Returns the next month.
46: *
47: * If the current date is greater than the next month's number of days, returns the next month's last date.
48: * This is different from strtotime('+1 month') behaviour, where August 31st returns October 1st.
49: *
50: * @param integer $now Current date/time
51: * @return Jyxo_Time_Time
52: */
53: public static function nextMonth(Jyxo_Time_Time $now = null)
54: {
55: $now = $now ? $now->unix : time();
56:
57: $nextMonth = date('n', $now) + 1;
58: $thisYear = date('Y', $now);
59: // Actual date vs. next month's number of days
60: $day = min(date('j', $now), date('t', mktime(0, 0, 0, $nextMonth, 1, $thisYear)));
61:
62: // Create the date
63: $date = mktime(date('H', $now), date('i', $now), date('s', $now), $nextMonth, $day, $thisYear);
64: return new Jyxo_Time_Time($date);
65: }
66:
67: /**
68: * Checks if the given date is a working day.
69: *
70: * @param Jyxo_Time_Time $day Date to be checked
71: * @return boolean
72: */
73: public static function isWorkDay(Jyxo_Time_Time $day)
74: {
75: $holidays = self::$holidays;
76:
77: // Adds Easter Monday. easter_date is supposed to be buggy http://cz.php.net/manual/en/function.easter-date.php#80664
78: $year = (int) $day->format('Y');
79: $days = easter_days($year);
80: // $days returns the number of days from March 21st until the Easter Sunday, +1 because of Monday
81: $holidays[] = date('j.n', strtotime($year . '-03-21 +' . ($days + 1) . ' days'));
82:
83: $isWorkDay = true;
84:
85: if ($day->format('N') > 5) {
86: // Saturday or Sunday
87: $isWorkDay = false;
88: } elseif (in_array($day->format('j.n'), $holidays)) {
89: // Public holiday, hurray!
90: $isWorkDay = false;
91: }
92:
93: return $isWorkDay;
94: }
95: }
96: