Viewing file: phpmkrfn.php (17.12 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
/*
--------------------------------------------------------------------------------
Functions for PHPMaker 2.0
(C)2002-2004 e.World Technology Limited. All rights reserved.
--------------------------------------------------------------------------------
*/
define("DEFAULT_CURRENCY_SYMBOL", "$");
define("DEFAULT_MON_DECIMAL_POINT", ".");
define("DEFAULT_MON_THOUSANDS_SEP", ",");
define("DEFAULT_POSITIVE_SIGN", "");
define("DEFAULT_NEGATIVE_SIGN", "-");
define("DEFAULT_FRAC_DIGITS", 2);
define("DEFAULT_P_CS_PRECEDES", true);
define("DEFAULT_P_SEP_BY_SPACE", false);
define("DEFAULT_N_CS_PRECEDES", true);
define("DEFAULT_N_SEP_BY_SPACE", false);
define("DEFAULT_P_SIGN_POSN", 3);
define("DEFAULT_N_SIGN_POSN", 3);
// PHPMaker DEFAULT_DATE_FORMAT:
/* "yyyy/mm/dd"(default) or "mm/dd/yyyy" or "dd/mm/yyyy" */
define("DEFAULT_DATE_FORMAT", "dd/mm/yyyy");
// FormatDateTime
/*
Format a timestamp, datetime, date or time field from MySQL
$namedformat:
0 - General Date,
1 - Long Date,
2 - Short Date (Default),
3 - Long Time,
4 - Short Time,
5 - Short Date (yyyy/mm/dd),
6 - Short Date (mm/dd/yyyy),
7 - Short Date (dd/mm/yyyy)
*/
function FormatDateTime($ts, $namedformat)
{
$DefDateFormat = str_replace("yyyy", "%Y", DEFAULT_DATE_FORMAT);
$DefDateFormat = str_replace("mm", "%m", $DefDateFormat);
$DefDateFormat = str_replace("dd", "%d", $DefDateFormat);
if (is_numeric($ts)) // timestamp
{
switch (strlen($ts)) {
case 14:
$patt = '/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/';
break;
case 12:
$patt = '/(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/';
break;
case 10:
$patt = '/(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/';
break;
case 8:
$patt = '/(\d{4})(\d{2})(\d{2})/';
break;
case 6:
$patt = '/(\d{2})(\d{2})(\d{2})/';
break;
case 4:
$patt = '/(\d{2})(\d{2})/';
break;
case 2:
$patt = '/(\d{2})/';
break;
default:
return $ts;
}
if ((isset($patt))&&(preg_match($patt, $ts, $matches)))
{
$year = $matches[1];
$month = @$matches[2];
$day = @$matches[3];
$hour = @$matches[4];
$min = @$matches[5];
$sec = @$matches[6];
}
if (($namedformat==0)&&(strlen($ts)<10)) $namedformat = 2;
}
elseif (is_string($ts))
{
if (preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/', $ts, $matches)) // datetime
{
$year = $matches[1];
$month = $matches[2];
$day = $matches[3];
$hour = $matches[4];
$min = $matches[5];
$sec = $matches[6];
}
elseif (preg_match('/(\d{4})-(\d{2})-(\d{2})/', $ts, $matches)) // date
{
$year = $matches[1];
$month = $matches[2];
$day = $matches[3];
if ($namedformat==0) $namedformat = 2;
}
elseif (preg_match('/(^|\s)(\d{2}):(\d{2}):(\d{2})/', $ts, $matches)) // time
{
$hour = $matches[2];
$min = $matches[3];
$sec = $matches[4];
if (($namedformat==0)||($namedformat==1)) $namedformat = 3;
if ($namedformat==2) $namedformat = 4;
}
else
{
return $ts;
}
}
else
{
return $ts;
}
if (!isset($year)) $year = 0; // dummy value for times
if (!isset($month)) $month = 1;
if (!isset($day)) $day = 1;
if (!isset($hour)) $hour = 0;
if (!isset($min)) $min = 0;
if (!isset($sec)) $sec = 0;
$uts = @mktime($hour, $min, $sec, $month, $day, $year);
if ($uts == -1) { // failed to convert
$year = substr_replace("0000", $year, -1 * strlen($year));
$month = substr_replace("00", $month, -1 * strlen($month));
$day = substr_replace("00", $day, -1 * strlen($day));
$hour = substr_replace("00", $hour, -1 * strlen($hour));
$min = substr_replace("00", $min, -1 * strlen($min));
$sec = substr_replace("00", $sec, -1 * strlen($sec));
$DefDateFormat = str_replace("yyyy", $year, DEFAULT_DATE_FORMAT);
$DefDateFormat = str_replace("mm", $month, $DefDateFormat);
$DefDateFormat = str_replace("dd", $day, $DefDateFormat);
switch ($namedformat) {
case 0:
return $DefDateFormat." $hour:$min:$sec";
break;
case 1://unsupported, return general date
return $DefDateFormat." $hour:$min:$sec";
break;
case 2:
return $DefDateFormat;
break;
case 3:
if (intval($hour)==0)
return "12:$min:$sec AM";
elseif (intval($hour)>0 && intval($hour)<12)
return "$hour:$min:$sec AM";
elseif (intval($hour)==12)
return "$hour:$min:$sec PM";
elseif (intval($hour)>12 && intval($hour)<=23)
return (intval($hour)-12).":$min:$sec PM";
else
return "$hour:$min:$sec";
break;
case 4:
return "$hour:$min:$sec";
break;
case 5:
return "$year/$month/$day";
break;
case 6:
return "$month/$day/$year";
break;
case 7:
return "$day/$month/$year";
break;
}
} else {
switch ($namedformat) {
case 0:
return strftime($DefDateFormat." %H:%M:%S", $uts);
break;
case 1:
return strftime("%A, %B %d, %Y", $uts);
break;
case 2:
return strftime($DefDateFormat, $uts);
break;
case 3:
return strftime("%I:%M:%S %p", $uts);
break;
case 4:
return strftime("%H:%M:%S", $uts);
break;
case 5:
return strftime("%Y/%m/%d", $uts);
break;
case 6:
return strftime("%m/%d/%Y", $uts);
break;
case 7:
return strftime("%d/%m/%Y", $uts);
break;
}
}
}
// Convert a date to MySQL format
function ConvertDateToMysqlFormat($dateStr)
{
@list($datePt, $timePt) = explode(" ", $dateStr);
$arDatePt = explode("/", $datePt);
if (count($arDatePt) == 3) {
switch (DEFAULT_DATE_FORMAT) {
case "yyyy/mm/dd":
list($year, $month, $day) = $arDatePt;
break;
case "mm/dd/yyyy":
list($month, $day, $year) = $arDatePt;
break;
case "dd/mm/yyyy":
list($day, $month, $year) = $arDatePt;
break;
}
return trim($year . "-" . $month . "-" . $day . " " . $timePt);
} else {
return $dateStr;
}
}
// FormatCurrency
/*
FormatCurrency(Expression[,NumDigitsAfterDecimal [,IncludeLeadingDigit
[,UseParensForNegativeNumbers [,GroupDigits]]]])
NumDigitsAfterDecimal is the numeric value indicating how many places to the
right of the decimal are displayed
-1 Use Default
The IncludeLeadingDigit, UseParensForNegativeNumbers, and GroupDigits
arguments have the following settings:
-1 True
0 False
-2 Use Default
*/
function FormatCurrency($amount, $NumDigitsAfterDecimal, $IncludeLeadingDigit, $UseParensForNegativeNumbers, $GroupDigits)
{
// export the values returned by localeconv into the local scope
if (function_exists("localeconv")) extract(localeconv());
// set defaults if locale is not set
if (empty($currency_symbol)) $currency_symbol = DEFAULT_CURRENCY_SYMBOL;
if (empty($mon_decimal_point)) $mon_decimal_point = DEFAULT_MON_DECIMAL_POINT;
if (empty($mon_thousands_sep)) $mon_thousands_sep = DEFAULT_MON_THOUSANDS_SEP;
if (empty($positive_sign)) $positive_sign = DEFAULT_POSITIVE_SIGN;
if (empty($negative_sign)) $negative_sign = DEFAULT_NEGATIVE_SIGN;
if (empty($frac_digits) || $frac_digits == CHAR_MAX) $frac_digits = DEFAULT_FRAC_DIGITS;
if (empty($p_cs_precedes) || $p_cs_precedes == CHAR_MAX) $p_cs_precedes = DEFAULT_P_CS_PRECEDES;
if (empty($p_sep_by_space) || $p_sep_by_space == CHAR_MAX) $p_sep_by_space = DEFAULT_P_SEP_BY_SPACE;
if (empty($n_cs_precedes) || $n_cs_precedes == CHAR_MAX) $n_cs_precedes = DEFAULT_N_CS_PRECEDES;
if (empty($n_sep_by_space) || $n_sep_by_space == CHAR_MAX) $n_sep_by_space = DEFAULT_N_SEP_BY_SPACE;
if (empty($p_sign_posn) || $p_sign_posn == CHAR_MAX) $p_sign_posn = DEFAULT_P_SIGN_POSN;
if (empty($n_sign_posn) || $n_sign_posn == CHAR_MAX) $n_sign_posn = DEFAULT_N_SIGN_POSN;
// check $NumDigitsAfterDecimal
if ($NumDigitsAfterDecimal > -1)
$frac_digits = $NumDigitsAfterDecimal;
// check $UseParensForNegativeNumbers
if ($UseParensForNegativeNumbers == -1) {
$n_sign_posn = 0;
if ($p_sign_posn == 0) {
if (DEFAULT_P_SIGN_POSN != 0)
$p_sign_posn = DEFAULT_P_SIGN_POSN;
else
$p_sign_posn = 3;
}
} elseif ($UseParensForNegativeNumbers == 0) {
if ($n_sign_posn == 0)
if (DEFAULT_P_SIGN_POSN != 0)
$n_sign_posn = DEFAULT_P_SIGN_POSN;
else
$n_sign_posn = 3;
}
// check $GroupDigits
if ($GroupDigits == -1) {
$mon_thousands_sep = DEFAULT_MON_THOUSANDS_SEP;
} elseif ($GroupDigits == 0) {
$mon_thousands_sep = "";
}
// start by formatting the unsigned number
$number = number_format(abs($amount),
$frac_digits,
$mon_decimal_point,
$mon_thousands_sep);
// check $IncludeLeadingDigit
if ($IncludeLeadingDigit == 0) {
if (substr($number, 0, 2) == "0.")
$number = substr($number, 1, strlen($number)-1);
}
if ($amount < 0) {
$sign = $negative_sign;
// "extracts" the boolean value as an integer
$n_cs_precedes = intval($n_cs_precedes == true);
$n_sep_by_space = intval($n_sep_by_space == true);
$key = $n_cs_precedes . $n_sep_by_space . $n_sign_posn;
} else {
$sign = $positive_sign;
$p_cs_precedes = intval($p_cs_precedes == true);
$p_sep_by_space = intval($p_sep_by_space == true);
$key = $p_cs_precedes . $p_sep_by_space . $p_sign_posn;
}
$formats = array(
// currency symbol is after amount
// no space between amount and sign
'000' => '(%s' . $currency_symbol . ')',
'001' => $sign . '%s ' . $currency_symbol,
'002' => '%s' . $currency_symbol . $sign,
'003' => '%s' . $sign . $currency_symbol,
'004' => '%s' . $sign . $currency_symbol,
// one space between amount and sign
'010' => '(%s ' . $currency_symbol . ')',
'011' => $sign . '%s ' . $currency_symbol,
'012' => '%s ' . $currency_symbol . $sign,
'013' => '%s ' . $sign . $currency_symbol,
'014' => '%s ' . $sign . $currency_symbol,
// currency symbol is before amount
// no space between amount and sign
'100' => '(' . $currency_symbol . '%s)',
'101' => $sign . $currency_symbol . '%s',
'102' => $currency_symbol . '%s' . $sign,
'103' => $sign . $currency_symbol . '%s',
'104' => $currency_symbol . $sign . '%s',
// one space between amount and sign
'110' => '(' . $currency_symbol . ' %s)',
'111' => $sign . $currency_symbol . ' %s',
'112' => $currency_symbol . ' %s' . $sign,
'113' => $sign . $currency_symbol . ' %s',
'114' => $currency_symbol . ' ' . $sign . '%s');
// lookup the key in the above array
return sprintf($formats[$key], $number);
}
// FormatNumber
/*
FormatNumber(Expression[,NumDigitsAfterDecimal [,IncludeLeadingDigit
[,UseParensForNegativeNumbers [,GroupDigits]]]])
NumDigitsAfterDecimal is the numeric value indicating how many places to the
right of the decimal are displayed
-1 Use Default
The IncludeLeadingDigit, UseParensForNegativeNumbers, and GroupDigits
arguments have the following settings:
-1 True
0 False
-2 Use Default
*/
function FormatNumber($amount, $NumDigitsAfterDecimal, $IncludeLeadingDigit, $UseParensForNegativeNumbers, $GroupDigits)
{
// export the values returned by localeconv into the local scope
if (function_exists("localeconv")) extract(localeconv());
// set defaults if locale is not set
if (empty($currency_symbol)) $currency_symbol = DEFAULT_CURRENCY_SYMBOL;
if (empty($mon_decimal_point)) $mon_decimal_point = DEFAULT_MON_DECIMAL_POINT;
if (empty($mon_thousands_sep)) $mon_thousands_sep = DEFAULT_MON_THOUSANDS_SEP;
if (empty($positive_sign)) $positive_sign = DEFAULT_POSITIVE_SIGN;
if (empty($negative_sign)) $negative_sign = DEFAULT_NEGATIVE_SIGN;
if (empty($frac_digits) || $frac_digits == CHAR_MAX) $frac_digits = DEFAULT_FRAC_DIGITS;
if (empty($p_cs_precedes) || $p_cs_precedes == CHAR_MAX) $p_cs_precedes = DEFAULT_P_CS_PRECEDES;
if (empty($p_sep_by_space) || $p_sep_by_space == CHAR_MAX) $p_sep_by_space = DEFAULT_P_SEP_BY_SPACE;
if (empty($n_cs_precedes) || $n_cs_precedes == CHAR_MAX) $n_cs_precedes = DEFAULT_N_CS_PRECEDES;
if (empty($n_sep_by_space) || $n_sep_by_space == CHAR_MAX) $n_sep_by_space = DEFAULT_N_SEP_BY_SPACE;
if (empty($p_sign_posn) || $p_sign_posn == CHAR_MAX) $p_sign_posn = DEFAULT_P_SIGN_POSN;
if (empty($n_sign_posn) || $n_sign_posn == CHAR_MAX) $n_sign_posn = DEFAULT_N_SIGN_POSN;
// check $NumDigitsAfterDecimal
if ($NumDigitsAfterDecimal > -1)
$frac_digits = $NumDigitsAfterDecimal;
// check $UseParensForNegativeNumbers
if ($UseParensForNegativeNumbers == -1) {
$n_sign_posn = 0;
if ($p_sign_posn == 0) {
if (DEFAULT_P_SIGN_POSN != 0)
$p_sign_posn = DEFAULT_P_SIGN_POSN;
else
$p_sign_posn = 3;
}
} elseif ($UseParensForNegativeNumbers == 0) {
if ($n_sign_posn == 0)
if (DEFAULT_P_SIGN_POSN != 0)
$n_sign_posn = DEFAULT_P_SIGN_POSN;
else
$n_sign_posn = 3;
}
// check $GroupDigits
if ($GroupDigits == -1) {
$mon_thousands_sep = DEFAULT_MON_THOUSANDS_SEP;
} elseif ($GroupDigits == 0) {
$mon_thousands_sep = "";
}
// start by formatting the unsigned number
$number = number_format(abs($amount),
$frac_digits,
$mon_decimal_point,
$mon_thousands_sep);
// check $IncludeLeadingDigit
if ($IncludeLeadingDigit == 0) {
if (substr($number, 0, 2) == "0.")
$number = substr($number, 1, strlen($number)-1);
}
if ($amount < 0) {
$sign = $negative_sign;
$key = $n_sign_posn;
} else {
$sign = $positive_sign;
$key = $p_sign_posn;
}
$formats = array(
'0' => '(%s)',
'1' => $sign . '%s',
'2' => $sign . '%s',
'3' => $sign . '%s',
'4' => $sign . '%s');
// lookup the key in the above array
return sprintf($formats[$key], $number);
}
// FormatPercent
/*
FormatPercent(Expression[,NumDigitsAfterDecimal [,IncludeLeadingDigit
[,UseParensForNegativeNumbers [,GroupDigits]]]])
NumDigitsAfterDecimal is the numeric value indicating how many places to the
right of the decimal are displayed
-1 Use Default
The IncludeLeadingDigit, UseParensForNegativeNumbers, and GroupDigits
arguments have the following settings:
-1 True
0 False
-2 Use Default
*/
function FormatPercent($amount, $NumDigitsAfterDecimal, $IncludeLeadingDigit, $UseParensForNegativeNumbers, $GroupDigits)
{
// export the values returned by localeconv into the local scope
if (function_exists("localeconv")) extract(localeconv());
// set defaults if locale is not set
if (empty($currency_symbol)) $currency_symbol = DEFAULT_CURRENCY_SYMBOL;
if (empty($mon_decimal_point)) $mon_decimal_point = DEFAULT_MON_DECIMAL_POINT;
if (empty($mon_thousands_sep)) $mon_thousands_sep = DEFAULT_MON_THOUSANDS_SEP;
if (empty($positive_sign)) $positive_sign = DEFAULT_POSITIVE_SIGN;
if (empty($negative_sign)) $negative_sign = DEFAULT_NEGATIVE_SIGN;
if (empty($frac_digits) || $frac_digits == CHAR_MAX) $frac_digits = DEFAULT_FRAC_DIGITS;
if (empty($p_cs_precedes) || $p_cs_precedes == CHAR_MAX) $p_cs_precedes = DEFAULT_P_CS_PRECEDES;
if (empty($p_sep_by_space) || $p_sep_by_space == CHAR_MAX) $p_sep_by_space = DEFAULT_P_SEP_BY_SPACE;
if (empty($n_cs_precedes) || $n_cs_precedes == CHAR_MAX) $n_cs_precedes = DEFAULT_N_CS_PRECEDES;
if (empty($n_sep_by_space) || $n_sep_by_space == CHAR_MAX) $n_sep_by_space = DEFAULT_N_SEP_BY_SPACE;
if (empty($p_sign_posn) || $p_sign_posn == CHAR_MAX) $p_sign_posn = DEFAULT_P_SIGN_POSN;
if (empty($n_sign_posn) || $n_sign_posn == CHAR_MAX) $n_sign_posn = DEFAULT_N_SIGN_POSN;
// check $NumDigitsAfterDecimal
if ($NumDigitsAfterDecimal > -1)
$frac_digits = $NumDigitsAfterDecimal;
// check $UseParensForNegativeNumbers
if ($UseParensForNegativeNumbers == -1) {
$n_sign_posn = 0;
if ($p_sign_posn == 0) {
if (DEFAULT_P_SIGN_POSN != 0)
$p_sign_posn = DEFAULT_P_SIGN_POSN;
else
$p_sign_posn = 3;
}
} elseif ($UseParensForNegativeNumbers == 0) {
if ($n_sign_posn == 0)
if (DEFAULT_P_SIGN_POSN != 0)
$n_sign_posn = DEFAULT_P_SIGN_POSN;
else
$n_sign_posn = 3;
}
// check $GroupDigits
if ($GroupDigits == -1) {
$mon_thousands_sep = DEFAULT_MON_THOUSANDS_SEP;
} elseif ($GroupDigits == 0) {
$mon_thousands_sep = "";
}
// start by formatting the unsigned number
$number = number_format(abs($amount)*100,
$frac_digits,
$mon_decimal_point,
$mon_thousands_sep);
// check $IncludeLeadingDigit
if ($IncludeLeadingDigit == 0) {
if (substr($number, 0, 2) == "0.")
$number = substr($number, 1, strlen($number)-1);
}
if ($amount < 0) {
$sign = $negative_sign;
$key = $n_sign_posn;
} else {
$sign = $positive_sign;
$key = $p_sign_posn;
}
$formats = array(
'0' => '(%s%%)',
'1' => $sign . '%s%%',
'2' => $sign . '%s%%',
'3' => $sign . '%s%%',
'4' => $sign . '%s%%');
// lookup the key in the above array
return sprintf($formats[$key], $number);
}
?>
|