help with time handling in Time with php

Asked

Viewed 21 times

0

I have a code where I store in the database the time of posting in time format and I want to show the time of posting in the format "posted to 1 day" for example, the hour, minutes and seconds I have managed, I would like help to put days, months and years, if there’s a way to do it without having to take for example every 24 hour a day.

 public function postTime($date) {

    $time = time() - $date;
    $h = floor($time / 3600);
    $time -= $h * 3600;
    $m = floor($time / 60);
    $time -= $m * 60;
    $s = $time;

    if ($h > 0) {
        $this->txt = $h == 1 ? "A uma hora" : "A ## horas";
        $this->txt = preg_replace("/#([#]+)/i", $h, $this->txt);
    }
    if ($m > 0) {
        if ($h > 0) {
            $this->txt .= " e ";
        }
        $this->txt .= $m == 1 ? " A um minuto" : "A ## minutos" ;
        $this->txt = preg_replace("/#([#]+)/i", $m, $this->txt);
    }
    if ($h == 0 && $m == 0) {
        if ($s == 0) {
            echo "Agora";
        }
        $this->txt = $s == 1 ? "A um segundo" : "A ## segundos";
        $this->txt = preg_replace("/#([#]+)/i", $s, $this->txt);
    }
    echo $this->txt;
}

}

  • There are a number of things we need to know. What have you done? Do you have any sample code? What is the format of the date you are saving in the bank?

  • For hours I am saving in time format();

No answers

Browser other questions tagged

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