Jump to content

timestamp , what's wrong ?


asmith

Recommended Posts


<?php
$conn = mysql_connect("localhost","john","qwerty");
mysql_select_db("test5",$conn);
$offset = '60'; // 1 min
$now = time();
$sql = "DELETE FROM azxc
        WHERE aaa + $offset < $now
       ";
if ( !$result = mysql_query($sql) )
{echo "no";}
else {echo "yes";
    
}
?>

 

 

aaa is timestamp ,  azxc the table name,

the out put is yes,

but when i check mysql data, nothing has changed !

 

aaa                              id

2007-12-04 11:08:02      1

2007-12-04 11:08:02      2

 

 

what is wrong with the code ?

 

 

 

Link to comment
Share on other sites

You can use the UNIX_TIMESTAMP function in mysql itself

 

"DELETE FROM azxc
        WHERE (UNIX_TIMESTAMP(aaa) + $offset) < UNIX_TIMESTAMP(now())
       ";

 

not too sure it will work but you can try it up since your doing a delete operation please backup your DB before you go ahead with it.

Link to comment
Share on other sites

The code you have will work without any fuss if you store the original timestamp as a Unix timestamp instead of a MySQL or other formatted timestamp. Unix is the native timestamp, and is easiest to perform operations on. Store it, compare it, pull it, but don't bother formatting it until it needs to be presented to the browser. Then just use date() to present it to a user. Simple...

 

PhREEEk

Link to comment
Share on other sites

well i fixed it ,

the problem was i record id 1 , 2 in mysql , which its time is the time of my windows, (GMT + 3:30 )

but in php code, the date()  time is in GMT ,  so  it wasn't time! i changed the date() in php to "now()"  which is mysql , and worked out !

2 question poped up  !

1. how can i change the mysql time to GMT .

2. mysql store the time like  YYYY-MM-dd HH:mm:ss, 

it is for timestamp , can i compare a value with this type which is recorded in a varchar field to a timestamp record ?

Link to comment
Share on other sites

You can do anything you want with timestamps and date formatting... but it is all a bunch of wasted overhead programming. I told you above how to make a timestamp field and just store a native Unix stamp. Your database does not care one bit how pretty your timestamp is when formatted, so store it as Unix and format for someone with a browser who can appreciate a nice formatting.

 

PhREEEk

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.