JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
re-licensed under GPLv3
[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         } elseif($day > 31) {
66                 # FIXME this should check the month
67                 $day = 31;
68         }
69
70         return array($year, $month, $day);
71 }
72
73 # convert date string from mm/dd/yyyy to yyyy-mm-dd
74 function mdy_to_ymd($date) {
75         $date = ereg_replace('[^0-9/-]', '', $date);
76         $date = ereg_replace('-', '/', $date);
77         $parts = explode('/', $date);
78         switch(count($parts)) {
79                 case 1:
80                         $year = $parts[0];
81                         if(strlen($year) == 0) {
82                                 list($month, $day, $year) = explode('/', date('m/d/Y'));
83                         } else {
84                                 list($month, $day) = explode('/', date('m/d'));
85                         }
86                 break;
87                 case 2:
88                         list($month, $year) = $parts;
89                         $year = date('d');
90                 break;
91                 default:
92                         list($month, $day, $year) = $parts;
93         }
94
95         list($year, $month, $day) = clean_ymd($year, $month, $day);
96
97         return sprintf('%04u-%02u-%02u', $year, $month, $day);
98 }
99
100 # convert date string from yyy-mm-dd to mm/dd/yyyy
101 function ymd_to_mdy($date) {
102         $date = ereg_replace('[^0-9/-]', '', $date);
103         $date = ereg_replace('/', '-', $date);
104         $parts = explode('-', $date);
105         switch(count($parts)) {
106                 case 1:
107                         $year = $parts[0];
108                         if(strlen($year) == 0) {
109                                 list($year, $month, $day) = explode('-', date('Y-m-d'));
110                         } else {
111                                 list($month, $day) = explode('-', date('m-d'));
112                         }
113                 break;
114                 case 2:
115                         list($year, $month) = $parts;
116                         $year = date('d');
117                 break;
118                 default:
119                         list($year, $month, $day) = $parts;
120         }
121
122         list($year, $month, $day) = clean_ymd($year, $month, $day);
123
124         return sprintf('%02u/%02u/%04u', $month, $day, $year);
125 }
126
127 function enc_mdy($date) {
128         return ymd_to_mdy($date);
129 }
130
131 ?>