Jump to content

Displaying timestamp from db


Pain

Recommended Posts

Hi.

 

I have this code which input data to the database.

<?php


include("connect.php");

echo $date = date("Y-m-d H:i:s");
echo "\n <br />";

echo $date2 = date('Y-m-d H:i:s', strtotime("+20 seconds")); 
mysql_query("UPDATE users2 SET date2 = '$date2'");


$query = mysql_query("SELECT date2 FROM users2");
$numrows = mysql_num_rows($query);

if ($numrows != 0)
{
while ($row = $mysql_fetch_array($query))
{
	$date_final = $row['date2'];
}
}
echo $date_final;
?>

Variable date2 is stored as timestamp. Now i want to display this two variable on the web, but this code doesn't work. Why? Thank you.

Link to comment
Share on other sites

Oh one more thing.

 

I am trying to increment a variable by using timestamps.

<?php

$submit = $_POST['submit'];
include("connect.php");

echo $date = date("Y-m-d H:i:s");
echo "\n <br />";
if ($submit)
{
$i++;
echo $date2 = date('Y-m-d H:i:s', strtotime("+20 seconds")); 
mysql_query("UPDATE users2 SET date2 = '$date2'");


$query = mysql_query("SELECT date2 FROM users2");
$numrows = mysql_num_rows($query);

if ($numrows != 0)
{
while ($row = mysql_fetch_array($query))
{
	$date_final = $row['date2'];
}
}
}


if ($date > $date_final)
{
mysql_query("UPDATE users2 SET slaves = '$i'");
}
?>

 

However it doesn't increment at all. Is it possible to do this with php mysql at all? Or should i try using javascript, something like a countdown timer etc.

Link to comment
Share on other sites

1. Say time is 13.40.

2. Say we have a variable $i++;

3. When time is 14.00, increment $i.

 

Get it?:]

 

Not really, no.

 

All I can surmise form that is this:

 

$i = 1;
if( $time == '14:00' ) {
     $i++;
}

 

But I somehow suspect that isn't really what you mean.

Link to comment
Share on other sites

Is your slaves columns suppose to increment every 20 seconds, or only every 20 seconds while the user is online?  Is there a maximum number of slaves  at one time?

 

The reason all these questions are asked, is we can suggest a more efficient way, and possibly keep it in the database itself.

Link to comment
Share on other sites

Curious...

 

1. Will the database table EVER (other than initially creating the table) be empty?

2. Will $i ever contain a value other that that created in the script you have provided here?

 

1. Not it should never be empty.

2. No, i will probably input $i in to the database everytime the script is executed.

Link to comment
Share on other sites

Is your slaves columns suppose to increment every 20 seconds, or only every 20 seconds while the user is online?  Is there a maximum number of slaves  at one time?

 

The reason all these questions are asked, is we can suggest a more efficient way, and possibly keep it in the database itself.

 

It is supposed to increment everytime user clicks on a button and there is no maximum.

Link to comment
Share on other sites

See the comments in your code...

<?php
$submit = $_POST['submit'];
include("connect.php");

echo $date = date("Y-m-d H:i:s");
echo "\n <br />";
if ($submit){

  /* as you never set or change $i anywhere else, this will equate to 1. Is this your intent? */
$i++;

echo $date2 = date('Y-m-d H:i:s', strtotime("+20 seconds")); 

 /* this will set ALL records. is this your intent? */
mysql_query("UPDATE users2 SET date2 = '$date2'");

/* effectively this sets $date_final to the value of the last record */
/* if that is your intent, you are being redundant as date2 = $date2, therefore $date_final = $date2 */
$query = mysql_query("SELECT date2 FROM users2");
$numrows = mysql_num_rows($query);
if ($numrows != 0){
	while ($row = mysql_fetch_array($query)){
		$date_final = $row['date2'];
	}
}
}

/* if NOT submit, $date_final has no value nor been set */
if ($date > $date_final){
mysql_query("UPDATE users2 SET slaves = '$i'");
}
?>

Link to comment
Share on other sites

You will also be setting slaves to 1, so your users will never have more than 1 slave.

 

I would suggest leaving it up to the database.

 

UPDATE users2 SET slaves=slaves+1, date2=DATE_ADD(NOW(),INTERVAL 20 SECOND)

 

This will do the same as all of your code above, Although, I think you would want to specify a row for a specific user.

Link to comment
Share on other sites

  • 3 weeks later...

You will also be setting slaves to 1, so your users will never have more than 1 slave.

 

I would suggest leaving it up to the database.

 

UPDATE users2 SET slaves=slaves+1, date2=DATE_ADD(NOW(),INTERVAL 20 SECOND)

 

This will do the same as all of your code above, Although, I think you would want to specify a row for a specific user.

 

Thank you, your code helped a lot!

But how do i make slaves increment 20 seconds after the button is clicked? It just inserts the date + 20 seconds into the database and increments slaves right away.

Link to comment
Share on other sites

I'm not sure why you need it to wait 20 seconds before adding slaves.  Rather, you should be checking that 20 seconds has passed before adding the slaves to the table.

 

By checking if the date2 that is currently stored is less than the current time, we know that 20 seconds have passed, otherwise it will not update the column.  You should also be checking the user, so I threw that in there as well.

UPDATE users2 SET slaves=slaves+1, date2=DATE_ADD(NOW(),INTERVAL 20 SECOND) WHERE date2 < NOW() AND user = '$currentUser';

Link to comment
Share on other sites

I'm not sure why you need it to wait 20 seconds before adding slaves.  Rather, you should be checking that 20 seconds has passed before adding the slaves to the table.

 

By checking if the date2 that is currently stored is less than the current time, we know that 20 seconds have passed, otherwise it will not update the column.  You should also be checking the user, so I threw that in there as well.

UPDATE users2 SET slaves=slaves+1, date2=DATE_ADD(NOW(),INTERVAL 20 SECOND) WHERE date2 < NOW() AND user = '$currentUser';

 

OK i can now see some progress happening.

What happens now is:

Slaves increment when i load the page, not when i click on submit. Slaves increment straight away and will not increment for the next 20 seconds. after 20 s i can reload page again and slaves will increment again.

 

If i want to make it increment when i click on a button (not when i load the page) i should probably write your line somewhere in the if($submit) line?

Link to comment
Share on other sites

Please ignore my last post. I have removed everything redundant from the code.

 

<?php
include("connect.php");

echo $date2 = date('Y-m-d H:i:s', strtotime("+20 seconds")); 
mysql_query("UPDATE users2 SET slaves=slaves+1, date2=DATE_ADD(NOW(),INTERVAL 20 SECOND) WHERE date2 < NOW()");

?>

Now when the page loads, slaves is incremented instantly. After 20 seconds i can reload again, and slaves is incremented again as well.

One last thing that i want to do is to make this incrementation possible without reloading the page:) ?

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.