FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Possible to reduce minutes from PHP?





Diablosblizz
I am making a PHP script that checks who's online and who's not. Anyways, I am using this code:

Code:
<?php
$date = date("Gi");
$tdate = $date - 60;
echo $tdate;
?>


Let's say the time is currently 17:51 (24 hour time) and I wanted to remove 60 minutes (making it 16:51) with the code above it just makes it 1691. So, I was wondering if there was a script or function that keeps it in the time format? So I wanna take 17:51, minus 60 minutes making it 16:51. I'm pretty sure it's doable, just unsure on how to do it.

Thanks!
coreymanshack
Hey buddy, here is how I did it.

Code:
$currenttime = time();
$testtime = $currenttime - $lastactivity;
if( $testtime < 300  ) { //they are online
//if they do not visit a page within 300 seconds to update $lastactivity they are considered offline.
//do stuff like say they are online.
}
else{ //they are offline
//do offline stuff
}
Hogwarts
Corey's way is how I'd be doing it if I used just PHP. Saying that, I think that's kind of redundant code (it won't update them as being offline unless they're online). For that reason, I would use a MySQL select from my database to select all users by last activity.
Diablosblizz
Working wonderfully, although I can't seem to get the commas to work correctly. Here's my code:

Code:
$x = 0;
$lastact = mysql_query("SELECT * FROM `cms_users`");
$members = totalMembers();
echo "Total Members: $members<br />Users online: ";

   while($lastactarray = mysql_fetch_array($lastact)) {
      $lastac = $lastactarray['online'];
      $totalunact = $currenttime - $lastac;
      if($totalunact < 300) {
         $x++;
         if($x == 1) {
            $comma = "";
         } else if($x >= 2) {
            $comma = ", ";
         }
         echo "$lastactarray[username]$comma";      
      }
      }
      if($x == 0) {
         echo "Nobody";
      }


So it adds 1 to X each time the loop goes through. But it doesn't seem to be doing that properly.
Diablosblizz
Sorry for double posting, but does anybody know what's wrong here?
coreymanshack
Why are you adding a comma?
Diablosblizz
Well instead of having all usernames like:

Diablosblizz coreymanshack Hogwarts

I put commas for:

Diablosblizz, coreymanshack, Hogwarts
jmraker
Whenever I want to add commas between things I do something like

Code:

$string = '';
while($rec = mysql_fetch_array($res)){
  if($string != '')
    $string .= ', ';
  $string .= $rec['word'];
}
echo $string;
Hogwarts
Use commas or spaces, not both -- you'd otherwise be adding two bytes per entry, instead of one. Given mass amounts of short-length entries, this would be a waste of space and processing time.

Also, why is nobody mentioning explode/implode?
jmraker
I think the spaces are to show the list on the screen so humans can read it. Humans use punctuation and spaces in sentences sometimes.

implode could be used if the whole list is in an array, but not useful if you want to append/prepend text or html like a custom link to things in the list. My thinking is that If the array took up 2k of memory, it'll return a 2k string, 2k+2k=4k of memory. If the string is generated, it'll save 2k of memory as it doesn't need to create the array in the first place.
Related topics
? martial art video clip ftp ?
Can l get a css document to
Can someone help me??
how to handle post data as an array
Why are you using FRIhost
How Tough is PHP - Part II
php problem
If you have created a form on your site you can help...
Changing internal data with scripting.
Click adress?
PHP Code to HTML
Using PHP in error pages
PHP/Mysql Automated scheduled tasks
windows mobile programming and php, combination possible?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.