I am working on an intranet web application using PHP/MySQL. My question is about scheduled events: I have 2 important dates per year (let me call them date_01 and date_02) w开发者_JAVA技巧here the system has to change the start-time of check-in for the employees. For example, at date_01 check-in starts at x-time while at date_02 check-in starts at y-time. It is like summer check-in time is different than winter check-in time ...
From software engineering point of view, what do you think is the best way of achieving this?
-> Cron job? -> MySQL scheduled events? -> A certain PHP trick if possible? -> Your suggestions?Thanks
I think you don't need scheduled tasks at all. You can have a table like this:
check_ins (valid_from, valid_until, check_in_start_time)
And then do something like this when you need to get current check-in start time in your web application:
select check_in_start_time
from check_ins
where
now() >= valid_from and
now() < valid_until
I usually do that kind of tast with the system's cron
Make a file with the classic mark for external interpreter, then the php code inside tags
#!/usr/bin/php -f
<?php
function hello_world(){ echo "hi!"; }
hello_world();
?>
Then make it executable with chmod a+x my_php_files_with_commands.php and add an entry to the cron for the desired schedule. You get instant access to your application's php functions, just add the require() calls you need and play arround with it. Database access, file access, whatever... except (obviously) anything that has to do with sessions. Carefull with that.
EDIT: for the people that says not to do this as a cron... do you realize then the code will be executed if someone does something in the intranet in the chosen day? This means you are going to NEED someone actually doing something with the intranet, instead of the action beign executed at the chosen day. What if that day no one enters the intranet?
I wouldn't implement it a a scheduled job at all - I'd make the code deal with the scenario depending on the date it is processing.
Modifying te state of the system at this level means you're going to get in a mess if the rollover fails to occur. It also means that you can't easily measure historic data.
It's hard to say exactly how you should implement this without understanding more about what the system is doing. One way would be to hard-code the logic, e.g. in MySQL....
SELECT emp_id,
STRTODATE(CONCAT(DATE_FORMAT(clock_in_time, '%Y-%m-%d '),
IF(DATE_FORMAT(clock_in_time, '%m')
BETWEEN '05' AND '08', '08:00', '09:00')))
AS expected_clockin,
clock_in_time AS actual
FROM clockin
Or alternatively use a lookup table....
SELECT a.emp_id,
STRTODATE(CONCAT(DATE_FORMAT(a.clock_in_time, '%Y-%m-%d '),
b.start_time)
AS expected_clockin,
a.clock_in_time AS actual
FROM clockin a INNER JOIN lookup b
ON a.clock_in_time BETWEEN b.period_start AND b.period_end
精彩评论