PHP Counter Hour, minute and seconds

1

I have the following code for a PHP counter:

  

date_default_timezone_set ('America / Mexico_City');

function evento()
{
    time();
    $today = strtotime('today 12:00');
    $tomorrow = strtotime('tomorrow 12:00');
    $time_now = time();
    $timeLeft = ($time_now > $today ? $tomorrow : $today) - $time_now;
    return gmdate("H:i:s", $timeLeft);
}   

echo evento();

it gives me as a result:

  

00:07:18

My question and doubt (the question may be too new). How could I do to throw the following?

echo evento();
  

00 Hours, 07 Minutes, 18 Seconds.

I know that Javascript is possible, but I would like to use PHP for this.

Thanks in advance.

    
asked by Joseph Gonzaga 14.07.2017 в 19:01
source

3 answers

1

You could modify your evento function and use strftime instead of gmdate .

So for example:

<?php

    date_default_timezone_set('America/Mexico_City');
    function evento()
    {
        time();
        $today = strtotime('today 12:00');
        $tomorrow = strtotime('tomorrow 12:00');
        $time_now = time();
        $timeLeft = ($time_now > $today ? $tomorrow : $today) - $time_now;
        return strftime("%H Horas, %M minutos, %S segundos", $timeLeft);
    }   

    echo evento();

?>

Demo

    
answered by 14.07.2017 / 19:39
source
1

Something not so sophisticated occurs to me, use printf , you can add the following code inside or outside the function that already has implemented (within the function you would have to access gmdate("H:i:s", $timeLeft) instead of the call to the function) .

$vars = explode(":", evento());
printf(' %s Horas ,  %s Minutos , %s Segundos', $vars[0], $vars[1],$vars[2]);

You can also use the vsprintf function to format the string by passing a array as a parameter.

echo vsprintf(' %s Horas ,  %s Minutos , %s Segundos',explode(":" , evento()));
    
answered by 14.07.2017 в 19:08
0

From a result in strtotime format:

return date("H", $timeleft)." Horas, ".date("i", $timeleft)." Minutos,".date("s", $timeleft)." Segundos";
    
answered by 14.07.2017 в 19:42