Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

choose records from last month... Mysql

Status
Not open for further replies.

tgignac

IS-IT--Management
May 13, 2004
18
CA
Hi folks, I seem to have a problem with my script for only getting records from the previous month... my script is below: Any help would be greatly appreciated

*<?php require_once('../../Connections/MySQL.php'); ?>
<?php
$month = date('n');
$year = date('Y');

$lastmonth = $month-1;
if ($lastmonth==0) { $lastmonth=12; $year--; }

$startdate = $year . '-' . $lastmonth . '-01';
$enddate = date('Y') . '-' . $month . '-01';

mysql_select_db($database_MySQL, $MySQL);
$query_Recordset1 = "SELECT * FROM PlanInsp WHERE InspDate>='" . $startdate . "' AND InspDate<'" . $enddate . "' ";
$Recordset1 = mysql_query($query_Recordset1, $MySQL) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>*
 
I think you make it more complicated than necessary.
Use the MySQL function MONTH
Code:
SELECT * FROM PlanInsp WHERE MONTH(InspDate) = MONTH(NOW)-1
Something similar like that.
 
At the moment I am using the mktime() function. The date is converted into a number. There no converting problem and it is a little bit faster.

Michelle,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top