JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
new date-mangling functions
[wfpl.git] / time.php
1 <?php
2
3 #  Copyright (C) 2007 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 # This file contains functions to manipulate/calculate dates/times
23
24 function ymd_to_days($year, $month, $day) {
25         return (int)(mktime(12,0,0,$month,$day, $year, 0) / 86400);
26 }
27
28 function days_to_ymd($days) {
29         return explode('-', date('Y-n-j', $days * 86400 + 43200));
30 }
31
32 function days_to_weekday_name($days) {
33         $day_names = array('Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday');
34         return $day_names[$days % 7];
35 }
36
37
38 #function time_test() {
39 #       for($i = 0; $i < 41 * 86400; $i += 86399) {
40 #               echo "seconds: $i";
41 #               $days = (int)($i / 86400);
42 #               list($year, $month, $day) = days_to_ymd($days);
43 #               echo ", days_to_ymd($days): $year-$month-$day";
44 #               $days = ymd_to_days($year, $month, $day);
45 #               echo ", ymd_to_days($year, $month, $day): $days\n<br>";
46 #       }
47 #}
48
49
50 # pass anything, and get a valid date
51 #
52 # returns array($year, $month, $day)
53 function clean_ymd($year, $month, $day) {
54         $year = intval($year, 10);
55         $month = intval($month, 10);
56         $day = intval($day, 10);
57
58         if($year < 100) {
59                 $year += 2000;
60         }
61         if($month < 1) {
62                 $month = 1;
63         } elseif($month > 12) {
64                 $month = 12;
65         }
66         if($day < 1) {
67                 $day = 1;
68         } elseif($day > 31) {
69                 # FIXME this should check the month
70                 $day = 31;
71         }
72
73         return array($year, $month, $day);
74 }
75
76 # convert date string from mm/dd/yyyy to yyy-mm-dd
77 function mdy_to_ymd($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                         list($month, $day) = explode('/', date('m/d'));
85                 break;
86                 case 2:
87                         list($month, $year) = $parts;
88                         $year = date('d');
89                 break;
90                 default:
91                         list($month, $day, $year) = $parts;
92         }
93
94         list($year, $month, $day) = clean_ymd($year, $month, $day);
95
96         return sprintf('%04u-%02u-%02u', $year, $month, $day);
97 }
98
99 # convert date string from yyy-mm-dd to mm/dd/yyyy
100 function ymd_to_mdy($date) {
101         $date = ereg_replace('[^0-9/-]', '', $date);
102         $date = ereg_replace('/', '-', $date);
103         $parts = explode('-', $date);
104         switch(count($parts)) {
105                 case 1:
106                         $year = $parts[0];
107                         list($month, $day) = explode('-', date('m-d'));
108                 break;
109                 case 2:
110                         list($year, $month) = $parts;
111                         $year = date('d');
112                 break;
113                 default:
114                         list($year, $month, $day) = $parts;
115         }
116
117         list($year, $month, $day) = clean_ymd($year, $month, $day);
118
119         return sprintf('%02u/%02u/%04u', $month, $day, $year);
120 }
121
122 ?>