Eurogangster Posted January 18, 2020 Share Posted January 18, 2020 hello all i need your help to solve this issue. I created a function where users purchase with point bodyguard protection for 18 hours and this means they cant be killed. I added the timer in the loggeding.php. Everything is going good until the timer is 00:00. It roll over again and starts from the beginning and i cant find why the code of the timer is this : new hook("userInformation", function ($user) { global $page; $currentTime = new DateTime(); $bodyguardTime = new DateTime(); $bodyguardTime->setTimestamp($user->getTimer("bodyguard")); $timeRemaining = $currentTime->diff($bodyguardTime); $timeRemaining = $timeRemaining ->format(" %h hours, %i minutes "); if (($timeRemaining) > 0) { $page->addToTemplate('bodygua_timer',$timeRemaining); } else { $messagebodyg = 'No Protection'; $page->addToTemplate('bodygua_timer', $messagebodyg); } }); Quote Link to comment Share on other sites More sharing options...
sniko Posted January 18, 2020 Share Posted January 18, 2020 a) $timeRemaining should be an integer - make it the number of seconds (%s) instead of a string of hours and minutes b) Format the first occurence of addToTemplate to be the string you have in line 8 c) How is "bodyguard" timer updated - what is it updated with? Quote Link to comment Share on other sites More sharing options...
Eurogangster Posted January 18, 2020 Author Share Posted January 18, 2020 When someone purchase the bodyguard protection : Can you rewrite my code to the correct one because my brain is out of order after many hours of trying. case "bodyguard": if($qty>1){ $this->error('You can buy one Bodyguard Protection each time.'); } else if($this->user->checktimer("bodyguard")){ $this->user->set("US_points", $this->user->info->US_points - $cost); $this->user->updateTimer("bodyguard", time() + (1 * 64800)); $this->user->updateTimer("bodyguardpurchase", time() ); $this->error('You paid ' . $cost . ' {_setting "pointsName"} for 18 hours of Bodyguard Protection', "success"); }else{ $this->error('You have already purchased Bodyguard Protection. You can buy one each time.'); } break No one can help me with this ? Quote Link to comment Share on other sites More sharing options...
Dave Posted January 20, 2020 Share Posted January 20, 2020 @Eurogangster as @sniko (and I previously on Discord) said you're comparing a string to an integer, this check is going to create some weird results. Diff returns a string and shouldn't be used for comparison. You should actually just compare between the two DateTime instances you already have declared, the diff call just gives you the output to display to your users. Take the following example from PHP.net (https://www.php.net/manual/en/datetime.diff.php). This shows the 3 different types of comparisons you can do on DateTime <?php $date1 = new DateTime("now"); $date2 = new DateTime("tomorrow"); // Are the dates the same? var_dump($date1 == $date2); // Is date 1 before date 2? var_dump($date1 < $date2); // Is date 1 after date 2? var_dump($date1 > $date2); ?> In your case you're wanting to compare if the bodyguard timer is greater than the current time. $currentTime = new DateTime(); $bodyguardTime = new DateTime(); $bodyguardTime->setTimestamp($user->getTimer("bodyguard")); if ($bodyguardTime > $currentTime) { // Has a bodyguard $timeRemaining = $currentTime->diff($bodyguardTime); $timeRemaining = $timeRemaining ->format(" %h hours, %i minutes "); $page->addToTemplate('bodygua_timer',$timeRemaining); } Quote Link to comment Share on other sites More sharing options...
Eurogangster Posted January 20, 2020 Author Share Posted January 20, 2020 I fixed it thanx Dave and everyone for your support. I tried by my self reading online and finally achieve it!!! But thanx for trying to help me 2 Quote Link to comment Share on other sites More sharing options...
Dave Posted January 20, 2020 Share Posted January 20, 2020 17 minutes ago, Eurogangster said: I fixed it thanx Dave and everyone for your support. I tried by my self reading online and finally achieve it!!! But thanx for trying to help me Great! What was the problem you found? Quote Link to comment Share on other sites More sharing options...
Eurogangster Posted January 20, 2020 Author Share Posted January 20, 2020 This is what i wrote and i tested it and works 100% perfect. $timeb = time(); $bodyguardTime = $this->getTimer("bodyguard"); $timeRemaining = $bodyguardTime - $timeb ; if ($bodyguardTime > time() ) { $timeRemaining = gmdate("H:i:s", $timeRemaining); $page->addToTemplate('bodygua_timer',$timeRemaining); } else { $messagebodyg = 'No Protection' ; $page->addToTemplate('bodygua_timer',$messagebodyg); } 5 hours ago, Dave said: Great! What was the problem you found? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.