I have SQL like this:
`table1`.`DateField` >= DATE_SUB(NOW(), INTERVAL {$days} DAY
My table1.DateField is in the format of 2010-03-10 10:05:50.
The value of {$days} could be 4 hours or 3 days. How can I make the above sql smart enough to know that? Should I use
`table1`.`DateField` >= DATE_SUB(NOW(), INTERVAL {$days} DAY_HOUR
“could be 4 hours or 3 days” – and it can’t be
4 HOURor3 DAY?Do you have to worry about invalid units or even malicious (user) input?
You could test the given string and “convert” the units to something MySQL understand, e.g.
prints