add hours type time

0

I have a variable that is 18:00:00 bone at 6:00:00 pm and I want to add another 19:30:00 hours so that the resulting variable is 1:30 pm but the next day. To then generate a condition with that new resulting variable: for example that such hour is greater than the variable that contains 1:30 pm.

$hora5=("18:00:00");
$horali=date($hora5);
$horalimite2=date('H:i:s', strtotime($horali.' +19 hours'.' +30 minutes'));
echo $horalimite2;

 if($hentrega>$horalimite2)
    
asked by jasiel 07.09.2018 в 22:37
source

2 answers

0

You can try this

<?
$F=date("Y-m-d h:i:s");
$SF=strtotime($F);

echo date("Y-m-d h:i:s",mktime(
  date("h",$SF)+19,
  date("i",$SF)+30,
  date("s",$SF),
  date("m",$SF),
  date("d",$SF),
  date("Y",$SF)
));
?>

mktime I use to decompose the date

I add to the block of hours 19

I add to the block of minutes 30

You can use only the time see this example

$F=date("18:00:00");
$SF=strtotime($F);
echo date("Y-m-d h:i:s",$SF);

Take the current date and add the time you specify in 24 hour format

If you require the format in 12 hours you can use date("Y-m-d H:i:s a",$SF);

Greetings:)

    
answered by 07.09.2018 в 22:47
0

The DateTime::add() method allows us to add or add an amount of time (years, months, days, hours, minutes, seconds) to a date time. It also allows us to compare dates directly.

Example:

<?php
$fecha = new DateTime('2018-09-07 18:00:00');
$fecha2 = clone $fecha;

$intervalo = new DateInterval('PT19H30M'); // intervalo de tiempo 19 horas y 30 min

echo $fecha->format('Y-m-d h:i:s a');

$fecha->add($intervalo); // añadimos, sumamos el intervalo de tiempo
echo $fecha->format('Y-m-d h:i:s a');

// comparación 
if ($fecha >= $fecha2){
    echo 'fecha es mayor o igual a fecha2';
} else { 
    echo  'fecha es menor que fecha2';
} 

Result

2018-09-07 06:00:00 pm
2018-09-08 01:30:00 pm
fecha es mayor o igual a fecha2

Documentation: link

    
answered by 08.09.2018 в 13:05