Jump to content

Minusing Dates from Dates


cpd

Recommended Posts

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Yesideez: I'm talking a bit mockingly, a bit jokingly here :P

 

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).

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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);

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.