Tag: days of the week

Create an array containing the days of the week

You could simply code:

$days_of_week = array(‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’, ‘Sunday’);

but thats the “noob” way, a real coder creates a function…

function days_of_week(){
// declare days_of_week array
$days_of_week = array();

// get current date integer value (0 – 6)
$day = date(“w”);

// get current date values
$today = date(“j”);
$month = date(“n”);
$year = date(“Y”);

// get start day and end day of the week
$start_day = $today – $day;
$end_day = $start_day + 6;

// build days of the week array
for ($i = $start_day; $i <= $end_day; $i++) {
$days_of_week[] = date(“l”, mktime(0,0,1,$month,$i,$year));
}

// return array
return $days_of_week;
}

Filed under: DevelopmentTagged with: , ,