From 5061f0bf1235972ff1a6d8f3e01097ae84b0107e Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Fri, 6 Feb 2009 15:39:02 -0500 Subject: [PATCH] added code for 12 hour time formatting. Moved enc_mdy and format_ymd out of time.php --- encode.php | 25 +++++++++++++++++++++++++ format.php | 20 ++++++++++++++++++++ time.php | 10 +--------- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/encode.php b/encode.php index 6102a91..273d026 100644 --- a/encode.php +++ b/encode.php @@ -161,6 +161,11 @@ function enc_mmddyyyy($yyyy_mm_dd) { return substr($yyyy_mm_dd, 5, 2) . '/' . substr($yyyy_mm_dd, 8, 2) . '/' . substr($yyyy_mm_dd, 0, 4); } +# depricated. call enc_mmddyyyy() instead +function enc_mdy($str) { + return enc_mmddyyyy($str); +} + function enc_mmddyyyyhhmm($seconds) { return date('m/d/Y g:ia', (int)$seconds); } @@ -177,6 +182,26 @@ function enc_hhmm($str) { return $str; } +# takes decimal +# returns hh:mm followed by "am" or "pm" with no space +function enc_12hr($str) { + if(strlen($str) == 0) { + return ''; + } + $hours = floor($str); + $minutes = round(($str - $hours) * 60); + $suffix = 'am'; + if($hours >= 12.0) { + $suffix = 'pm'; + if($hours > 12.0) { + $hours -= 12.0; + } + } + $str = sprintf("%d:%02d", $hours, $minutes); + $str .= $suffix; + return $str; +} + diff --git a/format.php b/format.php index 8035be4..f90fc1f 100644 --- a/format.php +++ b/format.php @@ -218,6 +218,13 @@ function format_mdy_to_ymd($str) { return mdy_to_ymd(format_oneline($str)); } +# date is yyyy-mm-dd +function format_ymd($str) { + require_once('code/wfpl/time.php'); + list($year, $month, $day) = ymd_clean($str); + return sprintf('%04u-%02u-%02u', $year, $month, $day); +} + # takes any of: HH :MM HH:MM # returns decimal number of hours # @@ -253,6 +260,19 @@ function format_hours($str) { } } +# takes eg 12:23am +# returns decimal number of hours since midnight +function format_12hr_to_hours($str) { + if(eregi('noon', $str)) { + return 12; + } + $hours = format_hours($str); + if($hours < 12 && eregi('p', $str)) { + $hours += 12; + } + return $hours; +} + function format_phone($str) { $str = ereg_replace('[^0-9]', '', $str); diff --git a/time.php b/time.php index be2763e..3c42f79 100644 --- a/time.php +++ b/time.php @@ -18,6 +18,7 @@ # This file contains functions to manipulate/calculate dates/times +# FIXME make it so you can call this with a string YYYY-MM-DD function ymd_to_days($year, $month, $day) { return (int)(mktime(12,0,0,$month,$day, $year, 0) / 86400); } @@ -135,12 +136,3 @@ function ymd_to_mdy($str) { list($year, $month, $day) = ymd_clean($str); return sprintf('%02u/%02u/%04u', $month, $day, $year); } - -function enc_mdy($str) { - return ymd_to_mdy($str); -} - -function format_ymd($str) { - list($year, $month, $day) = ymd_clean($str); - return sprintf('%04u-%02u-%02u', $year, $month, $day); -} -- 1.7.10.4