We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,608 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Time/Date Comparison and Timer

Hello,


What I am trying to do is restrict access to something if 48 hours hasn't passed since the last access.

I have a MySQL table with the last access time (col name: lastAccess).

If they are within the 48hours, deny access and tell them how many hours and minutes until they can access it again, otherwise, let them through,

Here is my not-working code.

$q = mysql_query("SELECT lastAccess FROM access");
$a = mysql_fetch_array($q);
$access = $a['lastAccess'];
if(strtotime("+48 hours", $access) >= time()){
	echo "Access Granted";
}else{
	echo "No Access! You have __ hours and __ minutes left.";
}

Thank you in advance for your help.

-- Turt2Live

2
Contributors
3
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
turt2live
Junior Poster in Training
90 posts since Jan 2011
Reputation Points: 15
Solved Threads: 3
Skill Endorsements: 0

MySQL returns dates and times in a different format than PHP, which uses timestamps. Use the MySQL function UNIX_TIMESTAMP to convert it in your query.

$q = mysql_query("SELECT UNIX_TIMESTAMP(lastAccess) FROM access");
burgercho
Light Poster
47 posts since Jul 2008
Reputation Points: 26
Solved Threads: 11
Skill Endorsements: 0

Alright, and how would I determine the hours and minutes until the lock is over?

turt2live
Junior Poster in Training
90 posts since Jan 2011
Reputation Points: 15
Solved Threads: 3
Skill Endorsements: 0

The rest of the code looked good for that, except the name of the array element would be UNIX_TIMESTAMP(lastAccess) so:

$access = $a['UNIX_TIMESTAMP(lastAccess)'];
burgercho
Light Poster
47 posts since Jul 2008
Reputation Points: 26
Solved Threads: 11
Skill Endorsements: 0
Question Answered as of 1 Year Ago by burgercho

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0805 seconds using 2.65MB