Forum Moderators: coopster
$timerange = time() - 1200; //20 minutes from now
printf("Time:", time());
printf("<BR>");
printf("20 mins ago:", $timerange);
I think I'm mixing my data types up because the below didn't work. And does the time result come back in an array or can I use it straight up? Is it a string or something else because then I just want to use the result and query with the time I come up with:
$q1 = "select * from tablename where starttime > '".$timerange . "'"; //starttime in same format as time()
Thanks in advance!
strtotime($any_date_format);
The strtotime() funciton will give you the UNIX timestamp, no matter the time format you're applying to it - MySQL timestamp, Y-m-d formatted dates, etc.
If your column is in UNIX timestamp format (time() returns current UNIX timestamp, not just seconds after 1970 :P ), then I see no problem for this not working, but I really believe it isn't.
Is your starttime column a TIMESTAMP(14) type? If it isn't, you'll need to use some internal functions to do the comparison. Also, you want your query to return only records from the last 20 minutes, right? If both of those questions are true, it looks to me like your query should work.
The time() result comes back "straight up," and you should be able to echo out $timerange to see the 14 place integer. To get anything meaningful out of your calls to printf(), however, I think you'll have to add some formatting arguments, like:
printf("Time: %d", time()); printf("20 mins ago: %d", $timerange); I hope this helps.
Additionally you can also use the followin MySQL query to grab needed data as well:
SELECT * FROM tablename WHERE starttime > unix_timestamp(NOW() - INTERVAL 20 MINUTE);