Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP timestamp to/from file

Status
Not open for further replies.

Yaik

Programmer
Oct 1, 2010
36
US
Hey guys, I would like to know if you guys can help me make a simple PHP script that will:

1-Have a button, and when you press that button it will produce a time stamp and display it on the screen.

2-Save that timestamp to a file (e.g. timestamp.txt).

3-Next time the page is loaded, the timestamp from the file will be displayed.

p.s. If you can, have it so that if 2 hours haven't passed yet, they get some time of alert letting them know to wait 2 hours before pressing the button again.

Thanks.
 
We are here to hep you if you have issues with your existing code, not to write the entire code for you.

I suggest you look at the time() , date() , fopen() , fread() and fwrite() functions in the php online manual as a starting point.






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
ok, so I do have the code to have a sort of time stamp for the site.

Code:
function getTime()
{
  // create the xmlhttp object
  xmlhttp=GetXmlHttpObject();

  if (xmlhttp==null)
  {
    alert ("Your browser doesn't support AJAX or you are blocking Javascript");
    return;
  }

  // define a variable that indicates which backend PHP script we want to run
  var url="time.php";
  xmlhttp.onreadystatechange=stateChanged;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}

function stateChanged()
{
  if (xmlhttp.readyState==4)
  {
    // modify the HTML inside of the "myResponse" div tag
    document.getElementById("myResponse").innerHTML=xmlhttp.responseText;
  }
}


function GetXmlHttpObject()
{
  if (window.XMLHttpRequest)
  {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }
  if (window.ActiveXObject)
  {
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  return null;
}
</script>

The 'time.php' file contains this:
Code:
<?php 
echo date("m/d/y : H:i:s", time()) 
?>

and I am displaying the information by doing this

Code:
<input type="button" value="Run Script on All Computers" style="height: 25px; width: 180px" onClick="getTime();"><div style="float:left"; id="myResponse"></div>');

Now, what I would like help doing is being able to capture that time stamp and put it in a file, and then when the site is reloaded, it displays it.

I am not very good with PHP at all, and I have tried to get it to work, but it was just a big mess.

Here is an example of what I tried to do, which did absolutely nothing.
Code:
<?php 
$myFile = "timestamp.txt";
$fh = fopen($myFile, 'w');
?>
<input type="button" value="Run Script on All Computers" style="height: 25px; width: 180px" onClick="getTime();"><?php $stringData = "<div style='float:left'; id='myResponse'></div>"; fwrite($fh, $stringData);fclose($fh);?>');


 
I'd say if you want a different timestamp per visitor, then a cookie may be a better option. If you want to maintain the same timestamp for all visitors then a server file would be better.


From your code something like this should work for a general sever file:

Code:
<?php 
$filename="path/to/file.tmp";
if(file_exists($filename)){
$fileHandle=fopen("path\to\file.tmp","r");
$timestamp=fread($handle, filesize($filename));
}
else{
$timestamp=date("m/d/y : H:i:s", time()) ;
$fileHandle=fopen("path\to\file.tmp","w+");
}


$f=fwrite($fileHandle, $timestamp);

fclose($filehandle);

echo $timestamp;

?>


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top