Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2009
    Location
    uk
    Posts
    81
    Thanks
    4
    Thanked 3 Times in 3 Posts

    php seconds from database

    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.

    PHP Code:
    $ttime mysql_fetch_array(mysql_query("SELECT totaltime FROM users WHERE id='".$uid."'")); 
    and my usage which displays the number in php seconds
    PHP Code:
    echo "Total Online Time: $ttime[0]"
    thanks in advance .

  • #2
    New Coder
    Join Date
    Aug 2011
    Posts
    51
    Thanks
    6
    Thanked 12 Times in 12 Posts
    try the date() function.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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);



    .

  • #4
    New Coder
    Join Date
    Sep 2009
    Location
    uk
    Posts
    81
    Thanks
    4
    Thanked 3 Times in 3 Posts
    ok i found this.

    PHP Code:
    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
    PHP Code:
    $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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •