-
Notifications
You must be signed in to change notification settings - Fork 1
/
dateManager.php
77 lines (58 loc) · 1.77 KB
/
dateManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
Class dateManager {
public $dateArray = array();
public function __construct(){
date_default_timezone_set('America/New_York');
$this->readCSVFile();
}
public function readCSVFile(){
$row = 1;
if (($handle = fopen("data/holidays.csv", "r")) !== false) {
while (($data = fgetcsv($handle, 100, ",", '"')) !== false) {
$num = count($data);
$this->dateArray[strtotime("$data[0]")] = "$data[1]";
//echo $data[0]." ".$data[1]."</br>" ;
}
fclose($handle);
}
//var_dump($this->dateArray);
}
/**
* @param date - the date from which we would like to find the business date 'offset' days away
* date is in integer format
* @param offset - the number of days from 'date' from which we would
* like to find the business dates
* offset can be positive or negative
* @return the business date 'offset' days from 'date'
* (there will be 'offset' business days between 'date' and the return value)
*/
public function getTradeDateOffestFromDate($date, $offset) {
if ($offset < 0){
$increment = -1;
}else{
$increment = 1;
}
$result = $date;
for($daysLeft = $offset; $daysLeft != 0; $daysLeft -= $increment) {
do {
$result = $result + (24 * 60 * 60);
$day = date("D", $result);
} while( $day === "Sat"
|| $day === "Sun"
|| isset($this->dateArray[$result]) );
}
return $result;
}
/**
* @param date in integer format
* @return true if the date is a trading date,
* false otherwise (i.e. Saturday, Sunday, Holiday)
*/
public function isBusinessDay($date) {
$day = date("D", $date);
return !( $day === "Sat"
|| $day === "Sun"
|| isset($this->dateArray[$date]));
}
}
?>