BACKGROUND: I’m currently working on building a photography gallery where I release a new image every 2 hours. As I process and upload each image and store their information in the database, I run a check to see if I currently have images queued for future release. This check is exemplified below.
$last_added = mysql_query("SELECT date FROM images ORDER BY date DESC");
$last_added = mysql_fetch_row($last_added);
$last_added = strtotime($last_added[0]);
if($last_added < time() - 7200):
$time = time();
else:
$time = $last_added + 7200;
endif;
$timestamp = date( 'Y-m-d H:i:s', $time );
mysql_query("INSERT INTO images (name,date,artist,tags,id) VALUES ('".$name."','".$timestamp."','".$artist."','".$tags."','".$id."')");
Basically it grabs the image with the highest date and checks to see if it is in the past or in the future. If it’s in the past, it tags the newest image with the current timestamp, if it is not in the past, it tags the newest image with a timestamp 2 hours after the highest image already in the database.
I have it set that way because I don’t have time to add new images every single day and this allows them to be time released keeping the site alive and updated while I am away.
QUESTION: However, when I do have time, I would like to add more images and instead of having them be added 30 days in the future, I would like to decrease the interval from 2 hours to 1 hour…then possibly from one hour to 30 minutes, etc.
Is there a convenient way to first check the highest dated image (like perhaps today + 30 days), and then have it check to begin filling in the interval gaps so that if the highest timestamp meets our criteria the next image added will get added 1 hour after the next image that is about to be released (filling the gap between it and the one that would be released 2 hours later). Then the image after that would be added 1 hour after the second future queued image filling that interval gap, etc.
The goal is that I can just keep tagging and adding images and it automatically keeps me queued for the future and automatically decreases the interval based on how far into the future I’m already queued up.
If I’ve not been specific enough, please let me know. I’m happy to clarify if needed.
Sure. Here’s what I would do:
First, create two columns… an “added” datetime, and a “posted” datetime.
Added would be the datetime you put them in your database, and posted would be the datetime they appear live in a post on your site.
Determine a minimum and maximum number of images to post per day. So if you had a minimum of 1 per day, and a maximum of 12, you want your script to consider 7 days in advance, and you have 90 images ready to go, then you want the script to place 84 images, one every 12 hours. If instead you had 42 images ready, the script would place all 42, at an interval of 4 hours. If instead you only had 3 images ready, the script would place 3 at an interval of 1 day apart each. Make sense?
Create a script that runs in a cron job every hour or so… whatever you feel is necessary. In this script do the following:
SELECT COUNT(*) FROM images WHERE added IS NULL;(This determines how many images haven’t been placed that you have to work with.)SELECT MAX(posted) FROM images;(This determines the last time we posted an image.)(7/imagecount)*24. If this is greater than 24 (meaning, 24 hours per image) then, we use 24. If this is less than 2 (meaning, 2 hours per image), then use 2 instead.UPDATE images SET posted=NOW() WHERE id=12345. Otherwise, do nothing.So, this script will run every hour, and check if it is time to post a new image. If it isn’t, it does nothing. If it is, it will post only one. On the next hour, it will recalculate, thus factoring in any new images or conditions.
I hope this flow makes sense. There are undoubtedly other ways to solve this problem, but I think this is closest to what you are looking for.