PHP自动更改日期+1天

时间:2014-05-22 22:40:53

标签: php

我正在编写一个电子邮件应用程序,其中包含一个图形统计页面,该图表显示了用户每天发送的电子邮件数量,只有当我对其进行硬编码时,我才能正确获取每天的图表数据,但我想知道如何制作自动机制。

这是我用来从数据库中获取数据的查询:

查询:

 $today = "SELECT COUNT(tbl_template_log.user_id) FROM tbl_template_log ";
            $today .= "WHERE tbl_template_log.user_id='$id'";
            //$today .= " AND tbl_template_log.send_date > '2014-05-21' ";
            //$today .= "AND tbl_template_log.send_date < '2014-05-21'";

但是我希望有变量而不是日常重新调整的$ start和$ finish这样的日期,所以用户可以在没有我手动更改数据的情况下查看实际的每日数据....?

我的PHP课程:

class statsToday{

function con()
{
    require_once('connect.php');
    $DB = new dbConnect();
    return $DB->connect();
    }

    public $start;
    public $finish;

    public function getStats($user){

            $user = "SELECT user_id FROM tbl_user WHERE ott_email='$user'";
            $query = mysqli_query($this->con(), $user);
            $count = mysqli_num_rows($query);

            if($count == 1){

             while($row = mysqli_fetch_assoc($query)){
                 $id = $row['user_id'];
             }

            $today = "SELECT COUNT(tbl_template_log.user_id) FROM tbl_template_log ";
            $today .= "WHERE tbl_template_log.user_id='$id'";
            //$today .= " AND tbl_template_log.send_date > '$start' ";
            //$today .= "AND tbl_template_log.send_date < '$finihs''";
            $query_today = mysqli_query($this->con(), $today);
            $today = mysqli_fetch_row($query_today);

            echo $today[0];
        }

    }
}

有人可以建议解决方案..?

2 个答案:

答案 0 :(得分:0)

如果你只想要“今天”,你可以使用以下形式的内置mysql date功能:

$today = "SELECT COUNT(tbl_template_log.user_id) FROM tbl_template_log ";
$today .= "WHERE tbl_template_log.user_id='$id'";
$today .= " AND DATE(send_date) = DATE(NOW())";

或者在上周完成所有工作:

$today = "SELECT COUNT(tbl_template_log.user_id) FROM tbl_template_log ";
$today .= "WHERE tbl_template_log.user_id='$id'";
$today .= " AND tbl_template_log.send_date >= '" . date('Y-m-d', strtotime('-1 week')) . "' ";
$today .= "AND tbl_template_log.send_date =< '" . date('Y-m-d', strtotime('today')) . "'";

答案 1 :(得分:0)

另一种方法,如果您在send_date字段上索引数据库,则更容易使用:

$today = "SELECT COUNT(*) 
  FROM tbl_template_log
  WHERE user_id='$id'
  AND send_date BETWEEN CURDATE() AND CURDATE() + INTERVAL 1 DAY";