timestamp query doesnt work

Go To StackoverFlow.com

0

i have a timestamp problem. i would like to check a timestamp if that is in the past or future. if it is in the past, a query should happens.

up to this point i got a mysql-timestamp that will be fetched by

$row = $query->fetch_object();
$timestamp = $row->time

that contains:

2012-04-04 12:06:38

for further settings i use two more variables which will be:

$added = $timestamp + 1209600;  //what adds 14days (1209600 seconds) to my db-timestamp
$check = $added < time();  //what checks if the 14days have passed or not

okay now if i test all of these with var_dump it will return for each:

string(19) "2012-04-04 12:06:38" at var_dump($timestamp)
int(1211612) at var_dump($added)
bool(true) at var_dump($check)

so my problem is, that the following query will be executed all the time, even in case of when the 14days should have been passed. i know that because i changed the timestamp to earlier date in january. here is my query:

if ($check === true){
    $update = $db->query("UPDATE table SET xy='1' WHERE x='$x'");
    }

something seems to be wrong and i have no clue what that might be.

2012-04-04 23:34
by bonny
Let's see the query before $query->fetch_object(). Are you using UNIX_TIMESTAMP() function to convert a TIMESTAMP column into an integer in the query? If not, then $timestamp is a string - Marcus Adams 2012-04-04 23:49


1

I agree with barsju. The result from MySQL is a string of the DateTime entry and not actually a timestamp. Another option that would keep it within PHP is strtotime($string); that will translate the string into its timestamp. All that matters is that you get a timestamp before your check.

I would do it on this line of your example:

$timestamp = strtotime($row->time);

I believe there are also a couple other ways to get MySQL to return the timestamp instead as well.

2012-04-04 23:49
by gokujou
exactly, it was a converting problem. thanks a lot for helping - bonny 2012-04-05 00:05


1

Looks to me like your trying to add a string and and int.

Maybe you should try converting the timestamp to and int before adding your constant to it.

You can do it directly in the query when you are selecting the timestamp using for instance the TO_SECONDS() method:

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

2012-04-04 23:42
by barsju
And this might also give you some good ideas: http://us2.php.net/manual/en/ref.datetime.ph - uotonyh 2012-04-04 23:45
Ads