Month: March 2011

Converting a DATETIME variable into a UNIX timestamp

Here’s how:

/**
 * convert a datetime value (Y-m-d H:i:s) to UNIX timestamp value
 * @param datetime $datetime
 * @return timestamp
 */
function datetime_to_timestamp($datetime) {
    list($date, $time) = explode(" ", $datetime);
    list($year, $month, $day) = explode("-", $date);
    list($hour, $minute, $second) = explode(":", $time);
    return mktime($hour, $minute, $second, $month, $day, $year);
}

Using subkey to sort an array in PHP

It’s a bit tricky to sort multidimensional arrays in PHP by one of array’s subkeys, but it can absolutely be done. Check it out:

/**
 * USING POINTER TO ARRAY - sort an array of arrays with associative indeces by one of those associative values
 * @param $array pointer to our array
 * @param $subkey associative index that we want to order by
 * @param $sort_type [SORT_ASC|SORT_DESC]
 * @return void
 */
function sortBySubkey(&$array, $subkey, $sort_type = SORT_ASC) {
   foreach ($array as $subarray) {
      $keys[] = $subarray[$subkey];
   }
   array_multisort($keys, $sort_type, $array);
}

$1,000,000 a year – how difficult is that, really?

If you haven’t heard of David Heinemeier Hansson yet, you should check out the presentation he gave at Startup School ’08. It’s an amazingly motivational video and it got me thinking …

If you want to earn $1,000,000 a year, you only need 2,000 subscribers for your $40-a-month service. How difficult is it really to get 2,000 users for your high quality online service? Well, 2,000 people is .00011765% of people who use the internet. In other words, you need to have one paying subscriber in every city with 850,000 residents. Seems doable? 🙂

Creating absolutely safe strings

Sometimes you are given a string (someone’s name, or some title, or something completely different) and you cannot afford to use this string if you’re not completely sure what to expect. For instance, sometimes you need to convert a string into something that consists only of alphanumerical characters and spaces. You can always use the following function:

/**
 * create a string that only consists of alphanumerical characters and spaces
 * @param $string
 * @return string
 */
function createSafeString($string) {
	$string = strtolower($string);
        $output = "";
	for ($i = 0; $i < strlen($string); $i++) {
		$ord = ord($string[$i]);
		if (($ord >= 48 && $ord <= 57) || ($ord >= 65 && $ord <= 90) || ($ord >= 97 && $ord <= 122) || ($ord == 32)) {
			$output .= $string[$i];
		}
	}
	return $string;
}

Don’t manually escape all your variables

Let’s start with something fairly simple, shall we?

I’ve seen way too many programmers escaping all $_POST and $_GET variables like this:

$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);

I suggest you simply use something like this from now on:

$post = array();
$get = array();
foreach ($_POST as $key => $value) {
	$post[$key] = escape_string($value);
}
foreach ($_GET as $key => $value) {
	$get[$key] = escape_string($value);
}
/**
 * escape given variable so we can use it in an SQL query
 * @param anything $value
 * @return anything $escaped_value
 */
function escape_string($value) {
	if (get_magic_quotes_gpc()) {
		$value = stripslashes($value);
	}	
	if (!is_numeric($value)) {
		$value = mysql_real_escape_string($value);
	}	
	return $value;
}

This way you can always use $post instead of $_POST and $get instead of $_GET if you need escaped values. And you can always access the original values in the original $_POST and $_GET variables. Pretty neat, huh? 🙂