[PHP] Automation

Joined
Jul 26, 2006
Messages
3,626
Reaction score
1,006
I have this project where I have to generate a new set of "shifts" every sunday. Right now I'm having the user do it manually by inputting the starting Sunday and then my script fills in until the next Saturday (e.g: user fills in "generate shifts from the [5th] of [June]" and then it'll generate shifts from the 5th (sun) until the 11th (sat) of June.

Shift generation:
PHP:
	function generateShifts($day_start, $month, $year)
	{
		global $months, $month_lengths, $days, $times, $positions;
		$f = 0;
		$day = $day_start;
		for($i = $day_start; $i < ($day_start + 7); $i++)
		{
			if($day > $months[$month-1]["length"]) //are we entering a new month?
			{
				$day = 1;
				$month++;
			}
			if($month > 12) //are we entering a new year?
			{
				$month = 1;
				$year++;
			}
			for($p = 0; $p < count($positions); $p++)
			{
				for($t = 0; $t < count($times); $t++)
					$sql = mysql_query("INSERT INTO `shifts` (position, day, month, year, time, full_day) VALUES ('". $positions[$p] ."', '". $day ."', '". $month ."', '". $year ."', '". $times[$t] ."', '". $days[$f] ."')");
			}
			$f++;
			$day++;
		}
	}

The shifts have certain properties such as position, day, month, year, time (e.g: 6AM-4PM, etc.) and a unique ID. Day, month and year are in numbers and are in D-M-YY format (e.g: 6-6-11). I have arrays with their names, short format like Jun, Jul, Aug, etc. (so for the 6th month I'd do $month_names[$row["month"]-1] (-1 because the array starts at index 0, of course) and that would get me "Jun".

Now I want to automate this process so a new week of shifts is generated automatically every Sunday. I can't use a cron job if that is an option at all. It needs to be pure HTML/JS/PHP.

I'm not asking for complete scripts but just for a point in the right direction.

Thanks!
 
Last edited:
Unless you will leave your browser running for whole week, you won't be able to do that automatically. Or you could just insert this code somewhere in your site, so users would execute it without event knowing (do not forget to add a check if you need to update those lists in this method).
 
Thanks, I knew that much but I'm not sure if there will be a user visiting every Sunday so how do I know whether a sunday has passed since the last visit?

edit;
I've got some more ideas brewing up

Also, you could look for "cron job" API providers (site would call your script on desired time).
 
Wonder why they never named it Chron?

Anyway, if chronological execution honestly is not a possibility, have it run the automatic part everytime someone looks at the shifts, or if you can, when someone clocks in. Just have a condition, if someday < today..


Anything that might conflict with the fact it's not up to date- should update it.

And why is this done in PHP, JS, HTML? I can think of at least 5 better tools than that without really waking up and having my coffee yet...
 
Last edited:
:P it just needs to be simple and straight, and compatible in 99% of all cases. So I decided that js/php/html was pretty standard, thus my choice.
Lol.. well I wouldn't call PHP, JS and HTML compatible in 99% of all cases, but ok. Maybe with your select cases, but not in general.. But ok.


I guess you need to leave a browser open and have JavaScript refresh every 5/10 minutes or so.. or whatever you want it to do... Or what I said before, or obviously the best choice:

Cron Jobs.. See Foxx's post for link..
 
Last edited:
I suppose I don't know as much as I thought I did, still learning.

Also, I was thinking way too complicated. Got something going now which will most likely do the job.

Thanks for the help guys.

edit:
done, lol im stoopid.
this was all i had to do...
PHP:
$sql = @mysql_query("SELECT * FROM `shifts` ORDER BY `id` DESC LIMIT 1");
$shift = @mysql_fetch_assoc($sql);
if($shift["day"] < date("j") && $shift["month"] < date("n") && $shift["year"] < date("y"))
{
	generateShifts($shift["day"]+1, $shift["month"], $shift["year"]);
	header("Location: index.php");
}
 
Last edited:
Back