For the query below, how could I count the number of rows where datesent
is less than 24 hours old? (The field datesent
is a timestamp).
Thanks in advance,
John
$message = "SELECT datesent, recipient FROM privatemessage WHERE recipient = '$u'"; $messager = mysql_query($message); $messagearray = array();
If you want to select the last 24 hours from a datetime field, substitute 'curate()' with 'now()'. This also includes the time.
Here is the SQL to show latest time using now() function. Here is the SQL to get last 1 hour data in MySQL. In the above query, we select only those rows whose order_date falls within past 1 hour interval. We use INTERVAL clause to easily substract 1 hour interval from present time obtained using now() function.
Size − Datetime requires 5 bytes along with 3 additional bytes for fractional seconds' data storing. On the other hand, timestamp datatype requires 4 bytes along with 3 additional bytes for fractional seconds' data storing.
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision.
Use:
SELECT COUNT(*) AS cnt FROM PRIVATEMESSAGE pm WHERE pm.datesent >= DATE_SUB(NOW(), INTERVAL 1 DAY)
You can use the DATE_SUB function. Subtract one day from the current date in the where clause.
Something like
DATE_SUB(NOW(), INTERVAL 1 DAY)
EDIT: changed CURTIME() to NOW()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With