The ideal would solve in the SQL layer, which depends on the engine used.
For Mysql
Just do it right on select:
.--- converte de Unix para Timestamp, compativel com MySQL
|
SELECT ... WHERE DATE(FROM_UNIXTIME(last_used)) = CURRENT_DATE;
| |
'--- extrai a data, sem horas '--- compara com data de hoje
Solving with PHP
One way to do it with PHP is simply to determine when the day begins on Unix Timestamp, and add 86399 seconds, and locate values on this track, thus:
$hoje = time(); // Pega o timestamp do servidor já em segundos
$iniciododia = ( $hoje % 86400 ) * 86400; // "arredonda" para 0h00 do dia
$fimdodia = $iniciododia + 86399; // e obtem o 23h59m59 do dia
Applying to your query:
"SELECT * FROM {$table_prefix}_Users WHERE last_used BETWEEN $iniciododia AND $fimdodia"
|
retorna dados entre 0h00m00 e 23h59m59 ---'
The given solution is for other days, changing the time()
for the desired day, but if you only want the current day, you can simplify even more:
$hoje = time(); // Pega o timestamp do servidor já em segundos
$iniciododia = ( $hoje % 86400 ) * 86400; // "arredonda" para 0h00 do dia
// ... desnecessário calcular o fim do dia para data atual ...
"SELECT * FROM {$table_prefix}_Users WHERE last_used >= $iniciododia"
If it’s only the count
Remember that it is unnecessary to bring all the data if you just want to know the count. The way your query original, you are bringing all the DB data to the desired date unnecessarily. Consider doing this:
"SELECT COUNT(*) FROM {$table_prefix}_Users WHERE last_used >= $iniciododia"
then just take the only value returned instead of the rowCount()
.
It depends on the database. If you can post what it is in the question, better, because this is something to be solved on the SQL side and not PHP side. Bringing in data to filter locally generates unnecessary traffic and processing. Most DB Engines allow you to do conversions locally, as well as get the current day of a date to compare to today’s
– Bacco
ola @bacco, I’m using Mysql.
– Dante
I gave a supplemented with some important considerations. Any questions let me know.
– Bacco