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 06-02-2008, 07:27 PM   #1 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default Comparing a timestamp field

How do I do comparison on a timestamp field in MySQL? Or is it possible to use columns as data for UNIXTIME() or TIMESTAMPDIFF()? I could fix this problem fairly easily by changing the data in my MySQL timestamp column to a unix timestamp from the get go, but now that I've run into the problem I want to figure out how to fix it.

Here's an example of what I mean:

PHP Code:
$szGuestTimeout time()-60*GUEST_TIMEOUT;
$szUserTimeout  time()-60*USER_TIMEOUT;

$q1 "DELETE FROM `".TBL_ACTIVE_GUESTS."` WHERE `last_active` < {$szGuestTimeout}";

$q2 "DELETE FROM `".TBL_ACTIVE_USERS."` WHERE `last_active` < {$szUserTimeout}"
The column 'last_active' is a timestamp column so it stores dates in the 0000-00-00 00:00:00 format, which I'm sure you know. So I tried doing this to the variables to see if a direct comparison would work:

PHP Code:
$szGuestTimeout date('Y-m-d H:i:s'time()-60*GUEST_TIMEOUT);
$szUserTimeout  date('Y-m-d H:i:s'time()-60*USER_TIMEOUT); 
But apparently it doesn't work that way. So then I thought maybe I could turn the column data into a unix timestamp with UNIX_TIMESTAMP but it doesn't seem to work if you add a column name as the data... so what should I do to perform a comparison short of switching the field to an INT and just using a regular unix timestamp as its data?
-m
delayedinsanity is offline  
Reply With Quote
Old 06-02-2008, 09:42 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

It'd be something like the following. Simply convert the date in the database to a unix timestamp on-the-fly, and compare it against the current timestamp.

sql Code:
DELETE FROM
    myTable
WHERE
    UNIX_TIMESTAMP(myLastActiveColumn) < UNIX_TIMESTAMP()
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
delayedinsanity (06-02-2008)
Old 06-02-2008, 09:49 PM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

AHHHHH. Once again I was right on top of the answer, and missed it by an inch... see, I tried using UNIX_TIMESTAMP() on the column and it didn't work. I wasn't sure if you could feed it a column as a value or if you had to enter a hard coded value of some sort, but it turns out that the reason it didn't work is because I had already tried converting the timeout variables with the date() function and never changed them back. So in essence, I switched it from trying to validate 0000-00-00 00:00:00 against 2133421342 to trying to validate 2133421342 against 0000-00-00 00:00:00 . *rolls eyes*

It never ends... never.
-m
delayedinsanity 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 12:01 AM.

 
     

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