User Tools

Site Tools


geek:php

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).

<?php
function convert_british_date($date, $reverse = false) {
        if(!empty($date)) {
                if($reverse) {
                        // convert iso date to british date
                        $d = explode("-", $date);
                        $timestamp = mktime(0,0,0,$d[1],$d[2],$d[0]);
                        if(is_numeric($timestamp))
                                return date("d/m/Y", $timestamp);
                } else {
                        // convert british date to iso date
                        $d = explode("/", $date);
                        $timestamp = mktime(0,0,0,$d[1],$d[0],$d[2]);
                        if(is_numeric($timestamp))
                                return date("Y-m-d", $timestamp);
                }
        }
        return false;
}
?>

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.

<?php 
$count = 1;
foreach($array as $line) {
    echo ($count % 2 == 0) ? "<tr bgcolor='green'>" : "<tr bgcolor='white'>";
    // ...
    $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.

<?php
private function generate_uniq() {
        $values = array("0","1","2","3","4","5","6","7","8","9","a","b",
                        "c","d","e","f","g","h","i","j","k","l","m","n",
                        "o","p","q","r","s","t","u","v","w","x","y","z",
                        "A","B","C","D","E","F","G","H","I","J","K","L",
                        "M","N","O","P","Q","R","S","T","U","V","W","X",
                        "Y","Z");
 
        $string_length = 5;
 
        $size = sizeof($values);
 
        for($i=0;$i<$string_length;$i++) {
                $randval[$i] = $values[rand(0, $size)];
        }
 
        $key = implode("",$randval);
 
        $sql = "select id  from `{$this->userTable}` where uniq_code = ?";
        $stmt->execute([$key]);
        $row = $stmt->fetch();
        if($row) {
                return generate_uniq();
        } else {
                return $key;
        }
}
?>
geek/php.txt · Last modified: 2020/06/01 10:48 by admin