It is currently Thu Mar 28, 2024 10:05 am


All times are UTC


Forum rules


Please click here to view the forum rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: php code help please
PostPosted: Tue Jan 17, 2006 9:33 pm 
Noobie
Noobie

Joined: Tue Jan 17, 2006 3:05 pm
Posts: 18
ok im tring to have my webpage read my computer clock translate that time into seconds and save it to a file and then the next time you veiw the page it will veiw the file that u saved the time in and write it for a variable and then reread the time. now trasslate it into seconds and then find the difference between the two times and display the difference but for some reason it displays the difference as 0 witch it is not acctualy zero i was wondering if someone could help here is the part of the code that i am talking about :
<?php
// begin time
$day=date("d");
$hour=date("G");
$month=date("m");
$second=date("s");
$minute=date("i");
$minute*=60;
$hour*=3600;
$day*=86400;
$month*=2592000;
$time=$second+$minute+$hour+$day+$month;
echo $time;
if (!($f=fopen("/home/www/mikveh2.mybesthost.com/time.txt","r")))
exit("Unable to open file.");
while (!feof($f))
{
$x=fgetc($f);
$twotime=$x;
}
fclose($f);
echo $twotime;
$timesub=$time-$twotime;
echo "<br>";
echo $timesub;
$wtime=$time;
$f = fopen("/home/www/mikveh2.mybesthost.com/time.txt", "w");
fwrite($f, $wtime);
fclose($f);
// end time
?>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 9:54 pm 
Noobie
Noobie

Joined: Tue Jan 17, 2006 3:05 pm
Posts: 18
could it have anything to do with permissions what permision number are files that can be edited suposed to be on


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 11:22 pm 
Posts like crazy!
Posts like crazy!

Joined: Mon Sep 26, 2005 5:03 pm
Posts: 204
Location: The Netherlands
The error is in this part:
Code:
while (!feof($f))
{
$x=fgetc($f);
$twotime=$x;
}

change it to:
Code:
$twotime = fgets($f);


Also why use all the date() functions if u can just do 1 time();

this does the exact same as your code but much easier
Code:
$file = "time.txt";

$time = time(); //UNIX timestamp, amount of seconds since 1-1-1970.

if(($f = @fopen($file, "r")) == false) { //Open the file.
   if(!file_exists($file)) { //If it doesnt exist, make it.
      $f = fopen($file, "w");
      $oldtime = 0;
   }
   else //If it doesn't open.
      exit("Unable to open the file");
}
else
   $oldtime = fgets($f); //Get content.

fclose($f);

$f = fopen($file, "w");
fwrite($f, $time);
fclose($f);

$timesub = $time - $oldtime;

echo $time."</br>".$oldtime."</br>".$timesub;


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 11:24 pm 
Noobie
Noobie

Joined: Tue Jan 17, 2006 3:05 pm
Posts: 18
thankyou very much i will try this


Last edited by solarwar on Tue Jan 17, 2006 11:39 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 11:27 pm 
Noobie
Noobie

Joined: Tue Jan 17, 2006 3:05 pm
Posts: 18
well i need it the way it is because i am working on a site that u make a sertain amount of resources every second and u build armies and attack people but i am rather new to php but im good with other languages like c++ and stuff


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 11:36 pm 
Posts like crazy!
Posts like crazy!

Joined: Mon Sep 26, 2005 5:03 pm
Posts: 204
Location: The Netherlands
Your script has a few faults in it... First you use the same mount of seconds for every month.. but months have different lengths.

Second.. if you use the script exactly 1 year after the last time it is still only 0 seconds difference..

And after all I don't see why you wouldnt be able to use time(); Which does exactly what you do only it has a different offset...
It would also be better to use the database if you ask me..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 11:44 pm 
Noobie
Noobie

Joined: Tue Jan 17, 2006 3:05 pm
Posts: 18
well i already new about the year thing and the month thing but i will change that after i finish the building setups and u havent seen my whole script that was from a different file but i need the seconds thing for now because of the way i wroght something else but i may edit it in the future
also i just started working on it yesterday so it definitly had some bugs i just finished fixing most them in what i have written so far


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 11:46 pm 
Posts like crazy!
Posts like crazy!

Joined: Mon Sep 26, 2005 5:03 pm
Posts: 204
Location: The Netherlands
Well i don't know how the rest works but it's best to set a bug free base before going further. :/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ]  Moderators: fhmagic, KJ, Moderators, Support Team

All times are UTC


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
100WebSpace © 2011