View Full Version : Make a Time Limit

01-24-2010, 12:00 AM

what i try to do is , limit the User from Editing his Post in one Minute after that he can Not edit his post. after 60s

this wht i did and did not work .

1- get the time of the Post from the Mysql

$sql=mysql_query("SELECT * FROM test WHERE id='$id'")
or die("can't get the sql");
$row = mysql_fetch_array($sql);

2- Now See if the time in the Mysql Plus 60s is Passed ..

$time_now=time(); // time now notice that the time in thd database is stored in the same way
//after extracting data from the database
$database_time=$row['datetime']+60; //the time in the database + 60 seconds
echo "You are not allowed to edit ur post now";
echo "edit";

that did not work .. So whts wrong ..

01-24-2010, 12:13 AM
Can you confirm the `datetime` field value?

This is wrong:


That conditional statement says "if the time now is greater than the time stored in the db +60 seconds, disallow edit". This is the opposite of what you want. You do want the time now to be greater than the stored time +60. That means it's past the 60 second limit, and you can allow the edit.

01-24-2010, 12:38 AM
Hello again..

this Statment put the time in the table.

$datetime=date("d/m/y h:i:s"); //create date time

and after bring the time from the mysql , we Compare to the curent time . so the lets get the curent time .


now lets get the time when the the Comment was Posted.


Now i need the Correct Code to Make the time Limit .. . But seems Confusing to me

01-24-2010, 12:49 AM
$datetime=date("d/m/y h:i:s"); //create date time

That's not the same as the output from time(). The time() function (http://www.php.net/time) outputs a UNIX TIMESTAMP (UNIX EPOCH) value, e.g. '1264293695'. The date() function (http://www.php.net/date) creates a formatted date string based on the specifiers you pass, e.g. '01/23/10 16:43:00'. The first is an integer value, the second is a string.

The best way to do this IMHO is to use the MySQL DATETIME (or TIMESTAMP) data type in your `datetime` field. Then your SQL statement can perform the calculation. Please review the DATE & TIME data types (http://dev.mysql.com/doc/refman/5.1/en/date-and-time-types.html) and functions (http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html) in the MySQL reference manual (http://dev.mysql.com/doc/refman/5.1/en/).

At the very least make sure you're using compatible date / time values regardless of where the calculation is made.

01-24-2010, 01:13 AM
see this worked for me .

//check time

$datetime=time() ; //now time

$date = $row["datetime"] +60; // mysql time

if ( $datetime > $date) {

echo "no edit";
else {
echo "yes edit ";
//check time

But i echo the Time so user can see the time of the post , the time Now Prints as .

So now to conver the time back to normal format i did

$date = formatTimestamp($rows['datetime'],"m");

echo "$date";

It work Now .. but did i do all that correct