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 # FIXME this should check the month
70 return array($year, $month, $day);
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)) {
81 if(strlen($year) == 0) {
82 list($month, $day, $year) = explode('/', date('m/d/Y'));
84 list($month, $day) = explode('/', date('m/d'));
88 list($month, $year) = $parts;
92 list($month, $day, $year) = $parts;
95 list($year, $month, $day) = clean_ymd($year, $month, $day);
97 return sprintf('%04u-%02u-%02u', $year, $month, $day);
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)) {
108 if(strlen($year) == 0) {
109 list($year, $month, $day) = explode('-', date('Y-m-d'));
111 list($month, $day) = explode('-', date('m-d'));
115 list($year, $month) = $parts;
119 list($year, $month, $day) = $parts;
122 list($year, $month, $day) = clean_ymd($year, $month, $day);
124 return sprintf('%02u/%02u/%04u', $month, $day, $year);
127 function enc_mdy($date) {
128 return ymd_to_mdy($date);