Stanislas Piotrowski
Stanislas Piotrowski

Reputation: 2704

substracting 1 years to a date php/mysql

I'm trying to select something from my database but I do a chart, so for comparating In eed the same value but for the previous years.

Also I've done something like that:

$query254="
  SELECT SUM(honoraires) 
  FROM versements 
  WHERE `date_reception`=DATE_SUB(".$dates3[$i3].", INTERVAL 365 DAY)";

  $resultat254 = mysql_query($query254) or die(mysql_error());
  $rowing254 = mysql_fetch_array($resultat254); ?>

The thing is that it does not work, In fact it select the values for the same year. Without substracting the 365 days.

So I do not know how to do.

Receive all my Utmost Respect.

SP.

Upvotes: 0

Views: 97

Answers (2)

Pitchinnate
Pitchinnate

Reputation: 7566

If you don't mind doing it in php before you put it into the MySQL query. Just do this:

$tempdate = date('Y-m-d',strtotime($dates3[$i3] . ' -1 year'));
$query254="
  SELECT SUM(honoraires) 
  FROM versements 
  WHERE `date_reception`='{$tempdate}'";

Upvotes: 2

Thorsten
Thorsten

Reputation: 5644

$query254="SELECT 
SUM(honoraires) 
FROM 
versements 
WHERE 
`date_reception`=date(".mysql_real_escape_string($dates3[$i3])." - INTERVAL 1 YEAR)";

Upvotes: 1

Related Questions