Web Hosting Talk







View Full Version : PHP Calculate finish time from start time and duration


doop
10-15-2007, 09:35 PM
Hi Guys,

I have been having trouble finding something on how to calculate a finish time from a start time and duration. I have found various scripts from start and finish time to equal duration but really need it the other way around.

Any help/point in the right direction would be great thanks :)

luki
10-15-2007, 10:21 PM
$start = strtotime('2007-10-15 14:00:00');
$duration = 15*60; // 15 minutes
echo strftime('%x %X', $start + $duration);

doop
10-15-2007, 11:02 PM
Thanks for that luki, works a charm :)

Just one other question, would this work if it was overnight?

For example,

Start date 10/10/07 at 23:00 and duration of 3 hours

Would the finish date show 10/11/07 at 02:00 ?

luki
10-15-2007, 11:37 PM
Would this work if it was overnight?
Yes.

It even works when we change to/from DST, however, it counts the exact time passed, so in the spring there is only one hour between 1am and 3am, etc.

CarlosMtnez
10-16-2007, 10:33 PM
I created a class to do that, but it was programmed in spanish :stickout:
Well, now it's very late and I need to sleep, I cannot translate, but is very easy.


class Cronometro {
var $TiempoInicio;
var $TiempoFinal;
var $TiempoTranscurrido;

function Cronometro() {
$this->TiempoInicio=NULL;
$this->TiempoFinal=NULL;
$this->TiempoTranscurrido=NULL;

$this->Iniciar(); //Nada mas instanciar iniciamos el cronometro
}

//Formatea y lee el micro tiempo actual
function LeerTiempo() {
$tiempo=microtime();
$tiempo=explode(" ",$tiempo);
$tiempo=$tiempo[1]+$tiempo[0];

return $tiempo;
}

//Inicia el cronometro
function Iniciar() {
$this->TiempoInicio=$this->LeerTiempo(); //Leemos tiempo actual
}

function Parar() {
$this->TiempoFinal=$this->LeerTiempo();
$this->TiempoTranscurrido=$this->TiempoFinal-$this->TiempoInicio;

return $this->Ver();
}

function Ver() {
return number_format($this->TiempoTranscurrido, 3, ',', '.');
}
}



An example of use:

require_once("cronometro.class.php");
$time= new Cronometro();

//// YOUR PROGRAM ////

echo 'Page generated in '.$time->Parar().' seconds';


it's very simple class, if you want I'll translate for you.

Ciao.