Frage

I have a table that stores dates in the format of MM/DD/YY (field name is date). I need to have a count that totals just the records for the current month. From searching, this is what I currently have but something is not right. Do I need to do a convertToDate, is it that my field is called "date", or am I missing something entirely?

$totalcount = mysql_query("select count(*) as 'total' 
                           FROM state_to_state 
                           WHERE status = 99 AND type = 1 
                           AND MONTH(`date`) = MONTH(CURDATE()) 
                           AND YEAR(`date`) = YEAR(CURDATE())");

$totalnum = mysql_fetch_array($totalcount);
if($totalnum['total'] > 0) { $month_status = $totalnum['total']." this Month. "; }

Keine korrekte Lösung

Andere Tipps

Your dates are NOT native mysql dates (varchar, probably?), so you cannot use the mysql date functions on them directly. date('MM/DD/YY') will not work reliably:

mysql> select month('03/03/14'), month('03/18/14');
+-------------------+-------------------+
| month('03/03/14') | month('03/18/14') |
+-------------------+-------------------+
|                 3 |              NULL |
+-------------------+-------------------+

Convert your date fields to native mysql date types, or take a huge hit on performance and convert them to native date values onthefly via str_to_date().

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top