Image of the Month – PHP Snippet

Today I came across this blog post. It’s a PHP snippet for having a different image be displayed each month. I felt that it was lacking so I decided to do my own version of it. Don’t have a need for it but something triggered me to work on my own version.

The first thing that I did was set up my images array to handle cases where you want a specific image to be shown on a specific date. This is good for holidays and special dates.

// These are the monthly images.
$months = array(
	1 => 'image1.png',
	2 => array(
		0 => 'image2.jpg',
		14 => 'love.png',
	),
	3 => 'image3.jpg',
	4 => 'image4.gif',
	5 => 'image5.png',
	6 => 'image6.png',
	7 => 'image7.png',
	8 => 'image8.png',
	9 => 'image9.png',
	10 => 'image10.png',
	11 => 'vacationimages/motm1.png',
	12 => array(
		0 => 'winter.png', // Default image for the month.
		25 => 'x-mas.png', // Image for that day.
	),
);

If you want to have a static image all you have to do is supply that month with the filename of the image, the name can also be a url. Now if you want to have different images for specific dates the you need to supply an array with the dates for that month. For example:

	12 => array(
		0 => 'winter.png', // Default image for the month.
		25 => 'x-mas.png', // Image for that day.
	),

The 0 index is the default image that will be used for that month and the 25 index is the image that will be used December 25th.

Here is the full snippet. You can use as you please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
< ?php
/**
 * Image of the month.
 *
 * This is a small function that will get the filename of the image of the month.
 * It also handles displaying a different image for holidays.
 *
 * @version 		0.1
 * @author		Juan "JayCreations" Hernandez
 * @license		MIT License
 * @copyright		Juan "JayCreations" Hernandez
 */
 
function ImageOfTheMonth()
{
	// These are the monthly images.
	$months = array(
		1 => 'image1.png',
		2 => array(
			0 => 'image2.jpg',
			14 => 'love.png',
		),
		3 => 'image3.jpg',
		4 => 'image4.gif',
		5 => 'image5.png',
		6 => 'image6.png',
		7 => 'image7.png',
		8 => 'image8.png',
		9 => 'image9.png',
		10 => 'image10.png',
		11 => 'vacationimages/motm1.png',
		12 => array(
			0 => 'winter.png', // Default image for the month.
			25 => 'x-mas.png', // Image for that day.
		),
	);
 
	// Get the current month and day.
	$curMonth = date('m');
	$curDay = date('j'); // We need the date without the leading zero.
 
	// The image.
	$image = '';
	// First we try to find the special months.
	// If it's set and it's an array then check for today's date.
	if (isset($months[$curMonth]) && is_array($months[$curMonth]) && isset($months[$curMonth][$curDay]))
		$image = $months[$curMonth][$curDay];
	// So we at the month but no image for today's date? Lets use the default image for that month.
	elseif (isset($months[$curMonth]) && is_array($months[$curMonth]))
		$image = $months[$curMonth][0];
	// Ok so just a regular month?
	elseif (isset($months[$curMonth]) && is_string($months[$curMonth]))
		$image = $months[$curMonth];
	// Nothing yet? Ok just get the first image you can find.
	else
	{
		$current = current($months);
		$image = is_string($current) ? $current : $current[0];
	}
 
	// Return the image.
	return $image;
}
 
// Example usage.
$getImage = ImageOfTheMonth();
echo '<img src="' . $getImage . '" />';
 
?>

3 thoughts on “Image of the Month – PHP Snippet”

  1. The database sanitization snippet seems a little dated as it contains both get_magic_quotes_gpc and mysql_real_escape_string which have been deprecated for some time. (The source was from 2010) Other than that its a good starting point, – just not really useful in a production environment.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>