View Full Version : Query timestamp

01-26-2012, 03:27 PM
Hello guys I need a little help. the application i ma using stores a timestamps in the database that looks like this 1327582975 I am trying to get data from the database based on the timestamps so here is my code. please tell me where i am going wrong.

$today2 = date("Y-m-d");
$query_str = "SELECT addtime , COUNT(addtime) AS newmembers "
." FROM signup "
." WHERE DATE_FORMAT(addtime, '%Y %m, %d') = '$today2' ";

01-26-2012, 08:06 PM
Your format is different. Your $today2 is in format 2012-1-26 while your date_format in SQL is 2012 1 26. You can bypass this directly:

$query_str = 'SELECT addtime, count(addtime) AS newmembers
FROM signup

I *believe* that the unix_timestamp will work with current_date to push it to midnight this morning.

01-26-2012, 08:13 PM
so in that case would this be correct

$query_str = 'SELECT addtime, count(addtime) AS newmembers
FROM signup

01-26-2012, 08:31 PM
I can't test this (and far from a SQL expert, but need to get re-practiced up), but I believe that any DateTime handling type in SQL overloads +/- as you have above without an explicit call to DATE_ADD/DATE_SUB.
If it syntax errors out, then use UNIX_TIMESTAMP(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH)) (but as mentioned I believe what you have will work perfect).

01-26-2012, 09:02 PM
your right is does work mine and your I tested it. but it adds all of the sign ups for the pass month. another question

why is this not working BTW the viewtime Column has data like this "2012-01-26 01:56:43" basically timestamps

SELECT viewtime, COUNT(viewtime) AS dash_daily_view_Number
FROM video
WHERE DATE(viewtime) = MONTH(NOW(viewtime))
GROUP BY viewtime
ORDER BY viewtime

01-26-2012, 09:41 PM
viewtime is a datetime datatype from the looks of it. You are comparing only a month, which is a numeric datatype to a date. NOW() also doesn't accept any arguments.
Pull both as month: WHERE MONTH(viewtime) = MONTH(NOW()), which will match anything from the start of the current month (as opposed to within the past month, which is what I figure you are trying to replace).