TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-13-2008, 04:44 AM   #1 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default unix timestamps and group by

OK, i'm having trouble with a query that I hope someone can help me with...

I have a table that stores signup dates in unix timestamp format and I'm having problems trying to output data in this way:

date | signups
02-01-2008 | 5
02-02-2008 | 50
02-03-2008 | 10
02-04-2008 | 20
02-05-2008 | 25


SELECT sup_date, COUNT(id) FROM users WHERE sup_date > "1199145600" AND sup_date < "1202774400" GROUP BY sup_date

is there a way to convert the timestamps to output dates???

my goal is to be able to query the amount of signups for each day for the past 30 days - maybe i'm going about it wrong..
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Old 02-13-2008, 05:14 AM   #2 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default

i figured it out, here's what i did if this is useful to anyone else

SELECT DATE_FORMAT(FROM_UNIXTIME(sup_date),"%m-%d-%Y") as sdate, COUNT(id) as scount FROM users WHERE sup_date > "1199145600" AND sup_date < "1202774400" GROUP BY sdate

but if anyone want to give me pointers on how I can change this query to automatically set 30 days from today - that would be super :)
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Old 02-13-2008, 12:30 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

SQL Code:
SELECT
    DATE_FORMAT(FROM_UNIXTIME(sup_date), '%m-%d-%Y') AS sdate,
    COUNT(id) AS scount
FROM
    users
WHERE
    -- post date must be between NOW and 30 DAYS ago
    sup_date <= NOW()
    AND sup_date >= DATE_SUB(NOW(), INTERVAL 30 DAY)
GROUP BY
    sdate
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
webosb (02-13-2008)
Old 02-13-2008, 02:41 PM   #4 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
SQL Code:
SELECT
    DATE_FORMAT(FROM_UNIXTIME(sup_date), '%m-%d-%Y') AS sdate,
    COUNT(id) AS scount
FROM
    users
WHERE
    -- post date must be between NOW and 30 DAYS ago
    sup_date <= NOW()
    AND sup_date >= DATE_SUB(NOW(), INTERVAL 30 DAY)
GROUP BY
    sdate

I had to change it a lil bit to:

WHERE sup_date <= UNIX_TIMESTAMP(NOW())
AND sup_date >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))

but it works great Salathe. much thanks. you've been very helpful
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:20 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design