Showing Message for set time after date

I don’t really know how to title this, so let me know if there is a change I should make.

Right now, I have a system that records the current time (Using date('m-j-Y-G-i-s') )
It records this in a database table called “Account_x” and a column called “Creation_time”.
In the database, it looks like this: 11-1-2021-18-38-21

I want to show a div element for 25 minutes after the account is created. Basically, if the visitor is on the page, and the there account is less than 25 mins old, show the div. If the account is more than 25 mins old, don’t show the div. I only want the system to check the age of the account of page load, so a continuous checking script is not needed.

Basically, if the user loads the page, and the account is less than 25 mins old, show the div, if not, don’t show it.

I hope that makes sense, and thanks for any help!

Are you aware that MySQL has a column parameter which has a “creation timestamp” value?

This could be used to check for the seconds difference between the “creation timestamp” and PHP time(); function

Do you want the <div> to disappear after 25 minutes even if they don’t reload the page? If you do, I suspect you’ll need to add some Javascript to start a timer for (creation time - current time + 25s) and then call a function to hide it.

Otherwise, it’s surely just a case of having the page draw code check the creation time when it renders the page, and if it’s less than 25 minutes before current time, don’t draw the <div>? Show us the code you’ve tried, and I’m sure someone will spot why it’s not working for you. I suspect using a proper “datetime” column in your database table instead of what looks like a string might make it slightly easier, though maybe not much easier.

1 Like

Unfortunately not. I have only been using PHP for about a month, and only simple commands at that.

Nope, it should only check on pageload.

I don’t have any. This goes back to the back that I don’t know much about PHP. If someone has code that works, awesome! If not, a link to a tutorial or something helpful would work as well. My google searches have turned up nothing, but I think that is because I don’t know what to call this.
Thanks!

How far have you got? I see that you have code that records information into a database table - do you have corresponding code that will retrieve that information based on the user account information?

No.

I have a database that has the username and time stored in it. When the account it opened, the structure is like this:
http://domain.com/account/viewaccount.php?account_id=acc_98064253
The database stores “Account_username” (acc_98064253) and “Creation_time” (11-1-2021-18-38-21) in a table called “Account_x”.

Account_x
==========================================
| Account_username |    Creation_time    |
|------------------|---------------------|
|   acc_98064253   |  11-1-2021-18-38-21 |
==========================================

When http://domain.com/account/viewaccount.php?account_id=acc_98064253 is loaded, it should check if it has been less than 25 minutes of the creation of that account, and if so, display the div.

Thanks!

OK, so your first step is to write some code to open the database, retrieve the creation time based on your account id, and just display it. So you’ll need to:

  • get the account id from the URL in the $_GET array
  • connect to the database
  • run a query to retrieve the creation time
  • display that on your browser screen

Those are all very basic PHP steps (once you know how, of course) and you’ll easily be able to find sample code and tutorials to do these steps. If you find tutorials and samples that use the old mysql_ functions, avoid them - those are old and won’t work in current PHP.

Once you have that working, the only two other parts you need are

  • compare the creation date/time to the current date/time
  • add an if clause around the code that displays your <div> to only do so if the time allows it.

Out of interest, what does your current viewaccount.php code do? Or don’t you have that yet?

I’m going to reply to this later with better detail, as something else got totally messed up (And its totally stressing me out).

It shows some other account info taken from the same database at the moment.

So I basically need to get the date and time stored in the database, compare it to the current date and time, and show the div if the difference is less than 25mins? The page is already connected to the database, but how would I go about comparing the times, and displaying the div if the difference is less than 25 mins?

Thanks for your help!

Beware there are so many options and different methods to handle date and time formats especially when the datetimezone is brought into the equation!

Instead of storing the date as a string I would recommend using timestamp which is the numeric of elapsed seconds since 1970-01-01-00:00:00 I think from memory.

Check the following:

$intNow  = time();
$intThen = time() - 12*60; // minutes * seconds

// NOT REQUIRED 
   // $sNow    = date('m-j-Y-G-i-s', $intNow);
   // $sThen   = date('m-j-Y-G-i-s', $intThen);

$iDiff   = $intNow - $intThen;
$elapsed = date('i:s', $iDiff);

// DISPLAY RESULTS
echo "
  <div>
    intNow  ==>  $intNow  (seconds - integer) <br>
    intThen ==>  $intThen (seconds - integer ) <br>
    elapsed ==>  $elapsed  (Minutes - seconds - string) 
  </div>   ";

Results:

$intNow ==> 1635919552 (seconds - integer)
$intThen ==> 1635918832 (seconds - integer )
$elapsed ==> 12:00 (Minutes - seconds - string)

Useful URLs

https://www.php.net/manual/en/function.time.php

https://www.php.net/manual/en/function.date.php

https://www.php.net/manual/en/function.strtotime.php

OK, but that does cover most of the first section of my suggestion - all the stuff about connecting to the database and running a query is already done for you. So my suggested first step is to modify the query to retrieve the creation time and just display it somewhere on the page.

I understand that you have no background in PHP, but I presume if you’re looking after this site with a view to modifying it, you are intending to learn some PHP. So as you already have code that does connect to the database, run a query and do something with the results, you’ll be able to read through that code and, along with the PHP documentation, figure out which bit does what.

As for the comparison, @John_Betong makes a very good point. As you seem to be storing the creation time as a string rather than any of the date/time column types, it may make things more complex. So if you’ve only just added that column, now would be a good time to make the change.

2 Likes

What has worked for me in the past and currently is rather than using the database, use a cookie.

How would that help in this particular instance? The OP is already accessing the database when the page is drawn, so it’s just a case of comparing the current time to the creation time, and if the difference is less than 25 minutes, drawing a div or not drawing it. All I can think of here would be that you would set the cookie if the page-draw is within 25 minutes of account creation, and then use that cookie to decide whether to show the div. I haven’t done much with cookies, but that just seems to be adding an extra step to me - can you explain how it would help here?

In any case, the OP hasn’t come with any more detail as they indicated they would, so they’ve probably found a solution elsewhere.

The other issue was solved last night, and I am hoping to run some tests today. If all goes well, I should reply here again later today with more info relating to my project.

I know next to nothing about those (I only know what they do and how they are stored), and like @droopsnoot said, I don’t see how that will help.

:+1:

I’ll take a look at that later today, it sounds promising.

So just to be clear, I can use the “timestamp” function to save a number when the user is registered and check if the current time is x higher than is in the database (X representing 25mins). If the number is below 25mins of time difference, show the div, if not, don’t show the div.

If you cannot tell, I am a bit confused. Could you maybe explain the process using this “time stamp” thing?

Thanks for everyone’s help so far!

I don’t know PHP, but on the account created post page you create a cookie with an expiration date. Then on the page you want your DIV message you request the cookie you created and if 25 minutes have expired then they won’t see the message. You accomplish this with a conditional if/then statement.

Using a database will certainly work too.

Another idea that is simple to do is that once the account is created you do an auto redirect to your landing page with a QueryString. You have an if/then statement that if coming from that QS then display the message, else display nothing.

Indeed, similar approaches to the same thing - an if/then to decide whether to draw the required div.

Please try the script in post #9, there are only four relevant lines of code, the remainder is to render the results.

If there is anything unclear after running the script please post your questions.

I’m assuming you mean these 4 lines. So you’re setting the variable intNow to the current time, and intThen to the current time minus 12 mins? It looks like the variable iDiff is subtracting the current time minus 12 mins from the current time, but that kind of confuses me.

So do I replace date('m-j-Y-G-i-s') in my code with time()? What code would I place in the file that shows the div then? The file you linked shows how to store the time, but not how to compare it to the current time. Am I missing something?

Thanks for your help!

Try this instead - I have used a $_SESSION variable to store the $intThen value in seconds. This value should be set and retrieved from the database table:

<?php declare(strict_types=1);

$MINUTES = 25; // change to monitor results


# SET THEN
	session_start();
	if( isset($_SESSION['intThen'])) :
		# echo '<br> Already set: '. $_SESSION['intThen'];
	else:
		$_SESSION['intThen'] = time();
	endif;	
	$intThen = $_SESSION['intThen'];
	$intNow  = time();

# MAYBE DISPLAY DIV
	$iDiff   = time() - $intThen;
	$elapsed = date('i:s', $iDiff);

	$maybe = 'Sufficient time has elapsed to show this DIV';
		if( $iDiff <= $MINUTES * 60 ) :
			$maybe = 'NOT ENOUGH TIME HAS ELAPSED TO SHOW DIV';
		endif;	  
	echo '<h2>' .$maybe .'</h2>';

	echo "
				<h3> 
					Set \$MINUTES ==> $MINUTES 
				</h3>
				<h4>
		    	elapsed ==>  $elapsed  (Minutes - seconds - string)
		    <h4>	

		    intNow  ==>  $intNow  (seconds - integer) 
		    <br>
		    
		    intThen ==>  $intThen (seconds - integer )
		  	";

Output:

I must be missing something here, what do I add to the database? The session variable only lasts in one browser, so do I save its contents?

I understand that adding the code to my website will show the time elaped since creation, but how can I get it to show the div?

It is time() which is the instantaneous integer value of the session which should be saved the the database field.

From your first post:

Right now, I have a system that records the current time (Using date('m-j-Y-G-i-s') )
It records this in a database table called “Account_x” and a column called “Creation_time”.
In the database, it looks like this: 11-1-2021-18-38-21

To reiterate, instead of saving the string result of date(…), instead save the integer result of time()

Later… test the original integer saved result of time() against the current time() value and MAYBE display the Div.

Edit:

In the script I supplied, try changing the $MINUTES value, run the script and notice the contents of H2 which is the dynamic value of $maybe. Change the H2 to a Div and send an empty value when elapsed time has expired. or do not send anything, up to you :slight_smile:

Did you try running the revised supplied script?