Files
HotLunches/usr_lib/global.functions.php
T
2026-10-19 15:22:52 -07:00

143 lines
3.2 KiB
PHP

<?php
//
// Global functions of useful code
//
//
// year_calc
// - added 2007-02-08 (MWF)
//
// Calculates a person's age or anniversary number given
// a start date and an optional anniversary date. If no
// anniversary date is given it will use today's date.
//
// If called with a person's birth date it will return their
// current age.
//
if (!function_exists('year_calc')) {
function year_calc($start_date,$anniversary_date="") {
list($year,$month,$day) = explode("-",$start_date);
if ($anniversary_date) list($year1,$month1,$day1) = explode("-",$anniversary_date);
else list($year1,$month1,$day1) = explode("-",date("Y-m-d",time()));
$year_diff = $year1 - $year;
$month_diff = $month1 - $month;
$day_diff = $day1 - $day;
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
}
//
// date_difference
// - added 2007-05-09
//
// Same as above but this function returns an array of values
// -> years
// -> months
// -> days
// -> total_months
// -> total_days
//
//// if (!function_exists('date_difference')) {
//// function date_difference($start_date,$anniversary_date="") {
//// list($year,$month,$day) = explode("-",$start_date);
////
//// if ($anniversary_date) list($year1,$month1,$day1) = explode("-",$anniversary_date);
//// else list($year1,$month1,$day1) = explode("-",date("Y-m-d",time()));
////
//// $year_diff = $year1 - $year;
//// $month_diff = $month1 - $month;
//// $day_diff = $day1 - $day;
////
//// if ($month_diff < 0) {
//// $year_diff--;
//// $month_diff = 12 + $month_diff;
//// }
//// elseif (($month_diff==0) && ($day_diff < 0)) {
//// $year_diff--;
//// }
////
//// return $year_diff;
//// }
////}
//
// html
// - added 2007-05-09
//
// Convert a string of text so that all applicable characters are HTML entities
//
if (!function_exists('html')) {
function html($text) {
return htmlentities(stripslashes($text));
}
}
//
// parseEmailText
// - added 2007-05-09
//
// Perform value replacement on all variables in a block of text. The set of
// values to replace is passed as an array and the array indices are the variables..
//
if (!function_exists('parseEmailText')) {
function parseEmailText($message, $vals) {
$msg = $message;
foreach ($vals as $name => $value) {
$msg = eregi_replace("\[{$name}\]", $value, $msg);
$msg = eregi_replace("\{{$name}\}", $value, $msg);
}
return nl2br(trim($msg));
}
}
//
// timeToEnglish
// - added 2007-05-09
//
// Convert a time value (seconds since midnight) to an English value of hours and minutes
//
if (!function_exists('timeToEnglish')) {
function timeToEnglish($time, $hr24=0, $ampm=0) {
$hr = floor($time / 60);
if (!$hr24) {
$hr = $hr % 12;
if ($hr == 0) $hr = 12;
}
else {
if ($hr < 10) $hr = "0" . $hr;
}
$min = $time % 60;
if ($min < 10) $min = "0" . $min;
$t = "{$hr}:{$min}";
if ($ampm) {
if ($time < 12*60) {
$t .= " am";
}
else {
$t .= " pm";
}
}
return $t;
}
}
?>