Why value looks different with printf and echo

Asked

Viewed 65 times

0

I’m making a calculation between dates and times but when displaying the value with printf the result is one and with echo is different. With this calculation the value is displayed correctly:

 $Entrada = strtotime( '2010-05-26 08:00' );
    $Saida   = strtotime( '2010-05-26 11:15' );
    $Diferenca = $Saida - $Entrada;

    $hora = $Diferenca/3600;
    $min  = $Diferenca/60%60;

    printf( '%d:%d', $hora, $min );

The result is: 3:15

And thus the result is added a value at the end:

    $Entrada = strtotime( '2010-05-26 08:00' );
    $Saida   = strtotime( '2010-05-26 11:15' );
    $Diferenca = $Saida - $Entrada;

    $hora = $Diferenca/3600;
    $min  = $Diferenca/60%60;

    $Resultado = printf( '%d:%d', $hora, $min );
    echo $Resultado;

The result is: 3:154

Look at the PHP Sandbox

  • 1

    To save the value in a variable, you need to use sprintf. The printf is only for the formatted view.

  • Hello @lvr7, for you see, did not know this, thanks for the tip.

2 answers

2


The printf is a function that prints a formatted string in the console and returns the string length in numbers.

For example:

// Irá mostrar a string "Luiz Felipe" antes dos três hífens.
$printedLength = printf("%s %s", "Luiz", "Felipe");

echo "\n" . "---" . "\n";

// Irá imprimir 11:
echo $printedLength;

See it working on Ideone.

If you want to assign the value of a formatted string, you should use the function sprintf, which does not print to the stdout automatically:

// Irá atribuir a string "Luiz Felipe" à variável `$formated`:
$formated = sprintf("%s %s", "Luiz", "Felipe");

// Irá imprimir "Luiz Felipe" ao console:
echo $formated;

See it working on Ideone.


That way, when you do:

$resultado = printf('%d:%d', $hora, $min);
echo $resultado;

You are printing the time to stdout and then printing the length of the formatted string (which has already been printed on the console).

In the above case, you probably want to replace the function printf for sprintf:

$resultado = sprintf('%d:%d', $hora, $min);
echo $resultado;

Reference:

  • 1

    Thanks @Luizfelipe the tip was spectacular and helped too much to understand the subject, thank you.

1

As I noticed, when you use %d you are asking the printf to show the absolute integer, then when you put everything together in the "%d:%d", there is 0.25 minutes left of the hour, this leftover is placed in the minutes, so that, 15 + 0.25 ((60/4) minutes that represents the leftover) generate the 03:154 probably by rounding.

Use gmdate to format differently if desired.

<?php
// Inteiros com timestamp inicial.
$Entrada = strtotime('2010-05-01 08:00');
// Inteiros com timestamp finaç.
$Saida = strtotime('2010-05-01 11:15');
// diferença.
$Diferenca = $Saida - $Entrada;
// Conversão de tempos.
$hora = $Diferenca / 3600;
$min = $hora / 60 % 60;
// formatar como GMT:UTC Date/Time.
$Resultado = gmdate('H:i', $Diferenca);
// Mostrar resultado
echo $Resultado; // A saída será 03:15
?>
  • I do not think this answers the question. I want to know why the difference, not how to format the date itself...

  • Thanks for the tip Felipe.

  • Thanks for the excellent explanation @Luisalbertobatista, stayed on show.

Browser other questions tagged

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