Forum Moderators: coopster

Message Too Old, No Replies

Subtracting time in PHP

         

stillsix

8:43 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



I realize time() gives me the number of seconds since 1970. I'm fine with that. What's the easiest way to go back say 20 minutes from now?

$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!

Andrew83

9:04 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



What is the type of the MySQL column you're matching? Maybe it isn't unix_timestamp(), but timestamp only :) you have to make sure they match. BTW, you can make your MySQL column TIME-DATE.

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.

Salsa

9:19 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



My guess is the same as yours, Andrew. Here's what I had keyed up:

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.

stillsix

9:20 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Yeah, I can't even get the time or the difference after I subtract 20 minutes. Not sure what the problem is.

And yes the starttime column in already in Unix Time Format since I use the time() to get the time when I create the INSERT Sql and through the time() in as a value.

stillsix

9:23 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Thanks to Salsa I got the print statements showing.

To answer your question the starttime column is in a long/big integer.

Andrew83

9:33 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Dude I'm asking for column type. That is not what you insert, but what it's defined. Here's a specific thing about the TIMESTAMP(14) column - it has nothing to do with your insert. It's value is the time in MySQL timestamp format, of the moment when you insert/update the column for the last time. BTW, an INT column won't be able to store a UNIX timestamp, because the data values requires more than 16 bits, otherwise said you should use a BIGINT column to save UNIX timestamp.

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);