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 function ymd_to_days($year, $month, $day) {
22 return (int)(mktime(12,0,0,$month,$day, $year, 0) / 86400);
25 function days_to_ymd($days) {
26 return explode('-', date('Y-n-j', $days * 86400 + 43200));
29 function days_to_weekday_name($days) {
30 $day_names = array('Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday');
31 return $day_names[$days % 7];
35 #function time_test() {
36 # for($i = 0; $i < 41 * 86400; $i += 86399) {
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>";
47 # pass anything, and get a valid date
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);
60 } elseif($month > 12) {
66 $max = date('t', mktime(12, 0, 0, $month, 1, $year));
72 return array($year, $month, $day);
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)) {
84 if(strlen($year) == 0) {
85 list($month, $day, $year) = explode('/', date('m/d/Y'));
87 list($month, $day) = explode('/', date('m/d'));
91 list($month, $year) = $parts;
95 list($month, $day, $year) = $parts;
98 return clean_ymd($year, $month, $day);
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);
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)) {
116 if(strlen($year) == 0) {
117 list($year, $month, $day) = explode('-', date('Y-m-d'));
119 list($month, $day) = explode('-', date('m-d'));
123 list($year, $month) = $parts;
127 list($year, $month, $day) = $parts;
130 return clean_ymd($year, $month, $day);
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);
139 function enc_mdy($str) {
140 return ymd_to_mdy($str);
143 function format_ymd($str) {
144 list($year, $month, $day) = ymd_clean($str);
145 return sprintf('%04u-%02u-%02u', $year, $month, $day);