231 lines
6.1 KiB
PHP
231 lines
6.1 KiB
PHP
<?php
|
|
// writeLog("date-time.php");
|
|
|
|
|
|
// Date and Time processing functions
|
|
|
|
//
|
|
// mapTimeZone to convert invalid Time Zone entries to valid ones
|
|
//
|
|
function mapTimeZone($tz) {
|
|
switch ($tz) {
|
|
case 'Canada/Pacific':
|
|
$timezone = "America/Vancouver";
|
|
break;
|
|
|
|
case 'Canada/Mountain':
|
|
$timezone = "America/Edmonton";
|
|
break;
|
|
|
|
case 'Canada/Central':
|
|
$timezone = "America/Winnipeg";
|
|
break;
|
|
|
|
case 'Canada/Eastern':
|
|
$timezone = "America/Toronto";
|
|
break;
|
|
|
|
default:
|
|
$timezone = $tz;
|
|
break;
|
|
}
|
|
|
|
return($timezone);
|
|
}
|
|
|
|
|
|
//
|
|
// Get date text
|
|
//
|
|
function dateString($fmt='',$time=0) {
|
|
|
|
global $school_record;
|
|
global $currLanguage;
|
|
|
|
// Set the current timezone
|
|
$timezone = mapTimeZone($school_record['timezone']);
|
|
date_default_timezone_set($timezone);
|
|
|
|
if ($time == 0) $time = time();
|
|
|
|
if (strlen($fmt) == 0) $fmt = 'date';
|
|
if ($fmt == 'datetime') $fmt = 'defaulttime';
|
|
|
|
// Get the mapping of the month name
|
|
$monthText = date("F",$time);
|
|
$monthCode = "MONTH-" . strtoupper($monthText);
|
|
$month = getLanguageText($monthCode);
|
|
|
|
// Get the mapping of the short month name
|
|
$monText = date("M",$time);
|
|
$monCode = "MONTH-ABBR-" . strtoupper($monthText);
|
|
$mon = getLanguageText($monCode);
|
|
|
|
// Get the mapping of the day of the week
|
|
$dowText = date("l",$time);
|
|
$dowCode = "TEXT-" . strtoupper($dowText);
|
|
$dow = getLanguageText($dowCode);
|
|
|
|
// Format the date
|
|
switch ($fmt) {
|
|
case 'mon':
|
|
$date_string = $mon;
|
|
break;
|
|
|
|
case 'month':
|
|
$date_string = $month;
|
|
break;
|
|
|
|
case 'mon-year':
|
|
$date_string = $mon . "-" . date("Y",time);
|
|
break;
|
|
|
|
case 'month-year':
|
|
$date_string = $month . "-" . date("Y",time);
|
|
break;
|
|
|
|
case 'shortdate':
|
|
case 'shortdatetime':
|
|
case 'shortdate@time':
|
|
if (!array_key_exists('short_date_format',$school_record) || strlen($school_record['short_date_format']) == 0) $dateFormat = "M j";
|
|
else $dateFormat = $school_record['short_date_format'];
|
|
|
|
if (strlen($school_record['time_format']) == 0) $timeFormat = "g:ia";
|
|
else $timeFormat = $school_record['time_format'];
|
|
|
|
$date_string = date($dateFormat,$time);
|
|
$date_string = str_replace($monthText,$month,$date_string);
|
|
$date_string = str_replace($dowText,$dow,$date_string);
|
|
|
|
if ($fmt == 'shortdatetime') $date_string .= " " . date($timeFormat,$time);
|
|
if ($fmt == 'shortdate@time') $date_string .= " @ " . date($timeFormat,$time);
|
|
break;
|
|
|
|
default:
|
|
case 'date':
|
|
case 'datetime':
|
|
case 'date@time':
|
|
case 'default':
|
|
case 'defaulttime':
|
|
if (strlen($school_record['date_format']) == 0) $dateFormat = "F j, Y";
|
|
else $dateFormat = $school_record['date_format'];
|
|
|
|
if (strlen($school_record['time_format']) == 0) $timeFormat = "g:ia";
|
|
else $timeFormat = $school_record['time_format'];
|
|
|
|
$date_string = date($dateFormat,$time);
|
|
$date_string = str_replace($dowText,$dow,$date_string);
|
|
|
|
if (strpos($date_string, $monthText) !== false) {
|
|
// writeLog("monthText: $monthText");
|
|
// writeLog("month: $month");
|
|
// writeLog("date_string: $date_string");
|
|
$date_string = str_replace($monthText,$month,$date_string);
|
|
}
|
|
else {
|
|
// writeLog("monText: $monText");
|
|
// writeLog("mon: $mon");
|
|
// writeLog("date_string: $date_string");
|
|
$date_string = str_replace($monText,$mon,$date_string);
|
|
}
|
|
|
|
if ($fmt == 'datetime') $date_string .= " " . date($timeFormat,$time);
|
|
if ($fmt == 'defaulttime') $date_string .= " " . date($timeFormat,$time);
|
|
if ($fmt == 'date@time') $date_string .= " @ " . date($timeFormat,$time);
|
|
break;
|
|
|
|
case 'date-cal':
|
|
// Get the mapping of the short month name
|
|
$monText = date("F",$time);
|
|
$monCode = "MONTH-ABBR-" . strtoupper($monText);
|
|
$monText = date("M",$time);
|
|
$mon = getLanguageText($monCode);
|
|
|
|
// Get the mapping of the day of the week
|
|
$dowText = date("D",$time);
|
|
$dowCode = "TEXT-" . strtoupper($dowText);
|
|
$dow = getLanguageText($dowCode);
|
|
|
|
$date_string = date("D M j",$time);
|
|
$date_string = str_replace($monText,$mon,$date_string);
|
|
$date_string = str_replace($dowText,$dow,$date_string);
|
|
break;
|
|
|
|
case 'day':
|
|
// Get the mapping of the day of the week
|
|
$dowText = date("D",$time);
|
|
$dowCode = "TEXT-" . strtoupper($dowText);
|
|
$dow = getLanguageText($dowCode);
|
|
$date_string = $dow;
|
|
break;
|
|
|
|
case 'time':
|
|
if (strlen($school_record['time_format']) == 0) $timeFormat = "g:ia";
|
|
else $timeFormat = $school_record['time_format'];
|
|
|
|
$date_string = date($timeFormat,$time);
|
|
break;
|
|
}
|
|
|
|
return($date_string);
|
|
}
|
|
|
|
|
|
//
|
|
// Get default date time
|
|
//
|
|
function dateFormat($time=0) {
|
|
return(dateString('datetime',$time));
|
|
}
|
|
|
|
|
|
//
|
|
// Reformat date field based on a format string
|
|
//
|
|
function formatDate($date,$date_format) {
|
|
switch($date_format) {
|
|
case 'YYYY-MM-DD':
|
|
return($date);
|
|
break;
|
|
|
|
case 'YYYY/MM/DD':
|
|
case 'YY/MM/DD':
|
|
list($yr,$mo,$dy) = explode('/',$date);
|
|
break;
|
|
|
|
case 'YY-MM-DD':
|
|
list($yr,$mo,$dy) = explode('-',$date);
|
|
break;
|
|
|
|
case 'DD-MM-YYYY':
|
|
case 'DD-MM-YY':
|
|
list($dy,$mo,$yr) = explode('-',$date);
|
|
break;
|
|
|
|
case 'DD/MM/YYYY':
|
|
case 'DD/MM/YY':
|
|
list($dy,$mo,$yr) = explode('/',$date);
|
|
break;
|
|
|
|
case 'MM-DD-YYYY':
|
|
case 'MM-DD-YY':
|
|
list($mo,$dy,$yr) = explode('-',$date);
|
|
break;
|
|
|
|
case 'MM/DD/YYYY':
|
|
case 'MM/DD/YY':
|
|
list($mo,$dy,$yr) = explode('/',$date);
|
|
break;
|
|
}
|
|
|
|
if (strlen($yr) == 2) {
|
|
if ($yr <= date("y",time())) $yr = "20$yr";
|
|
else $yr = "19$yr";
|
|
}
|
|
$date = sprintf("%4d-%02d-%02d",$yr,$mo,$dy);
|
|
|
|
return($date);
|
|
}
|
|
|
|
?>
|