dongmeirang4679 2015-10-27 09:11
浏览 28

PHP MySQL组由unix Timestamp提供报告

I have a table tbl_news, and I wanted to create several reports such us today, this week, this month.

I have stored time in unix timestamp at "news_adate". For this week report I am using the following query.

$first = date('Y-m-d',strtotime("monday this week"));
$last = date('Y-m-d',strtotime("sunday this week"));

$first = $first ." 00:00:01";
$last = $last ." 23:59:59";

$query = "SELECT count(nid), news_adate FROM tbl_news WHERE (news_adate between '".$first."' AND '".$last."') AND tbl_news.news_status=1 GROUP BY DATE(FROM_UNIXTIME(tbl_news.news_adate))" ;

I got the following output.

25-Oct 2015     1

27-Oct 2015     45

But actually it should be

25-Oct 2015     1

26-Oct 2015     35

27-Oct 2015     10

Don't know where is the error?

  • 写回答

1条回答 默认 最新

  • dquh37673 2015-10-27 09:20
    关注

    Just a shot in the dark but you're missing the midnight hour by saying between 00:00:01 and 23:59:59 right? What prompted you to use the Unix timestamp rather than the MySQL now() function to store your date time values?I find querying against the MySQL date format substantially easier and more manipulatable with MySQL functions like trunc(). Typically I store both but when querying against dates I prefer the MySQL date time column as opposed to an integer representing a time. So my first suggestion is to check this output:

     $first = date('Y-m-d',strtotime("monday this week"));
    $last = date('Y-m-d',strtotime("sunday this week"));
    
    $first = $first ." 00:00:01";
    $last = $last ." 23:59:59";
    
    $query = "SELECT count(nid), news_adate FROM tbl_news WHERE (news_adate between '".$first."' AND '".$last."') AND tbl_news.news_status=1 GROUP BY DATE(FROM_UNIXTIME(tbl_news.news_adate))" ;
    
    Echo $query;
    

    Anytime you're constructing a query you should check the value of the string to ensure it truly is what you want it to query.

    You can also use a SQL tool like toad to help you construct valid queries and check their output before replacing hard values with variables.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分