Tuesday, August 4, 2009

PHP - Get a list of a particular day of the week between two dates

This is a modified version of function in the previous post. I had to modify it for a project I was doing.


****************************************************************************


function dayOfWeekListDateRange($start, $end, $day_of_week)

{
$range = array();

if (is_string($start) === true) $start = strtotime($start);
if (is_string($end) === true ) $end = strtotime($end);

if(!empty($day_of_week))
{
do
{
if(strcmp(strtolower(strftime("%A",$start)),strtolower($day_of_week)) == 0)
{
$range[] = date('Y-m-d', $start);
$start = strtotime("+ 1 day", $start);
}

else $start = strtotime("+ 1 day", $start);

}

while($start <= $end);
}

else
{

do
{

$range[] = date('Y-m-d', $start);
$start = strtotime("+ 1 day", $start);

}

while($start <= $end);
}

return $range;
}

PHP - Get a range of dates between two dates

function dateRangeArray($start, $end)
{
$range = array();

if (is_string($start) === true) $start = strtotime($start);
if (is_string($end) === true ) $end = strtotime($end);

do {
$range[] = date('Y-m-d', $start);
$start = strtotime("+ 1 day", $start);
} while($start <= $end);

return $range;
}

Credit: http://boonedocks.net/mike/archives/137-Creating-a-Date-Range-Array-with-PHP.html

PHP - Serialize an array for use in a MySQL table

This is the function I made to "serialize" a PHP array for dumping into a MySQL table into a readable, searchable format. It separates each value with an uncommon character , "|".

When you grab the "serialized" data out of the DB later, you should run the following to convert it back to an array --

$values_array = explode("|", $seralizedValues);

I'm aware that there is a PHP function to serialize data, but I wanted to make my own simpler, easier version that did not insert the array keys or any other junk into the array. Take it to leave it.

*******************************************************

function serializeArray($arr)
{
$temp = "";

if(!empty($arr))
{
$iter = 0;
$arr_size = count($arr);

foreach ($arr as $item)
{
$iter++;
$temp.=$item;
if($iter!==$arr_size) $temp.="|";

}
}

return $temp;
}

PHP - Format a variable for a MySQL query

This is my personal, very simple function I use to get a variable ready for use in a MySQL query.

*****************************************************

function sqlformat($var)
{
return "'".addslashes(trim($var))."'";
}

PHP - Find min and max values in an array

/**
signature
array getMinMax( array )

returns an associative array with:
'min' -> smallest value in the given array
'max' -> greatest value in the given array

if the array is empty both fields are NULL
*/
function getMinMax($array)
{

reset($array);
if(
FALSE === key($array))
{
return array(
'min' => NULL, 'max' => NULL);
}

$min = $max = current($array);
$val = next($array);

while(
NULL !== key($array))
{
if(
$val > $max)
{
$max = $val;
}
elseif(
$val < $min)
{
$min = $val;
}
$val = next($array);
}
return array(
'min' => $min, 'max' => $max);
}


Usage:

$min_max_arr = getMinMax($myarray);

$min_val = $min_max_arr['min'];
$max_val = $min_max_arr['max'];

(found here -- http://www.codingforums.com/showthread.php?t=130494)

Example application -- If you had an array of dates, you could turn each entry into a timestamp, run the array of timestamps through this function, and get the minimum and maximum timestamps. Then you could simply convert the min and max timestamps back to a string (using something like strftime).