how can I convert saved date to a table in minutes hours and according to php

-1

The fact is that I'm not very good at working with hours I'm creating a website where users can post photos so what I want is that the time I have stored in the table of my database in format date time, at the time of making a publication instead of appearing that date so ugly year month day hour etc .. that I look more or less like that. 3 seconds ago, so many minutes ago, so many hours ago. am I clear? thanks hopefully and someone can help me

    
asked by andy gibbs 04.05.2018 в 05:58
source

1 answer

0

It's really quite simple when you analyze it, I explain, what you need is to get the date as is, as you have stored it in your registry and then you must convert it to a unix format

$fecha_de_la_tabla=strtotime("2018-05-04 12:00:00");

then you must obtain the current date and convert it to unix format

$unix_actual=time();

Once you have this you must subtract the current unix time and the one you have stored in your table

$tiempo_transcurrido=$unix_actual-$fecha_de_la_tabla;

to finish you must use this function that I leave to convert the elapsed time in hours, minutes or seconds, as the case may be

function conversorSegundosHoras($tiempo_en_segundos) {
    $horas = floor($tiempo_en_segundos / 3600);
    $minutos = floor(($tiempo_en_segundos - ($horas * 3600)) / 60);
    $segundos = $tiempo_en_segundos - ($horas * 3600) - ($minutos * 60);

    $hora_texto = "";
    if ($horas > 0 ) {
        $hora_texto .= $horas . "h ";
    }

    if ($minutos > 0 ) {
        $hora_texto .= $minutos . "m ";
    }

    if ($segundos > 0 ) {
        $hora_texto .= $segundos . "s";
    }

    return $hora_texto;
}

echo conversorSegundosHoras($tiempo_transcurrido);
    
answered by 04.05.2018 / 18:35
source