简体   繁体   English

从日期中提取年份,并使用年份从数据库中获取数据

[英]Extracting year from date and using the year to fetch data from DB

First of all i'd like to say that i am fairly new to the programming world. 首先,我想说我对编程世界还很陌生。 I am creating a system where Date plays a major role, I am currently able to display all the items where full date is shown, however i would like to create a menu where the user can view his transactions by month and year. 我正在创建一个系统,其中日期起主要作用,我目前能够显示显示完整日期的所有项目,但是我想创建一个菜单,用户可以在其中按月和年查看其交易。

    echo "<table border='1'>";
    echo "<tr><th>Transaction Date</th><th>Total Amount</th></tr>"; 

    $val=mysqli_query($conn,"SELECT TransactionDate, SUM(Amount) FROM                   transaction GROUP BY TransactionDate");
    while($row = mysqli_fetch_array($val))
    { 
        echo ("

        <tr><td><a href='inserttransaction.php?date=$row[0]'>$row[0]</a></td>
        <td>$row[1]</td></tr>");

    }

With the help of the above code i was able to group them all by using the current date, I'm quite new to the programming world and need help or clues on how do i make it year wise or say month wise. 在上面的代码的帮助下,我可以使用当前日期将它们全部分组,这对编程世界来说是一个新手,需要帮助或线索以了解如何使其年度化或月度化。 How am i supposed to extract only the "year" from it and as i am gonna go and display all the items saved in the DB with the current YEAR, how do i extract those items through the DB? 我应该如何从中仅提取“年份”,并随着当前年份显示存储在数据库中的所有项目,我如何通过数据库提取这些项目?

You can use mysql DATE_FORMAT 您可以使用mysql DATE_FORMAT

Something like for yearly grouping: 类似于每年的分组:

SELECT DATE_FORMAT(TransactionDate, "%Y") as date_key, 
       SUM(Amount) 
 FROM transaction 
GROUP BY date_key;

Try some thing like this.first fetch the date from db and move it to variable.To know the date format Manual click here 尝试这样的事情。首先从db获取日期并将其移至变量。要了解日期格式, 请单击此处

$datz=$row['date'];
$time=strtotime($dateValue);
$month=date("F",$time);
$year=date("Y",$time);

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

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