Jump to content

Datetime format + Unit conversion


jarsky

Recommended Posts

Hey Guys,

Im fairly newbie with php + mysql still and have written up a script yesterday but am having a few issues as I'm having problems getting my head around variables.

I am outputting stats from a program into a mysql database and am outputting these stats with php however there are 2 parts I would like to reformat. The sharesize stored in the database is in bytes which I would like to reformat to kB/mb/gb/tb as appropriate. The other thing is the last logged in date I would like to take from the unix display YYYY-MM-DD Hh:Mm:Ss, to something such as 25th April, 2006 11.58pm for example. Ive attached below my code so far, with $sharesize & $last_used being the variables concerned, if anyone would be able to advise on how to do this :) Also the page can be viewed at [a href=\"http://ravestyle.no-ip.com/ynhub/\" target=\"_blank\"]http://ravestyle.no-ip.com/ynhub/[/a] would really appreciate some help on this. Thanks guys

[code]
<?

/*
YnHub User Stats php Script created by raVest for use with the Ravestyles NZ Hub
This script is designed to output the stats collected by the DC Hubsoft and display this
in a readable format. Any queries or tips please contact on trance@ravestyles.com
*/

include("private.inc.php");

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM nickstats";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<font class=ihead><b><center>Ravestyles NZ Hub</center></b></font><br>";
/* echo "<font class=ihead><b><center><? echo $hubname; ?></center></b></font><br>"; */
/* echo "<font class=ihead><b><center>Come and visit us at <a href="mailto:<? echo $hubadd; ?>"><? echo $hubadd; ?></a></center></b></font><br><br>"; */

$i=0;
while ($i < $num) {

$nick=mysql_result($result,$i,"nick");
$dns=mysql_result($result,$i,"dns");
$passive_mode=mysql_result($result,$i,"passive_mode");
$description=mysql_result($result,$i,"description");
$email=mysql_result($result,$i,"email");
$sharesize=mysql_result($result,$i,"sharesize");
$tag=mysql_result($result,$i,"tag");
$client=mysql_result($result,$i,"client");
$hubs=mysql_result($result,$i,"hubs");
$slots=mysql_result($result,$i,"slots");
$muted=mysql_result($result,$i,"muted");
$kennylized=mysql_result($result,$i,"kennylized");
$lunarized=mysql_result($result,$i,"lunarized");
$blocked=mysql_result($result,$i,"blocked");
$kicked_times=mysql_result($result,$i,"kicked_times");
$online=mysql_result($result,$i,"online");
$last_used=mysql_result($result,$i,"last_used");

?>

<tr>
<td><font class=itext><? echo $id." ".$nick; ?></font></td>
<td><font class=itext><? if ( $online == y ) {
    echo "<font color=#00CC33>Online</font>";
} else {
    echo "<font color=#FF0000>Offline</font>";
}
?></font></td>
<td><font class=itext><? echo $sharesize; ?></font></td>
<td><font class=itext><? echo $description; ?></font></td>
<td align=center><font class=itext><a href="mailto:<? echo $email; ?>"><img src="images/email1.gif" border="0" alt="Email" title="Email"></a></font></td>
<td align=center><font class=itext><? echo $slots; ?></font></td>
<td align=center><font class=itext><? if ( $blocked == 9 ) {
    echo "<font color=#9966CC>Yes</font>";
} else {
    echo "No";
}
?></font></td>
<td align=center><font class=itext><? if ( $muted == 9 ) {
    echo "<font color=#9966CC>Yes</font>";
} else {
    echo "No";
}
?></font></td>
<td align=center><font class=itext><? if ( $kennylized == 9 ) {
    echo "<font color=#9966CC>Yes</font>";
} else {
    echo "No";
}
?></font></td>
<td align=center><font class=itext><? if ( $lunarized == 9 ) {
    echo "<font color=#9966CC>Yes</font>";
} else {
    echo "No";
}
?></font></td>
<td><font class=itext><? echo $client; ?></font></td>
<td align=center><font class=itext><? echo $hubs; ?></font></td>
<td><font class=itext><? if ( $passive_mode == y ) {
    echo "<font color=#FF0000>Passive</font>";
} else {
    echo "<font color=#00CC33>Active</font>";
}
?></font></td>
<td align=center><font class=itext><? echo $kicked_times; ?></font></td>
<td><font class=itext><? echo $last_used; ?></font></td>
</tr>
<?

$i++;
}

echo "</table>";
?>
[/code]
Link to comment
Share on other sites

hmm .. i'm not sure on the date formmating but as for the bytes calculation, i would do somthing like this

[code]
$bytes = *query your database here*

$kb = $bytes / 1024; //1024 being the amount of bites in a kilobyte
$mb = $kb / 1024;
$gb = $mb / 1024;
$tb = $gb / 1024;
[/code]
and then reference them through out your code as needed.

**additional thought, if you wanted to use an if / else statement you could tell the code to look at the $bytes var and display the correct size id based on the amount of bytes.

something like
[code]
if ($bytes > 1000000000){
echo 'Total Space = ' . $gb . 'gb.';
}
[/code]


PS: I'm a bit a noob myself, so these may not the be perfect way, but they are the ways I have found to work.

hope this helps.

Chris
Link to comment
Share on other sites

[!--quoteo(post=368663:date=Apr 26 2006, 11:16 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 26 2006, 11:16 AM) [snapback]368663[/snapback][/div][div class=\'quotemain\'][!--quotec--]
for the date

[code]echo date ('jS F Y g:ia', strtotime($db_date));[/code]
[/quote]


Cheers for the help, the date code is exactly what I wanted :D
The unit conversion works but is not quite what I wanted as I would like it to convert to MB/GB/TB and work out which to display it as, as the figures range between few hundred mb's up to few tb's. I will keep working on that part though :)
Link to comment
Share on other sites

[!--quoteo(post=368774:date=Apr 26 2006, 05:46 AM:name=jarsky)--][div class=\'quotetop\']QUOTE(jarsky @ Apr 26 2006, 05:46 AM) [snapback]368774[/snapback][/div][div class=\'quotemain\'][!--quotec--]
The unit conversion works but is not quite what I wanted as I would like it to convert to MB/GB/TB and work out which to display it as, as the figures range between few hundred mb's up to few tb's.
[/quote]

like i said, you would use if / else statments to determine what gets displayed.

something like
[code]
if ($bytes > 1000000000){
echo 'Total Space = ' . $gb . 'gb.';
}
[/code]
you will just need to incorportate them all into one function.

Link to comment
Share on other sites

Try

[code]function getSize ($sz) {
         $units = array ('bytes', 'Kb', 'Mb', 'Gb', 'Tb');
         $i = 0;
         while ($sz > 1000) {
             $i++;
             $sz /= 1024;
         }
         return sprintf('%8.0f %s', $sz, $units[$i]);
}

$sizes = array (1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789,
         1234567899, 12345678999, 123456789999, 1234567899999, 12345678999999);

echo '<pre>';
foreach ($sizes as $sz) {
         printf('%20s %s<br/>', $sz, getSize($sz));
}
echo '</pre>';[/code]

-->
[code]
                   1        1 bytes
                  12       12 bytes
                 123      123 bytes
                1234        1 Kb
               12345       12 Kb
              123456      121 Kb
             1234567        1 Mb
            12345678       12 Mb
           123456789      118 Mb
          1234567899        1 Gb
         12345678999       11 Gb
        123456789999      115 Gb
      1.2345679E+012        1 Tb
      1.2345679E+013       11 Tb
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.