Forum Moderators: coopster

Message Too Old, No Replies

Compare time to allow users to edit info until 30 before entered time

         

dbzfyam

12:32 pm on Jan 13, 2008 (gmt 0)

10+ Year Member



I need to compare the time entered in the database (for example 18:15) with the current time to see if the user may still edit their reservation. They should be allowed to edit their reservation until 30 minutes before the entered time.

So when the time entered is 18:15, they should not be allowed to edit it anymore after 17:45. Does anyone know how to check for this?

Help is greatly appreciated.

Thanks in advance,
Stefan

venelin13

1:37 pm on Jan 13, 2008 (gmt 0)

10+ Year Member



Hello,
there are two ways to do it. In the both ways you should store the posting date for each entry. The column should DATE or DATETIME type.

#1 - at the databse level. You need to modify the SQL query like this:

"select * from my_table where DATE_ADD(posting_date, INTERVAL 30 MINUTE) < NOW()"

my_table - the table name.
posting_date - the column where you store the posting date.

If the query return a result, the user may edit his entry. Else - the 30 minutes has passed.

#2 - using PHP. After you retreive the entry information, including the posting date. Lets suppose all the information is retreived as array with name $data.

if($data < date("Y-m-d H:i:s", time() - 1800)){
//the user should be able to edit his entry
}

These may not work immediately, I did not tested, but this is the way.

dbzfyam

2:18 pm on Jan 13, 2008 (gmt 0)

10+ Year Member



Thanks for the fast reply. I just tried it and I think it should work. I do however have a big problem. I currently have 2 shared webservers with 2 different webbhosts.

When I run this (to test the time):

echo date("Y-m-d H:i:s");

I get 2 different times:
My main server: 2008-01-13 14:12:53
My backup server: 2008-01-13 15:12:30

The time from my backup server is the correct one. Is there a way to retreive the correct time when the time of the server is incorrect?

venelin13

3:34 pm on Jan 13, 2008 (gmt 0)

10+ Year Member



You should put the both servers in one time zone.

If you have PHP5 installed, use the date_default_timezone_set() [php.net] function.

Else, you may change the time zone via .htaccess file:

php_value date.timezone Europe/London

Start a phpinfo() stript at your backup server and look for the "date.timezone" value.

dbzfyam

4:02 pm on Jan 13, 2008 (gmt 0)

10+ Year Member



Unfortunately, I'm still running php 4.4.8. I've found a small workaround though, which should do the trick:


$dbdatetime = "16:28"; // Stored in DB like this
echo "Database time: ".$dbdatetime."<BR><BR>";
echo "Server time: ".date("H:i")."<BR>";
echo "Server time + hour (GMT + 1): ".date("H:i", time() + 3600)."<BR>";
echo "Server time - 30 min: ".date("H:i", time() + 1800)."<BR>";
if($dbdatetime < date("H:i", time() + 1800)){echo "<BR>Allowed";}
else {echo "<BR>Not allowed";}

Thank you for your help!