Thursday, July 31, 2008

PHP - Convert DateTo Mysql Format

function ConvertDateToMysqlFormat($dateStr)
{
@list($datePt, $timePt) = explode(" ", $dateStr);
$arDatePt = explode(EW_DATE_SEPARATOR, $datePt);
if (count($arDatePt) == 3) {
switch (DEFAULT_DATE_FORMAT) {
case "yyyy" . EW_DATE_SEPARATOR . "mm" . EW_DATE_SEPARATOR . "dd":
list($year, $month, $day) = $arDatePt;
break;
case "mm" . EW_DATE_SEPARATOR . "dd" . EW_DATE_SEPARATOR . "yyyy":
list($month, $day, $year) = $arDatePt;
break;
case "dd" . EW_DATE_SEPARATOR . "mm" . EW_DATE_SEPARATOR . "yyyy":
list($day, $month, $year) = $arDatePt;
break;
}
return trim($year . "-" . $month . "-" . $day . " " . $timePt);
} else {
return $dateStr;
}
}



Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.

PHP - Debug Trace function

function ewTrace($msg) {
$filename = "debug.txt";
if (!$handle = fopen($filename, 'a')) exit;
if (is_writable($filename)) fwrite($handle, $msg . "\n");
fclose($handle);
}


Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.

PHP - Trim function

function ewTrim($str) {
$temp = trim($str);
if (strtolower($str) == "null") return "";
if (substr($temp, 0, 1) == "`" && substr($temp, -1, 1) == "`") {
$temp = substr($temp, 1, -1);
} elseif (substr($temp, 0, 1) == "'" && substr($temp, -1, 1) == "'") {
$temp = substr($temp, 1, -1);
$temp = (get_magic_quotes_gpc()) ? stripslashes($temp) : $temp;
}
return $temp;
}


Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.

PHP - Format Currency function

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);
}



Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.

PHP - Date difference function

function DateDiff($interval, $date1, $date2)
{
// Function roughly equivalent to the ASP "DateDiff" function

//convert the dates into timestamps
$date1 = strtotime($date1);
$date2 = strtotime($date2);
$seconds = $date2 - $date1;

//if $date1 > $date2
//change substraction order
//convert the diff to +ve integer
if ($seconds < tmp =" $date1;" date1 =" $date2;" date2 =" $tmp;" seconds =" 0-$seconds;" interval ="="'y'" interval="="'m')" date1 =" date(" date2=" date(" time1 =" (date('H',$date1)*3600)" time2 =" (date('H',$date2)*3600)" diff =" $year2"> $month2) {
$diff -= 1;
} elseif($month1 == $month2) {
if($day1 > $day2) {
$diff -= 1;
} elseif($day1 == $day2) {
if($time1 > $time2) {
$diff -= 1;
}
}
}
break;
case "m":
list($year1, $month1, $day1) = split('-', $date1);
list($year2, $month2, $day2) = split('-',$date2);
$time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) + (date('s',$date1));
$time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) + (date('s',$date2));
$diff = ($year2 * 12 + $month2) - ($year1 * 12 + $month1);
if($day1 > $day2) {
$diff -= 1;
} elseif($day1 == $day2) {
if($time1 > $time2) {
$diff -= 1;
}
}
break;
case "w":
// Only simple seconds calculation needed from here on
$diff = floor($seconds / 604800);
break;
case "d":
$diff = floor($seconds / 86400);
break;
case "h":
$diff = floor($seconds / 3600);
break;
case "i":
$diff = floor($seconds / 60);
break;
case "s":
$diff = $seconds;
break;
}

return $diff;
}

Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.