PHP newbie - time help

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Emprex
Posts: 1
Joined: Mon Mar 30, 2015 7:31 pm

PHP newbie - time help

Post by Emprex »

Hello Guys,

big fan of the site, I was just wondering if anyone can lend a hand in this code that i have. It works but it is terrible. I know that there is a much easier way.
BTW this is one of the first pieces of code that i have ran after looking at halls tutorials. But I am sure that you can tell by the code itself

Code: Select all

if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)){

    
    echo "Name: " . $row['name'] . " - Time " . gmdate("H:i:s", $row['time']) . "<a href='index.php?id=$row[id]&time=$row[time]'>Begin quest</a><br>";
    echo  "<br>" . time();
}}else {
  echo "0 results";
}}  
above is self explanatory, provides all the quests based on criteria (there is no criteria ATM).

Code: Select all

if (isset($_GET['time'])){
   
    $id=$_GET['id'];
    $time = $_GET['time'];

    $remaining = $time + time();
    
    
$updateplayer="update players set quest='$id' where name='player1'";
  mysqli_query($db , $updateplayer) or die("Could not update player");

$updateplayer2="update players set questt='$remaining' where name='player1'";
  mysqli_query($db , $updateplayer2) or die("Could not update player");
   echo "Quest started"; 
}
if ($playerinfo3['questt'] > 1){
    
    if (time() >= $playerinfo3['questt']){
    
        $updateplayer2="update players set questt='0' where name='player1'";
  mysqli_query($db , $updateplayer2) or die("Could not update player");
   
        
    }else{
      
   
    echo "You are already in a quest! come back after you have completed your current quest";
    echo  "<br>" . time();
    
}}


this is the code in question, basically it does a check to see if the questt(quest time is greater than 0) if so then it says that you cannot do another quest until the current one if finished. (again this works but needs to be improved)

The worst part is the way that I implement time again it works but it is terrible. it adds the quest length which is 500 seconds to cron time and then checks if cron time is greater or equal to the quest length before resetting the questt to 0.

Thank you for you help
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

Re: PHP newbie - time help

Post by vitinho444 »

What is the real problem? I think that way of doing cronjobs is really the best one. You store the time() variable + the interval and then check if time() is great than the other, if so then the interval has passed...
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Post Reply

Return to “Beginner Help and Support”