JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
upload.php cleanup: really don't make dot files
[wfpl.git] / time.php
1 <?php
2
3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
7
8
9 # This file contains functions to manipulate/calculate dates/times
10
11 # FIXME make it so you can call this with a string YYYY-MM-DD
12 function ymd_to_days($year, $month, $day) {
13         return (int)(mktime(12,0,0,$month,$day, $year, 0) / 86400);
14 }
15
16 function days_to_ymd($days) {
17         return explode('-', date('Y-n-j', $days * 86400 + 43200));
18 }
19
20 function days_to_weekday_name($days) {
21         $day_names = array('Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday');
22         return $day_names[$days % 7];
23 }
24
25
26 #function time_test() {
27 #       for($i = 0; $i < 41 * 86400; $i += 86399) {
28 #               echo "seconds: $i";
29 #               $days = (int)($i / 86400);
30 #               list($year, $month, $day) = days_to_ymd($days);
31 #               echo ", days_to_ymd($days): $year-$month-$day";
32 #               $days = ymd_to_days($year, $month, $day);
33 #               echo ", ymd_to_days($year, $month, $day): $days\n<br>";
34 #       }
35 #}
36
37
38 # pass anything, and get a valid date
39 #
40 # returns array($year, $month, $day)
41 function clean_ymd($year, $month, $day) {
42         $year = intval($year, 10);
43         $month = intval($month, 10);
44         $day = intval($day, 10);
45
46         if($year < 100) {
47                 $year += 2000;
48         }
49         if($month < 1) {
50                 $month = 1;
51         } elseif($month > 12) {
52                 $month = 12;
53         }
54         if($day < 1) {
55                 $day = 1;
56         } else {
57                 $max = date('t', mktime(12, 0, 0, $month, 1, $year));
58                 if($day > $max) {
59                         $day = $max;
60                 }
61         }
62
63         return array($year, $month, $day);
64 }
65
66 # pass date like 3/21/99
67 # returns array(year, month, day)
68 function mdy_clean($date) {
69         $date = preg_replace('|[^0-9/-]|', '', $date);
70         $date = preg_replace('|-|', '/', $date);
71         $parts = explode('/', $date);
72         switch(count($parts)) {
73                 case 1:
74                         $year = $parts[0];
75                         if(strlen($year) == 0) {
76                                 list($month, $day, $year) = explode('/', date('m/d/Y'));
77                         } else {
78                                 list($month, $day) = explode('/', date('m/d'));
79                         }
80                 break;
81                 case 2:
82                         list($month, $year) = $parts;
83                         $year = date('d');
84                 break;
85                 default:
86                         list($month, $day, $year) = $parts;
87         }
88
89         return clean_ymd($year, $month, $day);
90 }
91
92 # convert date string from mm/dd/yyyy to yyyy-mm-dd
93 function mdy_to_ymd($date) {
94         list($year, $month, $day) = mdy_clean($date);
95         return sprintf('%04u-%02u-%02u', $year, $month, $day);
96 }
97
98 # pass date like 2008-11-21
99 # returns array(year, month, day)
100 function ymd_clean($date) {
101         $date = preg_replace('|[^0-9/-]|', '', $date);
102         $date = preg_replace('|/|', '-', $date);
103         $parts = explode('-', $date);
104         switch(count($parts)) {
105                 case 1:
106                         $year = $parts[0];
107                         if(strlen($year) == 0 || $year < 1971 || $year > 2050) {
108                                 list($year, $month, $day) = explode('-', date('Y-m-d'));
109                         } else {
110                                 list($month, $day) = explode('-', date('m-d'));
111                         }
112                 break;
113                 case 2:
114                         list($year, $month) = $parts;
115                         $year = date('d');
116                 break;
117                 default:
118                         list($year, $month, $day) = $parts;
119         }
120
121         return clean_ymd($year, $month, $day);
122 }
123
124 # convert date string from yyyy-mm-dd to mm/dd/yyyy
125 function ymd_to_mdy($str) {
126         list($year, $month, $day) = ymd_clean($str);
127         return sprintf('%02u/%02u/%04u', $month, $day, $year);
128 }