Archive for June, 2009



Jun 18th

PHP Build Array of Years and Months Between Two Dates

On another recent project I had to generate monthly reports for the clients, to allow them to select the desired report I decided on giving them two HTML select inputs, the first for year and second for month. I tied the two fields together via ajax.

To populate these select boxes I wanted to build an multi-dimensional array of dates organized by year and then by month where the first date or month available is the date the client’s account was created (a date from the database) and the last date or month to be the current.

I build the following function to build my array of years and months, it is dependent upon another function posted on this site that builds an array of individual dates. These could be combined into one ‘SUPER FUNCTION’ but there was no need as the project required both functions discretely anyway.

The function requires two input options, a starting date and an ending date. These dates must be in the ‘0000-00-00′ format.

The result of:

$start = '2007-05-12';
$end = '2009-06-18';
print_r( year_month_array( $start, $end ) );

Would be:

Array
(
    [2007] => Array
        (
            [0] => May
            [1] => June
            [2] => July
            [3] => August
            [4] => September
            [5] => October
            [6] => November
            [7] => December
        )

    [2008] => Array
        (
            [0] => January
            [1] => February
            [2] => March
            [3] => April
            [4] => May
            [5] => June
            [6] => July
            [7] => August
            [8] => September
            [9] => October
            [10] => November
            [11] => December
        )

    [2009] => Array
        (
            [0] => January
            [1] => February
            [2] => March
            [3] => April
            [4] => May
            [5] => June
        )

)
function year_month_array($from, $to)
{
	$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	$dates = array_values(array_unique(get_date_range_array($from, $to, 'Y-n')));
	$data = array();
	foreach ( $dates as $date )
	{
		$date = explode('-', $date);
		$data[$date[0]][] = $months[$date[1]-1];
	}
	return $data;
}
Jun 18th

PHP Build Date Range Array

On a recent client project I was tasked with creating a chart of daily revenue from sales of, lets say, widgets. The first step to building this was to build an array of dates between two dates. I found a great little function from Mike’s Page, www.boonedocks.net that did almost exactly what I needed it to do. I need to use a few different date formats so I modified it slightly and I am re-posting it here for your use.

This function accepts (3) options, the first is the starting date which needs to be in the format ‘0000-00-00′. The second is the ending date which also needs to be in the format ‘0000-00-00′ and lastly the third is the out put date format, examples and ideas can be found in the PHP manual PHP: date – Manual.

My input dates were already in the ‘Y-m-d’ format so I didn’t bother changing this, but one could utilize the strtotime() function to make these two function input options variable.

The result is an array of dates in whichever format you define.

function get_date_range_array( $date_from, $date_to, $format = 'Y-m-d' )
{
	$date_array = array();
	$i_date_from = mktime(1, 0, 0, substr($date_from, 5, 2), substr($date_from, 8, 2),substr($date_from, 0, 4));
	$i_date_to = mktime(1, 0, 0, substr($date_to, 5, 2), substr($date_to, 8, 2),substr($date_to, 0, 4));
	if ( $i_date_to >= $i_date_from )
	{
		array_push($date_array, date($format, $i_date_from)); // first entry
		while ( $i_date_from < $i_date_to )
		{
			$i_date_from += 86400; // add 24 hours
			array_push($date_array, date($format, $i_date_from));
		}
	}
	return $date_array;
}
Jun 18th

PHP Random Password Function

Anyone who has ever dealt with user authentication knows how important a strong password is, this is a very simple, and small function that will generate a password of variable length and complexity. The function accepts two options; the first, length is self explanatory. This is the length of the password you wish to generate, a common length is 6-8. The second is the complexity of the password, this involves the possible characters from which the password will be generated. I designed this to accept values 1-4 (any other value will default to 4) where 1 is the weakest and 4 the strongest.

The function returns a generated password.

function generate_password ( $length = 8, $complexity = 3 )
{
	$password = '';
	switch ( $complexity )
	{
		case 1:
			$possible = '0123456789';
			break;
		case 2:
			$possible = '0123456789bcdfghjkmnpqrstvwxyz';
			break;
		case 3:
			$possible = '0123456789bcdfghjkmnpqrstvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ';
			break;
		case 4:
		default:
			$possible = '0123456789bcdfghjkmnpqrstvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ@#$%&!?^~';
			break;
	}
	$i = 0;
	while ( $i < $length )
	{
		// pick a random character from the possible ones
		$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
		// we don't want this character if it's already in the password
		if ( !strstr($password, $char) )
		{
			$password .= $char;
			$i++;
		}
	}
	return $password;
}