JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix enc_ functions for image/thumb parts
[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 # FIXME make it so you can call this with a string YYYY-MM-DD
22 function ymd_to_days($year, $month, $day) {
23         return (int)(mktime(12,0,0,$month,$day, $year, 0) / 86400);
24 }
25
26 function days_to_ymd($days) {
27         return explode('-', date('Y-n-j', $days * 86400 + 43200));
28 }
29
30 function days_to_weekday_name($days) {
31         $day_names = array('Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday');
32         return $day_names[$days % 7];
33 }
34
35
36 #function time_test() {
37 #       for($i = 0; $i < 41 * 86400; $i += 86399) {
38 #               echo "seconds: $i";
39 #               $days = (int)($i / 86400);
40 #               list($year, $month, $day) = days_to_ymd($days);
41 #               echo ", days_to_ymd($days): $year-$month-$day";
42 #               $days = ymd_to_days($year, $month, $day);
43 #               echo ", ymd_to_days($year, $month, $day): $days\n<br>";
44 #       }
45 #}
46
47
48 # pass anything, and get a valid date
49 #
50 # returns array($year, $month, $day)
51 function clean_ymd($year, $month, $day) {
52         $year = intval($year, 10);
53         $month = intval($month, 10);
54         $day = intval($day, 10);
55
56         if($year < 100) {
57                 $year += 2000;
58         }
59         if($month < 1) {
60                 $month = 1;
61         } elseif($month > 12) {
62                 $month = 12;
63         }
64         if($day < 1) {
65                 $day = 1;
66         } else {
67                 $max = date('t', mktime(12, 0, 0, $month, 1, $year));
68                 if($day > $max) {
69                         $day = $max;
70                 }
71         }
72
73         return array($year, $month, $day);
74 }
75
76 # pass date like 3/21/99
77 # returns array(year, month, day)
78 function mdy_clean($date) {
79         $date = preg_replace('|[^0-9/-]|', '', $date);
80         $date = preg_replace('|-|', '/', $date);
81         $parts = explode('/', $date);
82         switch(count($parts)) {
83                 case 1:
84                         $year = $parts[0];
85                         if(strlen($year) == 0) {
86                                 list($month, $day, $year) = explode('/', date('m/d/Y'));
87                         } else {
88                                 list($month, $day) = explode('/', date('m/d'));
89                         }
90                 break;
91                 case 2:
92                         list($month, $year) = $parts;
93                         $year = date('d');
94                 break;
95                 default:
96                         list($month, $day, $year) = $parts;
97         }
98
99         return clean_ymd($year, $month, $day);
100 }
101
102 # convert date string from mm/dd/yyyy to yyyy-mm-dd
103 function mdy_to_ymd($date) {
104         list($year, $month, $day) = mdy_clean($date);
105         return sprintf('%04u-%02u-%02u', $year, $month, $day);
106 }
107
108 # pass date like 2008-11-21
109 # returns array(year, month, day)
110 function ymd_clean($date) {
111         $date = preg_replace('|[^0-9/-]|', '', $date);
112         $date = preg_replace('|/|', '-', $date);
113         $parts = explode('-', $date);
114         switch(count($parts)) {
115                 case 1:
116                         $year = $parts[0];
117                         if(strlen($year) == 0 || $year < 1971 || $year > 2050) {
118                                 list($year, $month, $day) = explode('-', date('Y-m-d'));
119                         } else {
120                                 list($month, $day) = explode('-', date('m-d'));
121                         }
122                 break;
123                 case 2:
124                         list($year, $month) = $parts;
125                         $year = date('d');
126                 break;
127                 default:
128                         list($year, $month, $day) = $parts;
129         }
130
131         return clean_ymd($year, $month, $day);
132 }
133
134 # convert date string from yyyy-mm-dd to mm/dd/yyyy
135 function ymd_to_mdy($str) {
136         list($year, $month, $day) = ymd_clean($str);
137         return sprintf('%02u/%02u/%04u', $month, $day, $year);
138 }