Save data printf in a string - PHP

1

Hello everyone I have problems saving the data of the printf function in a variable.

this is my code:

<?php
header("Content-type: text/html; charset=utf-8");
$ultima_fecha = "12/08/2018 07:16:26";
$indice1 = explode("/", $ultima_fecha);
$anio = $indice1[2];
$mes = $indice1[1];
$dia = $indice1[0];
$rest = substr($anio, 0, 4);
$hora = substr($anio, -8);
$fechaInicio = $rest . "-" . $mes . "-" . $dia . " " . $hora;
$fecha1 = new DateTime($fechaInicio);
$fecha2 = new DateTime("2019-08-20 15:52:30");
$fecha = $fecha1->diff($fecha2);
$formato = '%d años, %d meses, %d días, %d horas, %d minutos';
echo sprintf($formato, $fecha->y, $fecha->m, $fecha->d, $fecha->h, $fecha->i);
?>

The result of the PHP script is this:

What I need is for all that result to be stored in a variable as a string.

is there any way to do it?

    
asked by RCA 20.11.2018 в 23:29
source

4 answers

1

You could generate it as follows

  • You declare a variable that is called for example $fechaFinal which you will match with:
  • $fechaFinal = sprintf($formato, $fecha->y, $fecha->m, $fecha->d, $fecha->h, $fecha->i);
    
  • Now all that value is contained in said variable; which allows you to send it to print in this way
  • echo $fechaFinal;
    

    So that you keep using sprintf and when you print the result, it should look like this:

    1 años, 0 meses, 8 días, 8 horas, 36 minutos
    

    Even if you use the function gettype() to know what type of data is stored, in this way

    echo gettype($fechaFinal);
    

    The result in console should be:

    string
    
      

    Which tells you clearly that a value is stored in   text string format

        
    answered by 20.11.2018 в 23:40
    1

    You only need to assign it to a variable since the sprintf method returns as a result a string .

    That is:

    $resultado = sprintf($formato, $fecha->y, $fecha->m, $fecha->d, $fecha->h, $fecha->i);
    

    The official documentation indicates the following:

      

    string sprintf ( string $format [, mixed $args [, mixed $... ]] )

         

    Returns a string produced according to the formatting string given by    format .

    You can find information in the official documentation in example 9 and 10.

    References

    answered by 20.11.2018 в 23:41
    1

    I feel sorry when I see code that does not make the most of the objects.

    In your specific case, $fecha is a complete object, with own methods to print what you want and how you want. Why invoke another function to do what the object can already do on its own and in a single call to the object?

      

    Trying to use sprintf is giving up the format method that the object has as its own part.

    For example here you manipulate the object five times (you make five calls to five properties of the object) and use a supplementary function:

    sprintf($formato, $fecha->y, $fecha->m, $fecha->d, $fecha->h, $fecha->i);
    

    With this code, you get the same result, manipulating the object only once and without having to resort to another supplementary function:

    $str=$fecha->format("%y años, %m meses, %d días, %h horas, %i minutos");
    

    You already have everything you want, it's in the variable $str .

        
    answered by 21.11.2018 в 02:56
    0

    There is indeed a way, one of them is using ob_get_contents to capture what you have from your echo .

      

    ob_start   This will activate the output buffer preventing the script from being printed.

         

    ob_get_contents   It will return the contents of the output buffer.

         

    ob_end_clean ()   It will remove the contents of the output buffer and inactivate it

    Mainly it has utility to capture the values of functions that return data with printing and not with variables

    <?php
    header("Content-type: text/html; charset=utf-8");
    $ultima_fecha = "12/08/2018 07:16:26";
    $indice1 = explode("/", $ultima_fecha);
    $anio = $indice1[2];
    $mes = $indice1[1];
    $dia = $indice1[0];
    $rest = substr($anio, 0, 4);
    $hora = substr($anio, -8);
    $fechaInicio = $rest . "-" . $mes . "-" . $dia . " " . $hora;
    $fecha1 = new DateTime($fechaInicio);
    $fecha2 = new DateTime("2019-08-20 15:52:30");
    $fecha = $fecha1->diff($fecha2);
    $formato = '%d años, %d meses, %d días, %d horas, %d minutos';
    
    ob_start(); //Activa el buffer de salida basicamente
    
    // todo el codigo que quieres que sea capturado
    echo sprintf($formato, $fecha->y, $fecha->m, $fecha->d, $fecha->h, $fecha->i);
    
    // obtiene el buffer de salida y lo asigna en una variable
    $data = ob_get_contents();
    
    //finaliza el buffer de salida
    ob_end_clean();
    
    //un simple var dump para saber que hay en data
    var_dump($data);
    
    ?>
    
        
    answered by 21.11.2018 в 03:15