JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
refactored date-mangling stuff a bit
[wfpl.git] / time.php
1 <?php
2
3 #  Copyright (C) 2007 Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #  
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #  
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # This file contains functions to manipulate/calculate dates/times
20
21 function ymd_to_days($year, $month, $day) {
22         return (int)(mktime(12,0,0,$month,$day, $year, 0) / 86400);
23 }
24
25 function days_to_ymd($days) {
26         return explode('-', date('Y-n-j', $days * 86400 + 43200));
27 }
28
29 function days_to_weekday_name($days) {
30         $day_names = array('Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday');
31         return $day_names[$days % 7];
32 }
33
34
35 #function time_test() {
36 #       for($i = 0; $i < 41 * 86400; $i += 86399) {
37 #               echo "seconds: $i";
38 #               $days = (int)($i / 86400);
39 #               list($year, $month, $day) = days_to_ymd($days);
40 #               echo ", days_to_ymd($days): $year-$month-$day";
41 #               $days = ymd_to_days($year, $month, $day);
42 #               echo ", ymd_to_days($year, $month, $day): $days\n<br>";
43 #       }
44 #}
45
46
47 # pass anything, and get a valid date
48 #
49 # returns array($year, $month, $day)
50 function clean_ymd($year, $month, $day) {
51         $year = intval($year, 10);
52         $month = intval($month, 10);
53         $day = intval($day, 10);
54
55         if($year < 100) {
56                 $year += 2000;
57         }
58         if($month < 1) {
59                 $month = 1;
60         } elseif($month > 12) {
61                 $month = 12;
62         }
63         if($day < 1) {
64                 $day = 1;
65         } else {
66                 $max = date('t', mktime(12, 0, 0, $month, 1, $year));
67                 if($day > $max) {
68                         $day = $max;
69                 }
70         }
71
72         return array($year, $month, $day);
73 }
74
75 # pass date like 3/21/99
76 # returns array(year, month, day)
77 function mdy_clean($date) {
78         $date = ereg_replace('[^0-9/-]', '', $date);
79         $date = ereg_replace('-', '/', $date);
80         $parts = explode('/', $date);
81         switch(count($parts)) {
82                 case 1:
83                         $year = $parts[0];
84                         if(strlen($year) == 0) {
85                                 list($month, $day, $year) = explode('/', date('m/d/Y'));
86                         } else {
87                                 list($month, $day) = explode('/', date('m/d'));
88                         }
89                 break;
90                 case 2:
91                         list($month, $year) = $parts;
92                         $year = date('d');
93                 break;
94                 default:
95                         list($month, $day, $year) = $parts;
96         }
97
98         return clean_ymd($year, $month, $day);
99 }
100
101 # convert date string from mm/dd/yyyy to yyyy-mm-dd
102 function mdy_to_ymd($date) {
103         list($year, $month, $day) = mdy_clean($date);
104         return sprintf('%04u-%02u-%02u', $year, $month, $day);
105 }
106
107 # pass date like 2008-11-21
108 # returns array(year, month, day)
109 function ymd_clean($date) {
110         $date = ereg_replace('[^0-9/-]', '', $date);
111         $date = ereg_replace('/', '-', $date);
112         $parts = explode('-', $date);
113         switch(count($parts)) {
114                 case 1:
115                         $year = $parts[0];
116                         if(strlen($year) == 0) {
117                                 list($year, $month, $day) = explode('-', date('Y-m-d'));
118                         } else {
119                                 list($month, $day) = explode('-', date('m-d'));
120                         }
121                 break;
122                 case 2:
123                         list($year, $month) = $parts;
124                         $year = date('d');
125                 break;
126                 default:
127                         list($year, $month, $day) = $parts;
128         }
129
130         return clean_ymd($year, $month, $day);
131 }
132
133 # convert date string from yyyy-mm-dd to mm/dd/yyyy
134 function ymd_to_mdy($str) {
135         list($year, $month, $day) = ymd_clean($str);
136         return sprintf('%02u/%02u/%04u', $month, $day, $year);
137 }
138
139 function enc_mdy($str) {
140         return ymd_to_mdy($str);
141 }
142
143 function format_ymd($str) {
144         list($year, $month, $day) = ymd_clean($str);
145         return sprintf('%04u-%02u-%02u', $year, $month, $day);
146 }