2

我不是 PHP 专家,但我会边走边学。以下内容也让我感到困惑。

我想在我的 SQL 中搜索从那个月的特定日期开始的两个月内的结果

因此,假设今天是 2013 年 8 月 15 日,我希望结果显示从 2013 年 7 月 15 日到 2013 年 8 月 15 日或当前日期。但必须从上个月的 15 日开始。

$first = date('Y-m-15');
$last = date('Y-m-15'); //Not sure if i should change this to $last = date('Y-m-t');

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
        $query = "SELECT * FROM pearson_lbs_log WHERE date_time  >=  '" .    
 $first . "' 
 AND date_time  <= '" . $last . "' ORDER BY date_time DESC";
 $result = mysql_query($query);
 echo " ".mysql_num_rows($result)." ";
 ?>

这是出于会计目的,但我的帐户从每个月的 15 日到 15 日运行

我很高兴看到我登录时的帐户是什么,这就是为什么我想将结束日期作为当前日期

此代码现在有效:

$first = date('Y-m-15', strtotime("$last -1 month"));
$last = date('Y-m-t'); 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
        $query = "SELECT * FROM pearson_lbs_log WHERE date_time  >=  '" .    
$first . "' 
AND date_time  <= '" . $last . "' ORDER BY date_time DESC";
$result = mysql_query($query);
echo " ".mysql_num_rows($result)." ";
?>
4

2 回答 2

0

你可以用php来做,试试这个:

$last=   date('Y-m-15');
$first=  date('Y-m-15', strtotime("$last -1 month")); 

或者在mysql中,试试这个:

where date_time >= date_sub(now() interval 1 month)
于 2013-08-14T22:11:56.907 回答
0

做就是了:

<?php
$last = date('Y-m-15');
$month-today = date('m'); //get the current month

$lastmonth = $month-today - 1; //subtract one from this month to get last month
if($lastmonth == 00){ //in the case of January and December, December would be 00 (01 - 1 = 00)
$lastmonth = 12;
} 

$first = date('Y')."-".$lastmonth."-15"; //calculate the full variable in Y/lmonth/15 format

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
    $query = "SELECT * FROM pearson_lbs_log WHERE date_time  >=  '" .    
    $first . "' 
    AND date_time  <= '" . $last . "' ORDER BY date_time DESC";
$result = mysql_query($query);
echo " ".mysql_num_rows($result)." ";
?>

新行在代码中解释为注释。

我希望这有帮助!

于 2013-08-14T22:12:27.637 回答