【问题标题】:Cannot get result between datetime with 2 given dates无法在日期时间与 2 个给定日期之间获得结果
【发布时间】:2015-04-15 10:45:28
【问题描述】:

我想在日期时间之间得到一个结果,但没有成功。

这个问题困扰了我很久。 希望得到专家的解答。

我的 PHP 看起来像这样:

$fromdate = mysql_real_escape_string($_REQUEST['fromdate']);    
$fromdate = date_create_from_format('d/m/Y H:i:s', $fromdate);    
$fromdate = date_format($fromdate, 'Y-m-d H:i:s');

$todate= mysql_real_escape_string($_REQUEST['todate']);    
$todate= date_create_from_format('d/m/Y H:i:s', $todate);    
$todate= date_format($todate, 'Y-m-d H:i:s');    

$qtotal = mysql_query("SELECT SUM(total) AS total FROM pos WHERE status=1 and dateline >= '".$fromdate."' AND dateline < '".$todate."'");    
while ($ltotal = mysql_fetch_array($qtotal)){       
    $total = $ltotal['total'];    
}
echo $total;

我无法在 php 端得到任何结果,但 MySQL 查询工作正常。

select 
    sum(total) as total 
from pos 
where (status =1 and dateline >= '2015-04-11 00:00:00' 
and dateline < '2015-04-15 18:30:00'); 

日期线类型为 DATETIME。

【问题讨论】:

  • 尝试回显这个查询 SELECT SUM(total) AS total FROM pos WHERE status=1 and dateline >= '".$fromdate."' AND dateline
  • 使用BETWEENWHERE status =1 AND dateline BETWEEN '2015-04-11' AND '2015-04-15 18:30:00'
  • @Chlam 你在 url 中传递给 fromdatetodate 什么?
  • 日期格式如:11/04/2015 17:45:05 (d/m/Y H:i:s)
  • 这个查询应该并且它正在工作。您确定有与此查询匹配的记录吗? Can you show us a non-working example?

标签: php mysql datetime


【解决方案1】:

感谢您的帮助,我已经通过这种方式解决了我的问题...
;我真的很困惑为什么可以使用这个=.=

$strtodate= strtotime($todate);
$strfromdate= strtotime($fromdate);

$fromdate = "".date('Y',$strfromdate)."-".date('m',$strfromdate)."-".date('d',$strfromdate)." ".date('H',$strfromdate).":".date('i',$strfromdate).":".date('s',$strfromdate)."";
$todate = "".date('Y',$strtodate)."-".date('m',$strtodate)."-".date('d',$strtodate)." ".date('H',$strtodate).":".date('i',$strtodate).":".date('s',$strtodate)."";

$qtotal = mysql_query("SELECT SUM(total) AS total FROM pos WHERE (status=1 AND dateline >= '$fromdate' AND dateline < '$todate')");

【讨论】:

    猜你喜欢
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多