查看: 2596|回复: 24
|
PHP MySQL不让时间重叠
[复制链接]
|
|
本帖最后由 shern91 于 16-4-2015 12:05 AM 编辑
大家好,又是我了,之前的e-maid system又出了问题,现在是出在booking的时候了,我做成3个slot,让customer来booking时间,因为根据老师的说法,必须做成这样,如下图(还有选日期的图忘记没有附上)
当选择slot 1的 8:00 AM ,回拿到08:00:00的value,跟日期,然后用了如下的方式转成Datetime format,过后在从slot1,2,3来
- $starttime=date('Y-m-d H:i:s', strtotime("$date $book"));
复制代码
过后再从slot1,2,3来加interval的时间比如拿到选slot 1 08:00AM - 12:00PM,就会拿来加4个小时,slot2就会拿来加9个小时,再来就是compare跟database已经有的booking details确保日期时间不会重叠,
- if ($slot == 4)
- {
- $intervaltime->add(new DateInterval('PT04H00M00S'));
- $endtime = $intervaltime-> format('Y-m-d H:i:s');
- $query="SELECT COUNT(*) from booking_table where maid_id ='$maid' and start_time >= '$starttime' and end_time <= '$endtime' ";
- $result=mysql_query($query);
- list($num)=mysql_fetch_array($result);
- if(is_resource($result))
- {
- if($num>0)
- {
- echo "<script type='text/javascript'>alert('this slot already book by other member,please find another slot');window.location.href='selectMenu5.php';</script>";
- }
- else
- {
- $result1=mysql_query("INSERT INTO booking_table (user_id, maid_id, start_time, end_time) VALUES ('$username','$maid','$starttime','$endtime')")or die("Insert Error: ".mysql_error());
- echo "<script type='text/javascript'>alert('booking has success');window.location.href='selectMenu5.php';</script>";
- }
- }
- }
复制代码
有些时间可以detect到重叠,有些却不能
好像50,55,56就重叠了,到底是我的哪行出了问题 谢谢各位大大的帮忙,我觉得我的代码会乱,我想到就写了
|
|
|
|
|
|
|
|
发表于 16-4-2015 09:15 AM
|
显示全部楼层
本帖最后由 issac9413 于 16-4-2015 09:17 AM 编辑
因为你需要考虑到开始('$starttime)和结束('$endtime')的时间都不能与其他现有的数据的时间重叠
Line 05 :
- SELECT COUNT(*) from booking_table where maid_id ='$maid' and ('$starttime' between start_time and end_time) or ('$endtime' between
- start_time and end_time)
复制代码
|
|
|
|
|
|
|
|
楼主 |
发表于 16-4-2015 02:06 PM
|
显示全部楼层
我试试看大大给我的statement,现在才知道有between这个命令的
|
|
|
|
|
|
|
|
楼主 |
发表于 16-4-2015 02:22 PM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 16-4-2015 04:21 PM
|
显示全部楼层
查了很久,参考几个,目前用着这个还没发现任何问题
- "SELECT COUNT(*) from booking_table where maid_id ='$maid' and start_time <='$endtime' and end_time >= '$starttime' "
复制代码 |
|
|
|
|
|
|
|
发表于 16-4-2015 05:04 PM
|
显示全部楼层
Great, 只要你的endtime永远大过starttime就应该没有问题了
|
|
|
|
|
|
|
|
楼主 |
发表于 16-4-2015 05:50 PM
|
显示全部楼层
谢谢大大,搞不清楚这些logic的东西,要仔细想看看
|
|
|
|
|
|
|
|
发表于 18-4-2015 09:38 PM
|
显示全部楼层
终于有时间来回帖了.
关于时间的比对, 我上次有提过你了, 只是你忘了.
这边会相当复杂, 会分几个case, 我会一一解释
case 1:
当你的 $start_time 介于任何一笔记录之间, 他的概念就如下图
这个问题只要用以下语法就能避免掉
- (start_time <='$start_time' and end_time > '$start_time')
复制代码
case 2:
当你的 $end_time 介于任何一笔记录之间, 概念图如下:
一样, exclude 掉的语法如下:
- (start_time<'$end_time' and end_time>='$end_time')
复制代码
case 3:
当新的时段其中一小段时间已有类似的记录, 概念图如下:
要exclude 掉这情况, 语法如下:
- (start_time >= '$start_time' and end_time <='$end_time')
复制代码
总结以上三个情况, 整体语法如下
- select count(*) from booking_table where maid_id='$maid_id' and
- (
- (start_time <='$start_time' and end_time > '$start_time') or
- (start_time<'$end_time' and end_time>='$end_time') or
- (start_time >= '$start_time' and end_time <='$end_time')
- )
复制代码
那你可能会狐疑, 如果新增的时间区段只是已有记录的一小段, 如下图:
需要怎么办?
这个留给你自己去想.
祝学业进步, 共勉之
|
评分
-
查看全部评分
|
|
|
|
|
|
|
楼主 |
发表于 19-4-2015 11:44 AM
|
显示全部楼层
之前的贴有说过了,可是我还弄得不是很清楚,所以时间上有些有重叠,谢谢大大又再那么仔细的教导我, 想离题一下,如果要做GPS,用地址来找地址的距离,有可能吗?
|
|
|
|
|
|
|
|
楼主 |
发表于 19-4-2015 11:47 AM
|
显示全部楼层
目前是用着new_start_time >= old_end_time and new_end_time <= old_start_time, 还没发现任何的重叠。
|
|
|
|
|
|
|
|
发表于 19-4-2015 01:08 PM
|
显示全部楼层
你的方式人家已经指明了不行,
你照着我的图画去画看看, 你会发现, 你任何的新增都不会通过,
就算通过了, 你的记录不会是合法的, 因为 end_time > start_time
脑袋塞住了, 就用图片来清晰下思考
别在钻牛角尖.
|
|
|
|
|
|
|
|
发表于 21-4-2015 05:26 AM
|
显示全部楼层
如果你有上过基本的数学
两点的直线距离是
- d=sqrt((x1-x2)^2+(y1-y2)^2)
复制代码
只要知道这个观念, 你再去研究 google map API v3
大致上就能做到你要的效果.
必须注意的是, html5 是有提供获得使用者的location 的功能, 但是那功能基本上是废的,
第一是default 他不会启用, 使用者游览的过程才会被问及要不要"分享地点", 但是个人经验多半都是click no
第二是, 取得的位置是根据ISP的位置, 准确度很差.
你这个功能除非你要推出手机版, 否则不要花时间下去思考.
祝学业进步
|
|
|
|
|
|
|
|
楼主 |
发表于 21-4-2015 01:39 PM
|
显示全部楼层
谢谢大大的指导,Android的GPS有听朋友说是比较方便的,因为之前看了apple 网站的有一个找service location的位置,用current place来找最靠近的service center location,好像很不错。基于老师有建议做GPS,所以想学看看。
祝事事顺利
|
|
|
|
|
|
|
|
楼主 |
发表于 21-4-2015 01:52 PM
|
显示全部楼层
这个case,我用 (start_time <= '$start_time' and end_time >='$end_time')
当有新的start_time 大过 原本的 start_time,和新的end_time小过旧的start_time
|
|
|
|
|
|
|
|
发表于 21-4-2015 05:31 PM
|
显示全部楼层
答案是对的, 但是你掉进陷阱了
关于case 4
他会同时满足这个
- (start_time <='$start_time' and end_time > '$start_time')
复制代码
跟这个
- (start_time<'$end_time' and end_time>='$end_time')
复制代码
所以你无需再加入这种判断.
- (start_time <= '$start_time' and end_time >='$end_time')
复制代码
你多加一个判断式只是在浪费计算资源而以.
共勉之.
|
|
|
|
|
|
|
|
楼主 |
发表于 21-4-2015 07:02 PM
|
显示全部楼层
谢谢大大又指导,所以第三个陷进是挖给我跳的?哈哈
开玩笑而已 我的project基本上是ok了,小问题很多,比如insert资料那边如果blank也是可以照进,booking时间那边还没做限制几个小时前要进那样,做一个简单的system都不简单
|
|
|
|
|
|
|
|
发表于 18-12-2015 02:43 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 16-3-2016 05:29 PM
|
显示全部楼层
moot 发表于 18-12-2015 02:43 AM
PHP。。。 啊哈哈哈哈,令人怀念。 个人不鼓励新学的人用php, 上个stackoverflow, 一大把坏习惯的编辑码。
其实DB 做booking 是有个致命问题:No exclusive lock on FIFO.
几个人同时用的话,就
楼主的学校没有教用画flowchart 了吗? 你的问题其实在还没做coding的时候 , 用纸笔画流程就会发现问题。
想问大大, 除了DB做booking, 还有什么其它的方法吗?最好是改天能share with android and IOS
|
|
|
|
|
|
|
|
发表于 26-3-2016 05:29 PM
|
显示全部楼层
本帖最后由 hk 于 26-3-2016 05:33 PM 编辑
shern91 发表于 16-4-2015 02:22 PM
刚刚试过了,还是有重叠
我在xampp里面自己试先,
result出来应该3,可是拿到2而已,ID 44的重叠的detect不到,我试过把43和45delete了,结果result是0,就是会重叠了,还是我的statement有错,我再试试
这个方法只要where statement改一下就可以了,它有一个好处,就是代码的可读性比较高。
- SELECT COUNT(*) from booking_table where maid_id ='$maid' and ( start_time between '$starttime' and '$endtime' or end_time between '$starttime' and '$endtime');
复制代码
做这行,可读性是必须要优先考虑的,因为程式员的大部分时间并不是在编写代码,而是阅读代码和调试代码,最重要就是时间=金钱。
|
|
|
|
|
|
|
|
楼主 |
发表于 26-3-2016 06:41 PM
|
显示全部楼层
hk 发表于 26-3-2016 05:29 PM
这个方法只要where statement改一下就可以了,它有一个好处,就是代码的可读性比较高。
做这行,可读性是必须要优先考虑的,因为程式员的大部分时间并不是在编写代码,而是阅读代码和调试代码,最重要就是时间=金钱。
谢谢大大的回复以及教导,可是我的Project已经交了和也pass了,不过在我大学最后的最后一个学期,我自己多拿一个Web Programming来加强我的PHP基础,从刚才大大给我的SQL statement我多学到一个东西,可能以后可以运用在我的mini project上
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|