cpd Posted December 14, 2008 Share Posted December 14, 2008 I want to stop someone from accessing something before 48 hours has passed since the last time the accessed it (Which ive managed to do). Then i want to tell them how much time they have left... So the time they last accessed it is in my table as "Y M d H:i" EG "2008 Dec 13 12:41". Ive managed to work out how to stop them from accessing it but i now want to tell them how much time they have left before they can access this "area" again. To find out if 48 hours has passed i did: if($Last_attempt > date("Y M d H:i",strtotime("-2 days"))) { Where $Last_attempt is in the same format as the date(). I now want to return the time left...How do i do this? Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted December 14, 2008 Share Posted December 14, 2008 Are you actually storing the date in the database as a formatted string? Or are you using a date/timestamp on the database? If you're not using a timestamp, then you should be. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 A Unix timestamp will make it a lot easier - you can get the current from PHP's time() function - it returns a long integer. Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 14, 2008 Share Posted December 14, 2008 As usual I will make my point, that DATETIME or TIMESTAMP columns should be used. Quote Link to comment Share on other sites More sharing options...
cpd Posted December 14, 2008 Author Share Posted December 14, 2008 Ok so say i use them TimeStamp columns, how do i change it into the format of Y M d H:i and Y M d Using explode or something? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 There's no "should" about it - either way it'll work. Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 14, 2008 Share Posted December 14, 2008 Both will, but mine's better CKPD: Use strtotime and date functions Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 There's no "better way" it's all down to the individual and which way you get along with. By all means go down the path you mention - I might learn something here as well (will be watching) as I find the way you mention a complete muddle. I've only used the time() method as it works for me. Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 14, 2008 Share Posted December 14, 2008 Yesideez: I'm talking a bit mockingly, a bit jokingly here Here's my method for working with dates in MySQL 1. I use DATE, DATETIME, TIME or TIMESTAMP column in MySQL 2. When storing dates to database I use date to format it into MySQL edible form ("Y-m-d H:i:s" for example). For storing current time, I use either MySQL's NOW(), or TIMSTAMP's special features (DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP). 3. When selecting dates from database I use strtotime to convert it to Unix timestamp (usually together with date to format it for display). Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 I know - sometimes emotes aren't enough Quote Link to comment Share on other sites More sharing options...
cpd Posted December 14, 2008 Author Share Posted December 14, 2008 So should i simple store it as a big time stamp manually. Because im not EVER going to use seconds what so ever end of. thats why i dont want it. I also wont need the "Time" in many circumstances therefore i saw no point in using timestamp in the PHPmyadmin Also, when getting the timestamp say $TIMESTAMP What would strtotime($TIMESTAMP) do and how do i format it into Y M d H:i ? Then do the time they last attempted to enter this section take away 2 days and then print that out? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 14, 2008 Share Posted December 14, 2008 Here's how I would do it... Have the date check field in the database set to INT UNSIGNED. When they access it save the value from time() in it. Next when they try and check read the value back and check it against time()-(3600*48) and if the value read back is higher/lower (can't remember which) they need to wait some more. Just subtract one from the other to get the number of seconds until they can view it next - display using date() I'm interested to see how Mchl does it. Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted December 14, 2008 Share Posted December 14, 2008 Saving as a date/time stamp allows a lot more flexibility in building queries e.g select all entries for February 2009 select all entries in the next 48 hours select all entries more than 48 hours old These can all be done in the SQL query without any need to calculate values in PHP, or to retrieve more data from the database than is necessary and then filter it in PHP Quote Link to comment Share on other sites More sharing options...
cpd Posted December 14, 2008 Author Share Posted December 14, 2008 Ok so i do it with the time() function. how do i then change that into the format of d H:i Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 14, 2008 Share Posted December 14, 2008 I'm interested to see how Mchl does it. Save the current time using MySQL's NOW() into DATETIME field. Then use MySQL's TIMEDIFF() to see if 48 hours passed since the item was last accessed. SELECT * FROM table WHERE HOUR(TIMEDIFF(NOW(),lastAccessed)) >= 48 AND itemID = ? Ok so i do it with the time() function. how do i then change that into the format of d H:i date("d H:i",$timestamp); Quote Link to comment Share on other sites More sharing options...
cpd Posted December 14, 2008 Author Share Posted December 14, 2008 and is the $timestamp just time()? Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 14, 2008 Share Posted December 14, 2008 For current time just do: date("d H:i"); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.