Add today’s date by estimated travel hours

Asked

Viewed 71 times

2

I receive a value in the format of Hours and Minutes example:

"09:00" hours and minutes

I need to take today’s date make the sum hours and minutes and present example:

arrives today at 17:30 hours and minutes

In case the value I receive is too high that it passes of days for example:

"36:00" hours and minutes

By today’s date and time present

arrives tomorrow at 7:20

I put down below more or less than I need:

<?php 
$times = array(
date('H:I'),
'01:10',
);

$seconds = 0;

foreach ( $times as $time )
{
list( $g, $i ) = explode( ':', $time );
$seconds += $g * 3600;
$seconds += $i * 60;
}

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

echo "{$hours}:{$minutes} h";
?>
  • What did you try to do? You checked the PHP date/time functions to see if there’s anything that can help you?

  • Hi Anderson tried more could not

  • Could you add in the question your attempt and the obtained result? If you gave error, put the error message.

  • OK doing that

  • Put la plus this code works only if the day is today, in case Anderson I think I need to first convert minutes hours into date and then convert into weeks and present ne ?

  • So tomorrow, the day after tomorrow and then what you put on?

  • Need presents type arrives Thursday day 08/11/2018 as 17:22 example

Show 2 more comments

3 answers

0

People with a lot of work kind of that in the gambiarra "rsrsr" more came out if someone is a better idea it is only post here personal thanks:

<?php 
//Primeito vou separar hora do minutos
$estimado ='48:10'; 
$separador=explode(":",$estimado); 

$horas =  $separador[0];
$minutos =  $separador[1];


//Agora vou somar a data de hoje + horas e minutos
$data_hoje = date("d-m-Y H:i");
$soma_das_datas = date('d-m-Y H:i',strtotime('+'.$horas.' hour +'.$minutos.' minutes',strtotime($data_hoje)));
 
 
// Agora vou descobrir o dia da semana dessa data 
$diasemana = array('Domingo', 'Segunda', 'Ter&ccedil;a', 'Quarta', 'Quinta', 'Sexta', 'S&aacute;bado');
$data = $soma_das_datas;

// Varivel que recebe o dia da semana (0 = Domingo, 1 = Segunda ...)
$diasemana_numero = date('w', strtotime($data));

// Exibe o dia da semana com o Array

if ($diasemana[$diasemana_numero] == $diasemana[date('w', strtotime(date('d-m-Y')))]) {

$previsao = 'Hoje '.'as '.date('H:i', strtotime($soma_das_datas));



} else {

$previsao = $diasemana[$diasemana_numero].' as '.date('H:i', strtotime($soma_das_datas));


}

echo $previsao;

?>

0


setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');

$addHoras="48";
//$addHoras="4:10";
//$addHoras="4:1";
//$addHoras="0:01";
//$addHoras=" :60";
//transforma em minutos

$final=explode(":",$addHoras);
$addMinutos = ($addHoras*60) + (end($final));


$dataAtual=date('Y-m-d H:i:s');
//hora a ser exibida
$horaChegada = date('H:i', strtotime($dataAtual. " + {$addMinutos} minutes"));

$numDiaHoje= date('d');

$numDiaChegada = date('d', strtotime($dataAtual. " + {$addMinutos} minutes"));


if ($numDiaChegada==$numDiaHoje){

    echo "chego as " .$horaChegada."hs";

}elseif (($numDiaChegada-$numDiaHoje==1)){

    echo "Chego amanhã as " .$horaChegada."hs";

}elseif (($numDiaChegada-$numDiaHoje==2)){

    echo "Chego depois de amanhã as " .$horaChegada."hs";

}else{

    $dia_chegada = date('Y-m-d H:i:s', strtotime($dataAtual. " + {$addMinutos} minutes"));
    echo "Chego " .ucfirst(strftime("%A", strtotime($dia_chegada))). " " . $horaChegada . "hs";
}

Can test in Phptester that gives setlocale support - just paste the code there and click to test your php code

  • Perfect my noble thanks !

0

the correct way to add hours to a date is this

    $startTime = date("Y-m-d H:i:s");

    //exibir a hora de início
    echo 'Starting Time: '.$startTime;

    //adiciona 1 hora ao tempo
    $cenvertedTime = date('Y-m-d H:i:s',strtotime('+1 hour',strtotime($startTime)));

    //exibir o tempo convertido
    echo 'Converted Time (added 1 hour): '.$cenvertedTime;

    //adiciona 1 hora e 30 minutos ao horário
    $cenvertedTime = date('Y-m-d H:i:s',strtotime('+1 hour +30 minutes',strtotime($startTime)));

    //exibir o tempo convertido
    echo 'Converted Time (added 1 hour & 30 minutes): '.$cenvertedTime;

    //adiciona 1 hora, 30 minutos e 45 segundos ao horário
    $cenvertedTime = date('Y-m-d H:i:s',strtotime('+1 hour +30 minutes +45 seconds',strtotime($startTime)));

    //exibir o tempo convertido
    echo 'Converted Time (added 1 hour, 30 minutes  & 45 seconds): '.$cenvertedTime;

    //adiciona 1 dia, 1 hora, 30 minutos e 45 segundos ao horário
    $cenvertedTime = date('Y-m-d H:i:s',strtotime('+1 day +1 hour +30 minutes +45 seconds',strtotime($startTime)));

    //exibir o tempo convertido
    echo 'Converted Time (added 1 day, 1 hour, 30 minutes  & 45 seconds): '.$cenvertedTime;

I hope with this tutorial you understand how to manipulate your dates and predict the minute and second arrival time day.

follows a time difference calculation function that had ready here:

function timediff($inicio,$fim){
   $entrada = $inicio;
   $saida = $fim;
   $hora1 = explode(":",$entrada);
   $hora2 = explode(":",$saida);
   $acumulador1 = ($hora1[0] * 3600) + ($hora1[1] * 60) + $hora1[2];
   $acumulador2 = ($hora2[0] * 3600) + ($hora2[1] * 60) + $hora2[2];
   $resultado = $acumulador2 - $acumulador1;
   $hora_ponto = floor($resultado / 3600);
   $resultado = $resultado - ($hora_ponto * 3600);
   $min_ponto = floor($resultado / 60);
   $resultado = $resultado - ($min_ponto * 60);
   $secs_ponto = $resultado;
   //Grava na variável resultado final
   $tempo = $hora_ponto.":".$min_ponto.":".$secs_ponto;
   return $tempo;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.