0

I have this query:

$sql = "SELECT * FROM giorni WHERE punto = '$punto' && STR_TO_DATE(giorno, '%d/%m/%Y') ...

I need to do a similar query for the previous year. How can i do %Y-1?

Salman A
  • 262,204
  • 82
  • 430
  • 521
FrancescoElba
  • 33
  • 1
  • 6

1 Answers1

2

You can simply subtract 1 year from the converted date:

... STR_TO_DATE(giorno, '%d/%m/%Y') - INTERVAL 1 YEAR
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • Thanks, man!! So i did like this $sqlannoprima = "SELECT * FROM giorni WHERE punto = '$punto' && STR_TO_DATE(giorno, '%d/%m/%Y')- INTERVAL 1 YEAR BETWEEN STR_TO_DATE('" . $d1 . "', '%d/%m/%Y') - interval 1 YEAR AND STR_TO_DATE('" . $d2 . "', '%d/%m/%Y')- interval 1 YEAR ORDER BY STR_TO_DATE(giorno, '%d/%m/%Y')- interval 1 YEAR"; – FrancescoElba Oct 03 '18 at 13:58
  • To be honest, if I knew that the dates were coming from PHP I would simply add/subtract/format them using PHP (`DateTime` class). – Salman A Oct 03 '18 at 14:05
  • It is possibile to add/subtract with INTERVAL a variable, like "- INTERVAL $variable YEAR" ? Thanks – FrancescoElba Oct 05 '18 at 13:25
  • Variable coming from PHP? Yes. – Salman A Oct 05 '18 at 13:37