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
https://forums.phpfreaks.com/topic/216419-seconds-to-time/
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
https://forums.phpfreaks.com/topic/216419-seconds-to-time/#findComment-1124642
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
https://forums.phpfreaks.com/topic/216419-seconds-to-time/#findComment-1124643
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.