简体   繁体   English

每月固定日期之间的PHP日期

[英]PHP date between fixed day of every month

I am not a PHP guru but I try and learn as I go. 我不是PHP专家,但我会尝试学习。 The following also confuses me. 以下也使我感到困惑。

I want to search for results in my SQL between two months from specific day of that month 我想从当月的特定日期起两个月内在我的SQL中搜索结果

So lets say today is the 15 August 2013 i want the results to show from the 15 July 2013 to the 15 August 2013 or the current date. 可以说今天是2013年8月15日,我希望结果显示为2013年7月15日至2013年8月15日或当前日期。 But it must start from the 15th of the previous month. 但必须从上个月的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)." ";
 ?>

This is for accounting purpose but my accounts run from the 15th of each month to the 15th 这是出于会计目的,但是我的帐户从每个月的15号开始运行

I would be nice to see what the account is at when i log on that is why I would like to get the end date as the current date 我很高兴看到我登录时的帐户名称,这就是为什么我希望将结束日期作为当前日期

This code works now: 该代码现在可以工作:

$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)." ";
?>

You can do it php using, try this: 您可以使用php来做,试试这个:

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

Or in mysql, try this: 或在mysql中,尝试以下操作:

where date_time >= date_sub(now() interval 1 month)

Just do: 做就是了:

<?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)." ";
?>

The new lines are explained as comments in the code. 新行将在代码中作为注释进行解释。

I hope this helped! 希望对您有所帮助!

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

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