Search code examples
sqldatestr-to-date

Use STR_TO_DATE in sql with year change


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?


Solution

  • You can simply subtract 1 year from the converted date:

    ... STR_TO_DATE(giorno, '%d/%m/%Y') - INTERVAL 1 YEAR