Randomly sort an array using PHP’s shuffle() function

If you ever need to randomise a PHP array, and want to do it in one line, use the shuffle() function. It’s especially useful when you just want to draw one array member randomly, like in a lottery or something.

$values = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 
shuffle($values); //randomise the array members
echo array_pop($values); //outputs a random number from $values

MySQL timestamp syntax quick reference

Quick code post with a really useful little function, all it does it return a MySQL timestamp value of ‘now’. It’s useful if you haven’t already committed the format to memory, which sadly I have already… but still, it’s good to have in a common function library.

function mysqlnow() {
    return date("Y-m-d H:i:s");
}

 

Get the first and last days of last month in PHP

Here’s a really simple way to get the first and last dates of last month in PHP using strtotime. This’ll also format the dates correctly to use in comparisons with MySQL ‘TIMESTAMP’ fields! Nifty eh?


$lastmonth = strtotime("last month");
$fromdate = date('Ym01 00:00:00',$lastmonth);
$todate = date('Ymt 23:59:59',$lastmonth);

echo($fromdate.' to '.$todate);