Jump to content

Seconds to time


karimali831

Recommended Posts

Hi!

 

I have got a problem with this function:

 


<?php
function Sec2Time($time){
  if(is_numeric($time)){
    $value = array(
      "years" => 0, "days" => 0, "hours" => 0,
      "minutes" => 0, "seconds" => 0,
    );
    if($time >= 31556926){
      $value["years"] = floor($time/31556926);
      $time = ($time%31556926);
    }
    if($time >= 86400){
      $value["days"] = floor($time/86400);
      $time = ($time%86400);
    }
    if($time >= 3600){
      $value["hours"] = floor($time/3600);
      $time = ($time%3600);
    }
    if($time >= 60){
      $value["minutes"] = floor($time/60);
      $time = ($time%60);
    }
    $value["seconds"] = floor($time);
    return (array) $value;
  }else{
    return (bool) FALSE;
  }
}
?>

 

and my output for example:

 

echo Sec2Time(604800);

returns = "Array" ??

 

What I am wanting is to return a time from how many seconds.

604800 is 7 days so I want it to say 7 days.

2419200 is 1 month so I want it to say 1 month or 4 weeks.

 

is (above) the right function for what I want ?

 

thanks.

Link to comment
Share on other sites

I have changed it slightly which I hope it works, yes?

 

function Sec2Time($time){
  if(is_numeric($time)){
    $value = array(
      "years" => 0, "days" => 0, "hours" => 0,
      "minutes" => 0, "seconds" => 0,
    );
    if($time >= 31556926){
      return $value["years"] = floor($time/31556926). " Years";
      $time = ($time%31556926);
    }
    elseif($time >= 86400){ 
      return $value["days"] = floor($time/86400). " Days";
      $time = ($time%86400);
    }
    elseif($time >= 3600){
      return $value["hours"] = floor($time/3600). " Hours";
      $time = ($time%3600);
    }
    elseif($time >= 60){
      return $value["minutes"] = floor($time/60). " Minutes";
      $time = ($time%60);
    }
    else
      return $time ." Seconds";
  }else{
    return (bool) FALSE;
  }
}

Link to comment
Share on other sites

Well, you don't have any logic in there for months or weeks, and I haven't checked the logic that is there, but given the current function you would do something like:

 

$result = Sec2Time(604800);
echo $result['Years'] . ' ' . $result['Days'] . ' ' . $result['Hours'] . ' ' .  $result['Minutes'] . ' ' . $result['Seconds'];

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.