====== PHP Code ======
===== Convert a British date =====
Converting British dates to the ISO standard and back again.
The very useful strtotime() function works on pretty much everything except the British date format. This function works around that. i.e. 31/01/2010 becomes 2010-01-31. With the 'reverse' parameter set to true it converts an ISO date to the British format (mainly for display purposes).
You shouldn't ever need this SQL, but I found a need when I inherited a terribly designed database. It will convert a british textfield date, to an ISO date.
SELECT str_to_date(baddatecolumn, '%d/%m/%Y') AS iso_date FROM tablename
===== Highlight alternate rows =====
Highlighting alternating rows for readability.
Pretty simple, using the tertiary operator and modulus to highlight the even rows in a list.
" : "";
// ...
$count++;
}
?>
===== Generate a unique identifier =====
Recursive function to generate a unique user identifier.
This example is a function taken from my user management class. I try to use unique random user ids in my web applications (to stop people iterating through users, public listings etc by manipulating the URLs). The function generates a string, checks it isn't already in the database and returns it, otherwise it calls itself again.
It has a PDO database object available to it.
userTable}` where uniq_code = ?";
$stmt->execute([$key]);
$row = $stmt->fetch();
if($row) {
return generate_uniq();
} else {
return $key;
}
}
?>