[英]PHP Mysql query that gets a timeframe of all mondays within a certain date range and puts those different start and end times in a database
I have a challenge where I want all the start and end epoch of all the certain days (example Monday) within a specific date range and put these in individual rows The table looks like this: 我遇到一个挑战,我想在特定的日期范围内将所有特定日期(例如星期一)的所有开始和结束纪元放在各个单独的行中,该表如下所示:
+----+---------+------------+------------+
| id | id_room | start_ts | end_ts |
| 3 | 3 | 1445032800 | 1445378400 |
| 4 | 3 | 1445551200 | 1446073200 |
| 5 | 3 | 1446246000 | 1446591600 |
| 6 | 3 | 1446850800 | 1447196400 |
| 7 | 3 | 1447455600 | 1447801200 |
| 8 | 3 | 1448060400 | 1448406000 |
| 9 | 2 | 1445475600 | 1445572800 |
| 10 | 1 | 1445378400 | 1445464800 |
| 11 | 3 | 1445032800 | 1445378400 |
+----+---------+------------+------------+
I manage to get the start and end epochs in a few different ways: 我设法通过几种不同的方式来获取开始和结束的纪元:
$start = strtotime('2016-06-01');
$end = strtotime('2016-06-30');
$mondays_start=array();
while( $start <= $end ) {
if ( date('N',$start)== 1 )
$mondays_start[]=$start+43200;
$start += 86400;
}
$start = strtotime('2016-06-01');
$end = strtotime('2016-06-30');
$mondays_end=array();
while( $start <= $end ) {
if ( date('N',$start)== 1 )
$mondays_end[]=$start+64800;
$start += 86400;
}
But my real problem starts when I try to get them into my table. 但是,当我尝试将它们放入桌子时,我的真正问题开始了。 I guess I can break the arrays up into different variables and make individual queries based on that but it doesn't seem very productive.
我想我可以将数组分解为不同的变量,并以此为基础进行单个查询,但效率似乎并不高。
I tried some of the solution here Is it bad to put a MySQL query in a PHP loop? 我在这里尝试了一些解决方案,将MySQL查询放入PHP循环是否不好? but cannot get it to work.
但无法使其正常工作。
I also tried it in a for loop like so 我也在这样的for循环中尝试过
$start = strtotime('2016-06-01');
$end = strtotime('2016-06-30');
for ($start; $start < $end; $start+604800) {
$mondays .= "INSERT INTO `#__room_closure`(`id_room`,`start_ts`,`end_ts`) VALUES( ".
$args['id_room'].",".$start.",".$start+21600.");";
}
But that doesnt work either. 但这也不起作用。
Looking forward to your thoughts on the matter. 期待您对此事的想法。
I created a way to answer the question however it creates a new one. 我创建了一种方法来回答这个问题,但是它创建了一个新的问题。
The answer is as follows: 答案如下:
for ($args["start_ts"]; $args["start_ts"] < $args["end_ts"]; $args["start_ts"]+=604800) {
$mondays .= "INSERT INTO `#__room_closure`(`id_room`,`start_ts`,`end_ts`) VALUES( ".
$args['id_room'].",".$args["start_ts"].",".($args["start_ts"]+21600).");";
};
this will create an array with the several different queries in a string: 这将创建一个字符串数组,其中包含多个不同的查询:
"INSERT INTO `#__room_closure`(`id_room`,`start_ts`,`end_ts`) VALUES( 1,1306900800,1306922400);INSERT INTO `#__room_closure`(`id_room`,`start_ts`,`end_ts`) VALUES( 1,1307505600,1307527200);INSERT INTO `#__room_closure`(`id_room`,`start_ts`,`end_ts`) VALUES( 1,1308110400,1308132000);INSERT INTO `#__room_closure`(`id_room`,`start_ts`,`end_ts`) VALUES( 1,1308715200,1308736800);INSERT INTO `#__room_closure`(`id_room`,`start_ts`,`end_ts`) VALUES( 1,1309320000,1309341600);"
This solved my question. 这解决了我的问题。
However with joomla I cannot put multiple queries in the Query() function. 但是,使用joomla时,我无法在Query()函数中放置多个查询。 But that is a different story and should be told another time.
但这是一个不同的故事,应该再讲一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.