如何在 PHP/MySQL Web 应用程序中实现计划事件

发布于 2024-11-17 23:52:25 字数 324 浏览 1 评论 0原文

我正在使用 PHP/MySQL 开发一个 Intranet Web 应用程序。我的问题是关于预定的事件:我每年有 2 个重要的日期(让我称它们为 date_01 和 date_02),系统必须更改员工签到的开始时间。例如,在 date_01 签到在 x 时间开始,而在 date_02 签到在 y 时间开始。就像夏季签到时间与冬季签到时间不同......

从软件工程的角度来看,您认为实现这一目标的最佳方法是什么?
->计划任务?
-> MySQL 预定事件?
->如果可能的话,某个 PHP 技巧?
->你的建议?

谢谢

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) where 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

超可爱的懒熊 2024-11-24 23:52:25

我认为你根本不需要计划任务。您可以有一个像这样的表:

check_ins (valid_from, valid_until, check_in_start_time)

然后当您需要在 Web 应用程序中获取当前签到开始时间时执行类似的操作:

select check_in_start_time
from check_ins
where
    now() >= valid_from and
    now() < valid_until

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
彩扇题诗 2024-11-24 23:52:25

我通常使用系统的 cron 做这样的事情

使用外部解释器的经典标记创建一个文件,然后在标记内

#!/usr/bin/php -f
<?php

    function hello_world(){ echo "hi!"; }
    hello_world();

?>

添加 php 代码 然后使用 chmod a+x my_php_files_with_commands.php 使其可执行,并为所需的 cron 添加一个条目日程。您可以立即访问应用程序的 php 函数,只需添加您需要的 require() 调用并使用它即可。数据库访问、文件访问等等……除了(显然)与会话有关的任何内容。小心一点。

编辑:对于那些说不要以 cron 的方式执行此操作的人...您是否意识到,如果有人在选定的一天在内联网中执行某些操作,代码将会被执行?这意味着您将需要有人实际使用内联网执行某些操作,而不是在选定的日期执行操作。万一那天没人进内网怎么办?

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?

北城挽邺 2024-11-24 23:52:25

我根本不会将其实现为预定作业 - 我会让代码根据处理的日期来处理场景。

在此级别修改系统状态意味着如果翻转未能发生,您将陷入混乱。这也意味着您无法轻松测量历史数据。

如果不更多地了解系统正在做什么,很难准确地说出应该如何实现这一点。一种方法是对逻辑进行硬编码,例如在 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

或者使用查找表......

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文