views:

37

answers:

1

Hi

Can you please tell me why this works:

$customer_data_date14daysAgo = mysql_query("SELECT COUNT(*) AS count 
FROM tableName WHERE datetime BETWEEN '$date14daysAgo%' and 
'$dateToday%' ") or die(mysql_error()); 

But this doesn't?

$customer_data_date30daysAgo = mysql_query("SELECT COUNT(*) AS count 
FROM tableName WHERE datetime BETWEEN '$date30daysAgo%' and 
'$dateToday%' ") or die(mysql_error());

PHP:

$dateToday = date ( 'Y-M-d', strtotime ( '-0 day' . $date ) );   
$date14daysAgo = date ( 'Y-M-d', strtotime ( '-14 day' . $date ) );   
$date30daysAgo = date ( 'Y-M-d', strtotime ( '-1 month' . $date ) );   

$dateToday = 2010-Oct-28
$date14daysAgo = 2010-Oct-21
$date30daysAgo = 2010-Sep-28

The only difference is that the second query spans the Sep - Oct barrier.

If I set the date manually to 2010-Oct-01 until today - it works
But if its 2010-Sep-30 until today - it stops working

Thank you!

A: 

if you want search data one month ago until the current date, perhaps this can help:

SELECT COUNT(*) AS count FROM table 
WHERE date BETWEEN DATE_SUB(CURRENT_DATE(),INTERVAL 1 MONTH) AND CURRENT_DATE()

and if you want to show 1 month ago( the current date not included), you can use:

SELECT COUNT(*) AS count FROM table 
WHERE date <= DATE_SUB(CURRENT_DATE(),INTERVAL 1 MONTH)

but if i'm not misunderstand.


by following your question, try like:

$customer_data_date30daysAgo = mysql_query("SELECT COUNT(*) AS count 
FROM tableName WHERE datetime BETWEEN '".$date30daysAgo."%' and 
'".$dateToday."%' ") or die(mysql_error());
klox