We do allot of eccommerce development and decided early on that the best way to avoid strange rounding errors from either PHP or MySQL is to store all prices as cents. These two functions help manipulate that data whether you’re taking values from the user (like in an admin panel) or pushing values out to the screen:


function dollars2cents($float) {
	$number = str_replace(',', '', $float);
	$cents = (string)($number*100);
	return (int)$cents;
}

function cents2dollars($int) {
	return number_format(($int/100), 2);
}

Another of our conventions is to only ever store epoch timestamps in the database when dealing with dates. We’ve found timestamps to be much easier to work with compared to strings or native mySQL date types. The first function takes a timestamp and returns a formatted date and the second does the reverse, it takes formatted date and returns the epoch timestamp.


//takes an epoch timestamp
//returns formatted date
function stamp2date($stamp) {
	return date('d/m/Y', $stamp);
}

//takes date string as dd/mm/yy or dd-mm-yy
//returns epoch timestamp or 0 if date is not valid
function date2stamp($ddmmyy) {
	$ddmmyy = (!$ddmmyy)? date('d/m/y'):$ddmmyy;
	$date = explode('/', $ddmmyy);
	if (count($date) != 3) {
		//try with '-'
		$date = explode('-', $ddmmyy);
		if (count($date != 3)) {
			//doesn't appear to be valid ddmmyy
			return 0;
		}
	}
	if(checkdate($date[1], $date[0], $date[2])) {
		$stamp = gmmktime(0,0,0,$date[1], $date[0], $date[2])-35999;
	} else {
		//not valid date
		$stamp = 0;
	}
	return $stamp;
}

The next two functions output a niceley formatted version of print_r which is easy to read for debugging.


	function niceLive($input) {
		echo "<pre>".print_r($input)."</pre>";
	}

	function niceDie($input) {
		niceLive($input);
		exit;
	}

These three functions are great for doing those repetitive tasks like outputting links or values like status’ or states


//takes a string with words seperated by -
//reurns string without hypen and first letter of each in uppercase
function hyphenToCapitalSpace($string) {
	return ucwords(strtolower(str_replace('-', ' ', $string)));
}

//takes a string with words seperated by _
//reurns string without hypen and first letter of each in uppercase
function underscoreToTitleSpace($string) {
	return ucwords(strtolower(str_replace('_', ' ', $string)));
}

//takes a string
//returns the string with - between words and parses out characters unsuitable for url links
function toLink($name) {
	$name = str_replace('&','and',$name);
	$name = iconv("UTF-8", "ASCII//TRANSLIT", $name);
	$link = preg_replace('/[^A-Za-z0-9-]/','',str_replace(' ','-',$name));	
	while(strstr($link, '--')) {
		$link = str_replace('--', '-', $link);
	}
	if (substr($link, -1) == '-') {
		$link = substr($link, 0, -1);
	}
	return $link;
}

The last function is used for highlighting text in a search:


//takes string of content and array of search terms
//returns the content with swearch terms wrapped in strong tags
function highlight($content, $search_terms) {
	foreach ($search_terms as $search_term) {
		$content = preg_replace('/(' . $search_term . ')+/i', '${1}', $content);
	}
	return $content;
}


We'd love to
hear from you

We've prepared a simple project planner to get started.

Lets get started

Or send us a
message

Phone: (07) 3878 1151

Address: Office 2, 59 Hardgrave Rd,
West End, Brisbane, 4101, QLD, Australia

  • This field is for validation purposes and should be left unchanged.
css.php