From 1442d052713fdb380810692bbe1ddee5306b5a7a Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Wed, 3 Oct 2007 18:25:11 -0400 Subject: [PATCH] new date-mangling functions --- calendar.php | 4 ++-- time.php | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/calendar.php b/calendar.php index be81f98..9d109a6 100644 --- a/calendar.php +++ b/calendar.php @@ -71,7 +71,7 @@ function calendar($year, $month, $events = 0, $template = 0) { $template->set('month_year', strftime('%B', $start_timestamp) . " " . $year); # number of non-day slots at the begining of the month - $pre_non_days = date( 'w', $start_timestamp ); + $pre_non_days = date('w', $start_timestamp ); # first display empty cells so the 1st can be in the right column while($cell < $pre_non_days) { @@ -80,7 +80,7 @@ function calendar($year, $month, $events = 0, $template = 0) { } # do the days in this month - $days_count = date( 't', $start_timestamp ); + $days_count = date('t', $start_timestamp ); for($day = 1; $day <= $days_count; $day++ ) { $template->set('day_number', $day); if(($cell + 1) % 7 < 2) { diff --git a/time.php b/time.php index 501d89c..b9181b0 100644 --- a/time.php +++ b/time.php @@ -45,3 +45,78 @@ function days_to_weekday_name($days) { # echo ", ymd_to_days($year, $month, $day): $days\n
"; # } #} + + +# pass anything, and get a valid date +# +# returns array($year, $month, $day) +function clean_ymd($year, $month, $day) { + $year = intval($year, 10); + $month = intval($month, 10); + $day = intval($day, 10); + + if($year < 100) { + $year += 2000; + } + if($month < 1) { + $month = 1; + } elseif($month > 12) { + $month = 12; + } + if($day < 1) { + $day = 1; + } elseif($day > 31) { + # FIXME this should check the month + $day = 31; + } + + return array($year, $month, $day); +} + +# convert date string from mm/dd/yyyy to yyy-mm-dd +function mdy_to_ymd($date) { + $date = ereg_replace('[^0-9/-]', '', $date); + $date = ereg_replace('-', '/', $date); + $parts = explode('/', $date); + switch(count($parts)) { + case 1: + $year = $parts[0]; + list($month, $day) = explode('/', date('m/d')); + break; + case 2: + list($month, $year) = $parts; + $year = date('d'); + break; + default: + list($month, $day, $year) = $parts; + } + + list($year, $month, $day) = clean_ymd($year, $month, $day); + + return sprintf('%04u-%02u-%02u', $year, $month, $day); +} + +# convert date string from yyy-mm-dd to mm/dd/yyyy +function ymd_to_mdy($date) { + $date = ereg_replace('[^0-9/-]', '', $date); + $date = ereg_replace('/', '-', $date); + $parts = explode('-', $date); + switch(count($parts)) { + case 1: + $year = $parts[0]; + list($month, $day) = explode('-', date('m-d')); + break; + case 2: + list($year, $month) = $parts; + $year = date('d'); + break; + default: + list($year, $month, $day) = $parts; + } + + list($year, $month, $day) = clean_ymd($year, $month, $day); + + return sprintf('%02u/%02u/%04u', $month, $day, $year); +} + +?> -- 1.7.10.4