tags:

views:

52

answers:

6

hi there for some reason i convert a time format like : 03:30 to seconds 3*3600 + 30*60 , now .. i wanna convert it back to it's first ( same ) format upthere ... how could that be ?

My attempt :

3*3600 + 30*60 = 12600 12600 / 60 = 210 / 60 = 3.5 , floor(3.5) = 3 = hour

now .. what about the minutes ??

considering the value can be like 19:00 or 02:51 ... i think you got the picture

and by the way, how to convert 2:0 for example to 02:00 using RegEx ?

Thanks in advance

A: 

$hours = floor($seconds / 3600); $mins = floor(($seconds - ($hours*3600)) / 60;)

ITroubs
That is the one :), Thanks .. though i don't understand it
Dewan159
A: 

something like this?

 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; }

grabbed from: http://www.ckorp.net/sec2time.php

FatherStorm
The days portion ignores daylight saving, and the years portion ignores that and leap years.
Spudley
A: 

Use modulo:

$hours = $time_in_seconds / 3600;
$minutes = ($time_in_seconds / 60) % 60;
bitmask
i don't know .. it gives me this : 2 = h, 5.30 = m , when seconds are 9000 (2 hours+ 30 mins)
Dewan159
+1  A: 

If the you know the times will be less than an hour, you could just use the date() or $date->format() functions.

$minsandsecs = date('i:s',$numberofsecs);

This works because the system epoch time begins at midnight (on 1 Jan 1970, but that's not important for you).

If it's an hour or more but less than a day, you could output it in hours:mins:secs format with `

$hoursminsandsecs = date('H:i:s',$numberofsecs);

For more than a day, you'll need to use modulus to calculate the number of days, as this is where the start date of the epoch would become relevant.

Hope that helps.

Spudley
oh good ... how could i miss that ... thanks
Dewan159
How would you 'use modulus to calculate the number of days'?
tilman
@tilman: In PHP, use the % sign for modulus. Other answers showed how to do modulus which is why I didn't.
Spudley
If you only show days and not years, cant you just subtract one day (in seconds) from your $numberofsecs? `if ($numberofsecs > 86400) $numberofsec -= 86400;` effectively starting on Jan 0th 1970.
tilman
A: 

Let $time be the time as number of seconds.

$seconds = $time % 60;
$time = ($time - $seconds) / 60;
$minutes = $time % 60;
$hours = ($time - $minutes) / 60;

Now the hours, minutes and seconds are in $hours, $minutes and $seconds respectively.

Hammerite
A: 

Another solution that will give you the days, hours, minutes, and seconds for a passed-in seconds value:

function seconds_to_time($secs)
{
    $dt = new DateTime('@' . $secs, new DateTimeZone('UTC'));
    return array('days'    => $dt->format('z'),
                 'hours'   => $dt->format('G'),
                 'minutes' => $dt->format('i'),
                 'seconds' => $dt->format('s'));
}

print_r(seconds_to_time($seconds_value);

Extra logic will be needed for 'days' if the time is expected to be more than one year. Use str_pad() or ltrim() to add/remove leading zeros.

GZipp