Sometimes UNIX timestamps can be easier to manage than MySQL datetimes. Here's a function from converting one to the other.
This function should REALLY come in handy for a lot of folks (heavy sarcasm). It calculates all size 2 subsets of an array whose values are unique, and in no particular order. So, passing it an array of [1,2,3] will return [[1,2],[1,3],[2,3]]. Passing it an array of [1,2,3,2,1,2,3] will return exactly the same thing. Go wild!
{
$subsets = array();
// if the array only contains two or less element, just return it
if (count($array) <= 2)
return $array;
for ($i=0; $i<count($array); $i++)
{
$curr_el1 = $array[$i];
for ($j=0; $j<count($array); $j++)
{
$curr_el2 = $array[$j];
if ($curr_el1 == $curr_el2)
continue;
// if an element of [$curr_el1, $curr_el2] and [$curr_el2, $curr_el1] doesn't exist,
// add if to $subsets
if (!in_array(array($curr_el1, $curr_el2), $subsets) && !in_array(array($curr_el2, $curr_el1), $subsets))
$subsets[] = array($curr_el1, $curr_el2);
}
}
return $subsets;
}
A function written by Julius Davies, an old friend of mine who's actually responsible for getting me into programming in the first place. He's posted a demonstration of this function on his site, here.
Takes a string (hopefully containing digits), strips
out all garbage, and returns it as a nicely formatted
phone number.
cleanPhoneString("6042513219") returns: "604-251-3219"
cleanPhoneString("60425132191234") returns: "604-251-3219 x 1234"
cleanPhoneString("6a0b4c2d5e1f3g2h1i9") returns: "604-251-3219"
This method is meant to be used hand-in-hand with "isValidPhoneString()".
Example usage:
$cleanPhone = cleanPhoneString( $str );
if ( !isValidPhoneString( $cleanPhone ) )
{
$cleanPhone = "invalid!";
}
*/
function cleanPhoneString( $phoneStr )
{
$len = strlen( $phoneStr );
$buf = "";
for ( $i = 0; $i <$len; $i++ )
{
$c = $phoneStr{$i};
if ( ctype_digit( $c ) )
{
$buf = $buf.$c;
}
}
$phoneStr = $buf;
$len = strlen( $phoneStr );
$buf = "";
for ( $i = 0; $i <$len; $i++ )
{
$c = $phoneStr{$i};
if ( ctype_digit( $c ) )
{
if ( $i == 3 || $i == 6 )
{
$buf = $buf."-";
}
else if ( $i == 10 )
{
$buf = $buf." x ";
}
$buf = $buf.$c;
}
}
return $buf;
}
/*
Tests to make sure string is a valid phone number. You should first
run your string through cleanPhoneString() first, since isValidPhoneString()
depends on the formatting provided by cleanPhoneString().
*/
function isValidPhoneString( $phoneStr )
{
$l = strlen( $phoneStr );
$c = "";
if ( $l>= 1 )
{
$c = $phoneStr{0};
}
// Must be at least 12 digits (xxx-xxx-xxxx), and must
// not start with 0 or 1. Must not be more than 20 digits
// (xxx-xxx-xxxx x xxxxx).
return $l>= 12 && $l <= 20 && $c != '0' && $c != '1';
}
I can't believe PHP doesn't have a built-in function for this...! Code snippet for removing a single index from an array (non-associative).
A catch-all function to check that a folder is able to handle file uploads. In addition to the standard check that the folder specified exists, is readable and is writable, it also checks your server's PHP installation to confirm that the temporary upload folder is in fact valid. Up until this point I've always naively assumed that the smart folks who set up servers know to configure that value properly. But a couple of chaps on the Form Tools forums found that their installations of PHP had invalid settings - or weren't set at all. Doh!
Function: check_upload_folder
Description: examines a folder to check (a) it exists and (b) it has correct permissions.
This function also checks to see if the upload_temp_dir server setting has been
configured.
Parameter: the full path to the folder to be examined.
\*------------------------------------------------------------------------------------------------*/
function check_upload_folder($folder)
{
// first, check server's temporary file upload folder
$upload_tmp_dir = ini_get("upload_tmp_dir");
if (empty($upload_tmp_dir))
return array(false, "Your server's installation of PHP doesn't appear to have the
<b>upload_tmp_dir</b> setting configured. This setting determines where files
are temporarily uploaded to before they are moved to the folder you are specifying
here. This value needs to be set in order to allow this program to properly upload
files. Please contact your hosting provider.");
if (!is_dir($folder))
return array(false, "Your server's installation of PHP has an invalid setting for
the <b>upload_tmp_dir</b> value. \"$upload_tmp_dir\" is not a valid folder.");
if (!is_writable($folder))
return array(false, "This temporary upload folder specified by your PHP installation
is not writable. Until this is fixed, files cannot be uploaded through any PHP program
on your server. Please contact your hosting provider.");
// now check the folder specified by
if (!is_dir($folder))
return array(false, "This is not a valid folder.");
if (!is_writable($folder))
return array(false, "This folder is not writeable.");
return array(true, "This folder has the correct permissions.");
}
Helpful wrapper function used in my Form Tools script for converting a MySQL datetime into a human readable format. Includes the option of specifying hour offset. It gets pretty annoying having to rethink this problem every time you need it, so here it is for reference.
Function: get_date
Description: helper function to return a date according based on an offset and a display format.
Parameters: $offset - the GMT offset
$datetime - the mysql datetime to format
$format - the format to use (PHP's date() function).
\*-----------------------------------------------------------------------------------------*/
function get_date($offset, $datetime, $format)
{
$year = substr($datetime,0,4);
$mon = substr($datetime,5,2);
$day = substr($datetime,8,2);
$hour = substr($datetime,11,2);
$min = substr($datetime,14,2);
$sec = substr($datetime,17,2);
return date($format, mktime($hour + $offset, $min, $sec, $mon, $day, $year));
}
Next Page »








