<p>I need to rotate an image at 12 midnight every day from a group of 5-10 images. How can I go about doing this with JavaScript or jQuery or even PHP?</p>

[{'answer_id': 66449, 'author': 'The.Anti.9', 'author_id': 2128, 'author_profile': 'https://Stackoverflow.com/users/2128', 'pm_score': 0, 'selected': False, 'text': '<p>If you are running a linux system you can set a <a href="http://www.sitepoint.com/article/introducing-cron/" rel="nofollow noreferrer">Cron Job</a> or you can use the windows task scheduler if you are on windows</p>\n'}, {'answer_id': 66453, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 1, 'selected': False, 'text': "<p>Maybe I don't understand the question.<br />\nIf you just want to change the image write a batch file/cron job and have it run every day.<br />\nIf you want to display a certain image on Monday, and a different one of Tuesday then do something like this:\n<br /></p>\n\n<pre><code>&lt;?php\nswitch(date('w'))\n {\n  case '1':\n   //Monday\n   break;\n  case '2':\n   //Tuesday:\n   break;\n...\n}\n?&gt;\n</code></pre>\n"}, {'answer_id': 66454, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 1, 'selected': False, 'text': "<p>I'd do it on first access after midnight.</p>\n"}, {'answer_id': 66494, 'author': 'DamienG', 'author_id': 5720, 'author_profile': 'https://Stackoverflow.com/users/5720', 'pm_score': 3, 'selected': False, 'text': '<p>At a basic level what you want to do is define an array of image names then take the number of days from a given point in time then modulo (remainder after division) by the number of images and access that index in the array and set the image, e.g. (untested code)</p>\n\n<pre><code>var images = new Array("image1.gif", "image2.jpg", "sky.jpg", "city.png");\nvar dateDiff = new Date() - new Date(2008,01,01);\nvar imageIndex = Math.Round(dateDiff/1000/60/60/24) % images.length;\ndocument.GetElementById(\'imageId\').setAttribute(\'src\', images[imageIndex]);\n</code></pre>\n\n<p>Bear in mind that any client-side solution will be using the date and time of the client so if your definition of midnight means in your timezone then you\'ll need to do something similar on your server in PHP.</p>\n'}, {'answer_id': 66498, 'author': 'Hugh Buchanan', 'author_id': 9307, 'author_profile': 'https://Stackoverflow.com/users/9307', 'pm_score': 0, 'selected': False, 'text': "<p>You have two options.</p>\n\n<ol>\n<li><p>In JavaScript, you basically have it choose an image based on the day of the week or day of the month if you have more than 7 images.  Day of the month modulo by the length of the image array should let you pick the right array element.</p></li>\n<li><p>You need something a bit more stateful to track what's going on... using SQL you can track when images are used and pick from the rotating list.  You could also use a text file maintained by PHP to track the ordered list.</p></li>\n</ol>\n\n<p>The old school way is to have a cron job rotate the image, but I'd avoid that these days.</p>\n"}, {'answer_id': 66502, 'author': 'Jim', 'author_id': 8427, 'author_profile': 'https://Stackoverflow.com/users/8427', 'pm_score': 0, 'selected': False, 'text': '<p>That depends on how you are rotating them - sequentially, randomly, or what?</p>\n\n<p>There are a number of options.  You can determine which image you want in PHP, and dynamically change your <code>&lt;img&gt;</code> element to point to the correct location.  This is best if you are already generating your pages dynamically.</p>\n\n<p>You can always point to a single URL, which is a PHP file that determines which image to show and then performs a 302 redirect to it.  This is better if you have static pages that you don\'t want to incur the overhead of dynamic generation for.</p>\n\n<p>Don\'t make the image URL itself serve different images.  You\'ll screw up your cache hit ratio for no good reason and incur unnecessary overhead on what should be a static resource.</p>\n\n<p>Martin, "rotate" in this context means "change on a regular basis", not "turn around an axis".</p>\n'}, {'answer_id': 66545, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>It depends (TM) on what exactly you want to achieve. Just want to display a "random" image? Maybe this javascript snippet will get you started:</p>\n\n<pre><code>var date = new Date();\nvar day = date.getDate(); // thats the day of month, use getDays() for day of week\n\ndocument.getElementById(\'someImage\').src = \'/images/foo/bar_\' + (day % 10) + \'.gif\';\n</code></pre>\n'}, {'answer_id': 66558, 'author': 'Lucas Oman', 'author_id': 6726, 'author_profile': 'https://Stackoverflow.com/users/6726', 'pm_score': 1, 'selected': False, 'text': '<p>It doesn\'t even have to be in cron:</p>\n\n<pre><code>&lt;?php\n// starting date for rotation\n$startDate = \'2008-09-15\';\n// array of image filenames\n$images = array(\'file1.jpg\',\'file2.jpg\',...);\n\n$stamp = strtotime($startDate);\n$days = (time() - $stamp) / (60*60*24);\n$imageFilename = $images[$days % count($images)]\n?&gt;\n\n&lt;img src="&lt;?php echo $imageFilename; ?&gt;"/&gt;\n</code></pre>\n'}, {'answer_id': 66560, 'author': 'nickf', 'author_id': 9021, 'author_profile': 'https://Stackoverflow.com/users/9021', 'pm_score': 1, 'selected': False, 'text': '<p><em>Edit: I totally misread this question as "<strong>without</strong> using javascript/PHP". So disregard this response. I\'m not deleting it, just in case anyone was crazy enough to want to use this method.</em></p>\n\n<p>Doing it without Javascript, PHP, or any other form of scripting language could be difficult. Well actually, it would just be very contrived, since it would be trivial with even the most basic JS/PHP.</p>\n\n<p>Anyway, to actually answer your question, the only way I can think of doing it with vanilla HTML is to set up a shell script to run at midnight. That script would just rename your files. Do this with cron (on linux) or Windows Task Scheduler with a script kinda like this: <em>(dodgy pseudo code follows, convert to whatever you\'re comfortable with)</em>.</p>\n\n<pre><code>let number_of_files = 5\n\nrename current.jpg to number_of_files.jpg\n\nfor (x = 2 to number_of_files)\n    rename x.jpg to (x-1).jpg\n\nrename 1.jpg to current.jpg\n</code></pre>\n\n<p>In your HTML, just do this:</p>\n\n<pre><code>&lt;img src="path/to/current.jpg" /&gt;\n</code></pre>\n\n<p>And every day, current.jpg should change to something new. If you\'re using any sort of cache-control, make sure to change it so that it doesn\'t get cached for longer than a few hours.</p>\n'}, {'answer_id': 66576, 'author': 'Jimmy', 'author_id': 4435, 'author_profile': 'https://Stackoverflow.com/users/4435', 'pm_score': 0, 'selected': False, 'text': '<p>I assume by "rotate image" you mean "change the image in use" and not "rotational transformation about an axis" -- a simple way is to have a hash table that maps day modulo X to an image name.</p>\n\n<pre><code>$imgs = array("kitten.jpg", "puppy.gif","Bob_Dole.png");    \n$day_index = 365 * date("Y") + date("Z")\n\n...\n\n&lt;img src="&lt;? $imgs[$day_index % count($imgs)] ?&gt;" /&gt;\n</code></pre>\n\n<p>(sorry if I got the syntax wrong, I don\'t really know PHP))</p>\n'}, {'answer_id': 66725, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Set up a directory of images. \nSelect seven images and name them 0.jpg, 1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg, 6.jpg, \nUsing mootools Javascript framework with an image tag in HTML with id "rotatingimage":</p>\n\n<pre><code>var d=new Date();\nvar utc = d.getTime() + (d.getTimezoneOffset() * 60000);\nvar offset = -10;  // set this to your locale\'s UTC offset\nvar desiredTime = utc + (3600000*offset);\nnew dd = new Date(desiredTime); \n$(\'rotatingimage\').setProperty(\'src\', dd.getDay() + \'.jpg\'); \n</code></pre>\n'}, {'answer_id': 68281, 'author': 'nickf', 'author_id': 9021, 'author_profile': 'https://Stackoverflow.com/users/9021', 'pm_score': 0, 'selected': False, 'text': '<p>Here\'s a solution which will choose a random image per day from a directory, which might be easier than some other solutions posted here, since all you have to do to get something into the rotation is upload it, rather than edit an array, or name the files in arbitrary ways.</p>\n\n<pre><code>function getImageOfTheDay() {\n    $myDir = "path/to/images/";\n\n    // get a unique value for the day/year.\n    // 15th Jan 2008 -&gt; 10152008. 3 Feb -&gt; 10342008, 31 Dec -&gt; 13662008\n    $day = sprintf("1%03d%d", date(\'z\'), date(\'Y\'));\n\n    // you could of course get gifs/pngs as well.\n    // i\'m just doing this for simplicity\n    $jpgs = glob($myDir . "*.jpg");\n    mt_srand($day);\n    return $jpgs[mt_rand(0, count($jpgs) - 1)];\n}\n</code></pre>\n\n<p>The only thing is that there\'s a possibility of seeing the same image two days in a row, since this picks it randomly. If you wanted to get them sequentially, in alphabetical order, perhaps, then use this. <em>(only the last two lines are changed)</em></p>\n\n<pre><code>function getImageOfTheDay() {\n    $myDir = "path/to/images/";\n    $day = sprintf("1%03d%d", date(\'z\'), date(\'Y\'));\n    $jpgs = glob($myDir . "*.jpg");\n    return $jpgs[$day % count($jpgs)];\n}\n</code></pre>\n'}, {'answer_id': 68364, 'author': 'Kevin Haines', 'author_id': 10410, 'author_profile': 'https://Stackoverflow.com/users/10410', 'pm_score': 0, 'selected': False, 'text': "<p>How to rotate the images has been described in a number of ways. How to detect <i><b>when</b></i> to trigger the rotate depends on what you are doing (please clarify and people can provide a better answer).</p>\n\n<p>Options include:</p>\n\n<ol>\n<li>Trigger the rotate on 'first access after midnight'. Assumes some sort of initiating event (eg user access)</li>\n<li>Use the OS scheduling capabilities to trigger the rotate (cron on *nix, at/task scheduler on Windows)</li>\n<li>Write code to check time &amp; rotate</li>\n</ol>\n\n<p>Option 3 has the risk that a poorly coded solution could be overly resource intensive.</p>\n"}]
['https://Stackoverflow.com/questions/66422', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9750/']