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 & lt;= $end_day; $i ++) {
		$days_of_week[] = date( "l", mktime( 0, 0, 1, $month, $i, $year ) );
	}

	// return array
	return $days_of_week;
}

Update: 13/05/2018

Sigh What was wrong with me back then, this would have been so much better.

function days_of_week(){
	return = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
}

Keep it simple, stupid!


Posted

in

by

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.