简体   繁体   English

PHP Mysql查询获取特定日期范围内所有星期一的时间范围,并将这些不同的开始和结束时间放入数据库中

[英]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.

相关问题 php function 用于获取日期范围内的所有星期一 - php function for get all mondays within date range 在php中获取日期范围内的所有星期开始日期和结束日期 - Get all week start date and end date within a date range in php 如何在PHP / MySQL中的某个日期范围内汇总一行 - How to Sum a Row Within A Certain Date Range in PHP / MySQL 按值对数组排序,然后在这些值内随机分组,但将某些元素分组-还是修改MySQL查询以将所有元素组合在一起? - Sort array by value, then randomly within those values BUT group certain elements - or revise MySQL query to combine it all? 输入开始日期和结束日期到MYSQL数据库 - Input Start Date and End Date to MYSQL Database 如何在php中获得本月的开始/结束星期一 - How-to get the start/end Mondays of the month in php 使用php显示mysql数据库中从开始日期到结束日期的数据,其中用户提供开始日期和结束日期 - Displaying data from a mysql database from start date to end date using php where user provides the start and end date MySQL日期范围查询 - MySql Query For Date Range in PHP PHP:返回日期范围的日期时间开始和日期时间结束 - PHP: Return date time start and date time end for a date range php mysql搜索开始日期和结束日期字段以获取今天的日期(如果它在范围内) - php mysql search start & end dates fields for todays date if it falls inside range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM