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