使用 PHP/MySQL 调度脚本 - 逻辑帮助

发布于 2024-08-03 19:17:13 字数 424 浏览 3 评论 0原文

我正在开发一个小脚本,用户可以在其中插入他们的“时间表”。但是,我需要一些帮助来确定如何创建数据库结构以及如何在数据库中输入事件“时间”的逻辑。

然而,需要注意的一件事是,当用户输入“时间表”时,他们不会输入确切的日期。相反,他们将进入“一周中的几天”。有点像“定期”约会。

例如,用户 A 可以输入以下时间表:

MWF - 上午 8:00 - 上午 10:00 MW - 2:00pm - 3:00pm 等等...

正如您所看到的,我正在寻找一种使用通用“一周中的日子”的方法,而不一定是确切的日期。

考虑到这一点,将其存储在数据库中的最佳方式是什么,因为我知道最终我可能会“查询”数据库以搜索可用时间。

我应该以毫秒或秒为单位输入它们吗? “0”表示周日中午 12:00?

任何建议都会很棒。

谢谢!

I'm looking to develop a small script where users can insert their "schedule." However, I need some help determining the logic of how to create the DB structure and how to input the "time" of the events in the database.

One thing to note, however, is that when users enter their "schedule", they will not be entering exact dates. Instead, they will be entering 'days of the week.' Somewhat like a 'recurring' appointment.

For example, User A could enter the following time schedule:

MWF - 8:00am - 10:00am
MW - 2:00pm - 3:00pm
etc...

As you can see, I'm looking for a way to use generic 'days of the week', not necessarily exact dates.

With this in mind, what would be the best way to store this in the database, knowing that eventually, I may be "querying" the database to search for available times.

Should I enter them in milliseconds or seconds? With "0" being 12:00AM on Sunday?

Any suggestions would be great.

Thanks!

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

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

发布评论

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

评论(1

新雨望断虹 2024-08-10 19:17:13

首先,MySQL包含一个时间数据类型。我强烈建议您利用它。

无论如何,对于您的表格(这是用于学术安排,但相同的基本思想适用):

DaysOfWeek
----------
DowID int
DayOfWeek varchar(10)

Users
------------------------
UserID int autoincrement
UserName varchar(50)
PassHash varbinary(100)
FirstName varchar(50)
LastName varchar(50)
DOB datetime

Semesters
----------------------------
SemesterID int autoincrement
SemesterName varchar(50)
SemesterNumber int
AcademicYear int
CalendarYear int

Classes
-------------------------
ClassID int autoincrement
ClassName varchar(50)
ClassCode varchar(25)

Schedule
----------------------------
ScheduleID int autoincrement
UserID int
SemesterID int
DowID int
ClassID int
BeginTime time
EndTime time

现在,您需要做的就是查看某人在周一 1 点到 2 点之间是否有空:

select
    count(*) as classes
from
    schedule sch
    inner join users u on
        sch.userid = u.userid
    inner join semesters sem on
        sch.semesterid = sem.semesterid
where
    u.username = 'rmcdonald'
    and sem.academicyear = 2009
    and sem.semesternumber = 1
    and sch.dowid = dayofweek($mytime)
    and $mytime between sch.begintime and sch.endtime

替换 $ mytime 与您在那里查看的任何时间。

First of all, MySQL includes a time data type. I highly recommend you take advantage of it.

Anyway, for your tables (this is for an acadmic scheduling, but the same basic ideas apply):

DaysOfWeek
----------
DowID int
DayOfWeek varchar(10)

Users
------------------------
UserID int autoincrement
UserName varchar(50)
PassHash varbinary(100)
FirstName varchar(50)
LastName varchar(50)
DOB datetime

Semesters
----------------------------
SemesterID int autoincrement
SemesterName varchar(50)
SemesterNumber int
AcademicYear int
CalendarYear int

Classes
-------------------------
ClassID int autoincrement
ClassName varchar(50)
ClassCode varchar(25)

Schedule
----------------------------
ScheduleID int autoincrement
UserID int
SemesterID int
DowID int
ClassID int
BeginTime time
EndTime time

Now, all you'd need to do to see if somebody's available on a Monday between 1 and 2 is:

select
    count(*) as classes
from
    schedule sch
    inner join users u on
        sch.userid = u.userid
    inner join semesters sem on
        sch.semesterid = sem.semesterid
where
    u.username = 'rmcdonald'
    and sem.academicyear = 2009
    and sem.semesternumber = 1
    and sch.dowid = dayofweek($mytime)
    and $mytime between sch.begintime and sch.endtime

Replace the $mytime with whatever time you're checking there.

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