3 # Copyright (C) 2007 Jason Woofenden
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.
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.
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/>.
19 # This file contains functions to manipulate/calculate dates/times
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);
26 function days_to_ymd($days) {
27 return explode('-', date('Y-n-j', $days * 86400 + 43200));
30 function days_to_weekday_name($days) {
31 $day_names = array('Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday');
32 return $day_names[$days % 7];
36 #function time_test() {
37 # for($i = 0; $i < 41 * 86400; $i += 86399) {
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>";
48 # pass anything, and get a valid date
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);
61 } elseif($month > 12) {
67 $max = date('t', mktime(12, 0, 0, $month, 1, $year));
73 return array($year, $month, $day);
76 # pass date like 3/21/99
77 # returns array(year, month, day)
78 function mdy_clean($date) {
79 $date = ereg_replace('[^0-9/-]', '', $date);
80 $date = ereg_replace('-', '/', $date);
81 $parts = explode('/', $date);
82 switch(count($parts)) {
85 if(strlen($year) == 0) {
86 list($month, $day, $year) = explode('/', date('m/d/Y'));
88 list($month, $day) = explode('/', date('m/d'));
92 list($month, $year) = $parts;
96 list($month, $day, $year) = $parts;
99 return clean_ymd($year, $month, $day);
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);
108 # pass date like 2008-11-21
109 # returns array(year, month, day)
110 function ymd_clean($date) {
111 $date = ereg_replace('[^0-9/-]', '', $date);
112 $date = ereg_replace('/', '-', $date);
113 $parts = explode('-', $date);
114 switch(count($parts)) {
117 if(strlen($year) == 0 || $year < 1971 || $year > 2050) {
118 list($year, $month, $day) = explode('-', date('Y-m-d'));
120 list($month, $day) = explode('-', date('m-d'));
124 list($year, $month) = $parts;
128 list($year, $month, $day) = $parts;
131 return clean_ymd($year, $month, $day);
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);