...

View Full Version : php seconds from database



nclemale
09-04-2011, 02:26 PM
hi . i have searched and searched and tried things and none of them have worked so far.

o am a novice to start off with and trying to figure out how i display lets a Total online time :2 days .3 hours .5 mins .4 secs .
from my database which just says 4637384 .<<<completely random number for example sake .

am sure it can be done with just a few lines of code.



$ttime = mysql_fetch_array(mysql_query("SELECT totaltime FROM users WHERE id='".$uid."'"));

and my usage which displays the number in php seconds


echo "Total Online Time: $ttime[0]";


thanks in advance .

ASTP001
09-04-2011, 03:13 PM
try the date() function.

mlseim
09-04-2011, 03:58 PM
Use time() ... UNIX timestamp when you start.

time() is a 10 digit integer with a resolution in seconds.

Then you use date("h:m:s", $timestamp);



.

nclemale
09-04-2011, 04:54 PM
ok i found this.



function gettimemsg($sec)
{

$years=0;
$months=0;
$weeks=0;
$days=0;
$mins=0;
$hours=0;
if ($sec>59)
{
$secs=$sec%60;
$mins=$sec/60;
$mins=(integer)$mins;
}

if ($mins>59)
{
$hours=$mins/60;
$hours=(integer)$hours;
$mins=$mins%60;
}

if ($hours>23)
{
$days=$hours/24;
$days=(integer)$days;
$hours=$hours%24;
}

if ($days>6)
{
$weeks=$days/7;
$weeks=(integer)$weeks;
$days=$days%7;
}

if ($weeks>3)
{
$months=$weeks/4;
$months=(integer)$months;
$weeks=$weeks%4;
}

if ($months>11)
{
$years=$months/12;
$years=(integer)$years;
$months=$months%12;
}

if($years>0)
{
if($years==1){$yearmsg="year";}else{$yearmsg="years";}
if($months==1){$monthsmsg="month";}else{$monthsmsg="months";}
if($days==1){$daysmsg="day";}else{$daysmsg="days";}
if($hours==1){$hoursmsg="hour";}else{$hoursmsg="hours";}
if($mins==1){$minsmsg="minute";}else{$minsmsg="minutes";}
if($secs==1){$secsmsg="second";}else{$secsmsg="seconds";}

if($months!=0){$monthscheck="$months $monthsmsg ";}else{$monthscheck="";}
if(($days!=0)&&($months==0)){$dayscheck="$days $daysmsg ";}else{$dayscheck="";}
if(($hours!=0)&&($months==0)&&($days==0)){$hourscheck="$hours $hoursmsg ";}else{$hourscheck="";}
if(($mins!=0)&&($months==0)&&($days==0)&&($hours==0)){$minscheck="$mins $minsmsg ";}else{$minscheck="";}
if(($secs!=0)&&($months==0)&&($days==0)&&($hours==0)&&($mins==0)){$secscheck="$secs $secsmsg";}else{$secscheck="";}

return "$years $yearmsg $monthscheck$dayscheck$hourscheck$minscheck$secscheck";
}

if(($years<1)&&($months>0))
{
if($months==1){$monthsmsg="month";}else{$monthsmsg="months";}
if($days==1){$daysmsg="day";}else{$daysmsg="days";}
if($hours==1){$hoursmsg="hour";}else{$hoursmsg="hours";}
if($mins==1){$minsmsg="minute";}else{$minsmsg="minutes";}
if($secs==1){$secsmsg="second";}else{$secsmsg="seconds";}

if($days!=0){$dayscheck="$days $daysmsg ";}else{$dayscheck="";}
if(($hours!=0)&&($days==0)){$hourscheck="$hours $hoursmsg ";}else{$hourscheck="";}
if(($mins!=0)&&($days==0)&&($hours==0)){$minscheck="$mins $minsmsg ";}else{$minscheck="";}
if(($secs!=0)&&($days==0)&&($hours==0)&&($mins==0)){$secscheck="$secs $secsmsg";}else{$secscheck="";}

return "$months $monthsmsg $dayscheck$hourscheck$minscheck$secscheck";
}

if(($months<1)&&($weeks>0))
{
if($weeks==1){$weeksmsg="week";}else{$weeksmsg="weeks";}
if($days==1){$daysmsg="day";}else{$daysmsg="days";}
if($hours==1){$hoursmsg="hour";}else{$hoursmsg="hours";}
if($mins==1){$minsmsg="minute";}else{$minsmsg="minutes";}
if($secs==1){$secsmsg="second";}else{$secsmsg="seconds";}

if($days!=0){$dayscheck="$days $daysmsg ";}else{$dayscheck="";}
if(($hours!=0)&&($days==0)){$hourscheck="$hours $hoursmsg ";}else{$hourscheck="";}
if(($mins!=0)&&($days==0)&&($hours==0)){$minscheck="$mins $minsmsg ";}else{$minscheck="";}
if(($secs!=0)&&($days==0)&&($hours==0)&&($mins==0)){$secscheck="$secs $secsmsg";}else{$secscheck="";}

return "$weeks $weeksmsg $dayscheck$hourscheck$minscheck$secscheck";
}

if(($weeks<1)&&($days>0))
{
if($days==1){$daysmsg="day";}else{$daysmsg="days";}
if($hours==1){$hoursmsg="hour";}else{$hoursmsg="hours";}
if($mins==1){$minsmsg="minute";}else{$minsmsg="minutes";}
if($secs==1){$secsmsg="second";}else{$secsmsg="seconds";}

if($hours!=0){$hourscheck="$hours $hoursmsg ";}else{$hourscheck="";}
if(($mins!=0)&&($hours==0)){$minscheck="$mins $minsmsg ";}else{$minscheck="";}
if(($secs!=0)&&($hours==0)&&($mins==0)){$secscheck="$secs $secsmsg";}else{$secscheck="";}

return "$days $daysmsg $hourscheck$minscheck$secscheck";
}

if(($days<1)&&($hours>0))
{
if($hours==1){$hoursmsg="hour";}else{$hoursmsg="hours";}
if($mins==1){$minsmsg="minute";}else{$minsmsg="minutes";}
if($secs==1){$secsmsg="second";}else{$secsmsg="seconds";}

if($mins!=0){$minscheck="$mins $minsmsg ";}else{$minscheck="";}
if(($secs!=0)&&($mins==0)){$secscheck="$secs $secsmsg";}else{$secscheck="";}

return "$hours $hoursmsg $minscheck$secscheck";
}

if(($hours<1)&&($mins>0))
{
if($mins==1){$minsmsg="minute";}else{$minsmsg="minutes";}
if(($secs==1)&&($mins==0)){$secsmsg="second";}else{$secsmsg="seconds";}

if($secs!=0){$secscheck="$secs $secsmsg";}else{$secscheck="";}

return "$mins $minsmsg $secscheck";
}

if(($mins<1)&&($sec>0))
{
if($sec==1){$secsmsg="second";}else{$secsmsg="seconds";}

if($sec!=0){$secscheck="$sec $secsmsg";}else{$secscheck="";}

return "$secscheck";
}else{
return "Online!";
}
}

and using it like this


$totaltimeonline = mysql_fetch_array(mysql_query("SELECT totaltime FROM users WHERE id='".$who."'"));
echo "<b>Total Time Online: </b>".gettimemsg($totaltimeonline[0])."";

but theres still a problem. its going to fast . like 1 second is 2 minutes .so its jumping minutes as fast as it should be jumping seconds.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum