简体   繁体   English

如何在一个月内获得所有日期

[英]How to get all dates in a month

 CREATE TABLE `tbl_atn` (
`atn_id` int(15) NOT NULL AUTO_INCREMENT,
`eng_id` int(15) DEFAULT NULL,
`visit` varchar(50) DEFAULT NULL,
`travel` varchar(50) DEFAULT NULL,
`start_time` varchar(50) DEFAULT NULL,
`mile` varchar(50) DEFAULT NULL,
`end_time` varchar(50) DEFAULT NULL,
`comments` varchar(100) DEFAULT NULL,
`actual` varchar(50) DEFAULT NULL,
`total_job` varchar(50) DEFAULT NULL,
`regular` varchar(50) DEFAULT NULL,
`over` varchar(50) DEFAULT NULL,
`total_hrs` varchar(50) DEFAULT NULL,
`pay` varchar(50) DEFAULT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`atn_date` date DEFAULT NULL,
 PRIMARY KEY (`atn_id`)
 )

表

What I want to do is create is month report for each month. 我想要做的是创建每个月的月度报告。 All specific dates are displayed and records from above table if no record then empty 如果没有记录为空,则显示所有特定日期并从上表中记录

The above is my table. 以上是我的表。 I am trying to create this application, but due to this sheet I am stuck. 我正在尝试创建此应用程序,但由于此表,我被卡住了。 First of all, can this only be achieved using a MySQL query? 首先,这只能通过MySQL查询来实现吗? If not, what I have to do is generate all dates first, then for each date I have to fetch a record from the database then run another query to sum them up. 如果没有,我要做的就是首先生成所有日期,然后对于每个日期我必须从数据库中获取记录然后运行另一个查询来总结它们。 I am unable to create a query for that. 我无法为此创建查询。

Any help? 有帮助吗?

   $now = date('Y-m-d');
   $month = date("m",strtotime($now));
   $year = date("Y",strtotime($now));


   $first = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
   $last = date('Y-m-t', mktime(0, 0, 0, $month, 1, $year));

   $thisTime = strtotime($first);
   $endTime = strtotime($last);
   while($thisTime <= $endTime)
   {
   $thisDate = date('Y-m-d', $thisTime);
   echo $thisDate."<br>";

   $thisTime = strtotime('+1 day', $thisTime); // increment for loop
   }

Made this code now dynamic now i can get all the dates of any month given month and year is given now what i will do now is make a function that will loop thru all dates and send query to database to find data if found it will set values other wise zero is it right approach ? 现在这个代码现在变得动态我现在可以得到任何月份的所有日期给定月份和年份现在我将要做的是制作一个函数,它将循环通过所有日期并将查询发送到数据库以查找数据,如果发现它将设置价值其他明智的零是正确的方法吗?

This will get all records where atn_date is in this month: 这将获得atn_date在本月的所有记录:

SELECT * FROM `tbl_atn` WHERE `atn_date` BETWEEN "2012-06-01" AND "2012-06-30"

This PHP will loop through every day in this month: 这个PHP将在本月的每一天循环:

$thisTime = strtotime("2012-06-01");
$endTime = strtotime("2012-06-31");
while($thisTime <= $endTime)
{
    $thisDate = date('Y-m-d', $thisTime);
    echo $thisDate;

    $thisTime = strtotime('+1 day', $thisTime); // increment for loop
}

A common way of displaying a contiguous sequence when your table may have none or only some of the records in your range of interest, is to use an integer table. 当您的表可能没有或只有您感兴趣的范围内的某些记录时,显示连续序列的常用方法是使用整数表。 An integer table contains integers from 0 to 9 in sequence. 整数表按顺序包含0到9的整数。 When you need a set of sequential numbers you self join it to get what you want. 当您需要一组连续数字时,您可以自己加入它以获得您想要的数字。 So for a range from 5 to 25 do 所以对于5到25的范围

SELECT i.n + j.n*10 as num
FROM myints i CROSS JOIN myints j
WHERE (i.n + j.n*10) BETWEEN 5 AND 25
ORDER BY (i.n + j.n*10);

In your case you want sequential dates. 在您的情况下,您想要连续日期。 You know that any particular month can have at most 31 days, so you do a subquery for a set of integers from 0 to 31 and express them as dates starting on your beginning of month and finishing on your end of month. 您知道任何特定月份最多可以有31天,因此您可以为0到31之间的一组整数执行子查询,并将其表示为从月初开始到月末结束的日期。 Like so: 像这样:

SELECT DATE_ADD('2012-06-01', INTERVAL n.num DAY) AS mydate, o.*
FROM
    (SELECT i.n + j.n*10 as num
    FROM myints i CROSS JOIN myints j
    WHERE (i.n + j.n*10) BETWEEN 0 AND 31
    ORDER BY (i.n + j.n*10)) AS n
LEFT JOIN other o ON ( DATE_ADD('2012-06-01', INTERVAL n.num DAY) = o.atn_date)
WHERE mydate BETWEEN '2012-06-01 '2012-06-30';

or 要么

SELECT datelist.mydate, o.* FROM
    (SELECT DATE_ADD( '2012-01-06', INTERVAL i.n + j.n*10 DAY) as mydate
      FROM myints i CROSS JOIN myints j
      WHERE mydate BETWEEN '2012-01-06' AND '2012-01-30'
      ORDER BY (i.n + j.n*10)) datelist
LEFT JOIN othertable o ON (datelist.mydate=o.atn_date);

If you want to get specific days in a month then query for them, you can use the built in PHP function cal_days_in_month ( http://php.net/manual/en/function.cal-days-in-month.php ). 如果您想在一个月内获得特定日期然后查询它们,您可以使用内置的PHP函数cal_days_in_month( http://php.net/manual/en/function.cal-days-in-month.php )。 You can write a real simple function to handle this such as the following: 你可以写一个真正的简单函数来处理这个,如下所示:

function getDateTime($month, $year){
    $month = intval($month);
    $year = intval($year);
    $day = cal_days_in_month(CAL_GREGORIAN, $month, $year);

    //Now build your query here since you will have the days of the month
    $query = SELECT * FROM `tbl_atn` WHERE `atn_date` BETWEEN $year."-".$month."-1" AND $year."-".$month."-".$day;
}

Note, the dates piece is however you have it configured in your database. 请注意,日期部分是您在数据库中配置的。 I just used the above query example from Scott Saunders for simplicity sake. 为简单起见,我只使用了Scott Saunders的上述查询示例。

If you do not have the calendar plugin built for your PHP stack, you can also do a custom function with date() - http://php.net/manual/en/function.date.php . 如果您没有为PHP堆栈构建的日历插件,您还可以使用date() - http://php.net/manual/en/function.date.php执行自定义函数。

It is possible to retrieve a list of dates in a particular month and year using mysql 可以使用mysql检索特定月份和年份的日期列表

You can try this: 你可以试试这个:

           SELECT 
                ldays.`day` as 'Date',
                atn.`regular` as RegularHours,
                atn.`over` as OT1,
                atn.`over` as OT2,
                atn.`total_hrs` as TotalHrsPerDay,
                atn.`comments` as Comments
                FROM( 
                SELECT DATE_FORMAT(ADDDATE(LAST_DAY(SUBDATE(DATE_FORMAT('{$DateParamHere}','%Y-%m-%d'), INTERVAL 1 MONTH)), 1) + INTERVAL a + b DAY,'%Y-%m-%d') as 'day'
                FROM
                (SELECT 0 a UNION SELECT 1 a UNION SELECT 2 UNION SELECT 3
                    UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7
                    UNION SELECT 8 UNION SELECT 9 ) d,
                (SELECT 0 b UNION SELECT 10 UNION SELECT 20 
                    UNION SELECT 30 UNION SELECT 40) m
                WHERE ADDDATE(LAST_DAY(SUBDATE(DATE_FORMAT('{$DateParamHere}','%Y-%m-%d'), INTERVAL 1 MONTH)), 1) + INTERVAL a + b DAY  <=  LAST_DAY('{$DateParamHere}')
                ORDER BY a + b
                ) ldays
           LEFT JOIN `tbl_atn` atn ON (atn.`eng_id`='{$EngIDParamHere}' AND DATE(atn.`atn_date`) = ldays.`day`)

$DateParamHere = you can set here a particular year, month, and current day and concat it by the format of '%Y-%m-%d' in mysql but you can change its format anyways $ DateParamHere =你可以在这里设置一个特定的年,月和当天,并在mysql中以'%Y-%m-%d'的格式连接它,但你可以改变它的格式

$EngIDParamHere = put the id of a particular engineer here $ EngIDParamHere =在此处输入特定工程师的ID

after that you are good to go .. :) 之后你很高兴.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM