Get the real last 24 hours with PHP

Geschreven door Erwin A.W. Maas

Getting the last 24 hours

Looking around on the web about this, you mostly see the wrong solutions, so:
If you want to query a database for the last 24 hours in a MySQL-query like:

SELECT * FROM mytable WHERE date BETWEEN '$lastday' AND '$date'

Then using:

$date = date('Y-m-d'); $lastday = date('Y-m-d',strtotime("-1 day"));

will give you today + yesterday all day.

Instead you need to do:

$lastday = date('Y-m-d',strtotime("-1 day + 24 hours"));

So: if you're not sure, check your code!


  1. Markdown is toegestaan. Toegestane HTML-Tags: <strong>, <em>, <blockquote>, <code>, <pre>, <a>.