Tuesday, January 24, 2012

Turn a word into an array in PHP

Was mucking around with creating a little word game the other day and needed to turn a string in to an array, this is how it was done:

$word = 'words';
$len = strlen($word);
$exploded_word = array();

for($i = 0; $i < $len; $i++) {
    $exploded_word[] = $word[$i];
}
Would produce:
Array
(
    [0] => w
    [1] => o
    [2] => r
    [3] => d
    [4] => s
)

Labels: ,

Monday, January 9, 2012

Looping through select fields and getting the selected text using jQuery

I spent a little time wrestling with this and I thought it may come in useful to pop on to the blog. In my situation I had to loop through a number of select items as I needed to post some data. I needed to do this as I had to pass 3 sets of information from each select / drop down box. It had to be done this way because I was using the dropdown boxes for a number of different functions, some needed to pull the text and some needed to pull the value.
  • The name of the select box
  • The value of the select box
  • The text of the selected item
$('select').each(function(){
     var selected_text = $(this).children('option:selected').text();
})
Happy programming :)

Labels: , ,

Thursday, January 5, 2012

Work out the days between two dates using PHP

I've popped this up as I needed to work out the number of dates in between two dates. Again something similar might be out there, but I thought it may be useful for any one out there that may need it.

Parameters

Required - date1 and date2: Dates in the following format yyyy-mm-dd
Optional - inclusive: If you set to true it includes the two provided dates in it's calculation
Optional - return format of the dates: Combination of PHP's date formatting options

Return Values

Returns an array of dates and the number of days

The Code

/**
    * Get the difference between two dates...
    * @param string $date1 yyyy-mm-dd
    * @param string $date2 yyyy-mm-dd
    * @param boolean $inclusive
    * @param string $return format
    *
    * @return array of total number of days and the dates in the prescribed format
*/

function days_between($date1, $date2, $inclusive = true, $return_format = 'Y-m-d') {
       
        $date_array = array();
        $output_array = array();
       
        $date1_ts = strtotime($date1);
        $date2_ts = strtotime($date2);
        $second_difference = $date2_ts-$date1_ts;
        $num_days = floor($second_difference / 86400);
     
        if (is_numeric($num_days) && $num_days > 0) {
         
            for ($i = 0; $i <= $num_days; $i++) {
                $new_day = $date1_ts + ($i*86400);
                $date_array[] = date($return_format,$new_day);
            }

            if (!$inclusive) {
                unset($date_array[0]);
                array_pop($date_array);
            }
         
            $output_array['number_of_days'] = sizeof($date_array);
            $output_array['dates_between'] = $date_array;  
         
        }

        return ($output_array);
     
}    
Usage

days_between('2011-12-31', '2012-01-05', 1,'d M Y');

Labels: ,