Convert Stamp time to full date with php

Asked

Viewed 681 times

1

I have on my bench a field with timestamp datetime and I need to convert the timestamp for this format example:

WEDNESDAY, DECEMBER 31, 1969 posted at 3:30 pm

I searched here and found a code, but I can’t make it work it returns an error:

setlocale(LC_ALL, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');
$var_DateTime = $dd;
return utf8_encode(ucwords(strftime('%A', $var_DateTime->sec)).', '
    .strftime('%d', $var_DateTime->sec).
    ' de '.ucwords(strftime('%B', $var_DateTime->sec))
    .' de '.strftime('%Y', $var_DateTime->sec));

How to format this date?

3 answers

3

If the timestamp is right It will happen like this:

return utf8_encode(
    strtoupper(
    strftime('%A, %e de %B de %Y postado às %H:%M', 
        $var_DateTime->sec)
    ));
  • gave the emsmo error NOTICE: TRYING TO GET PROPERTY OF NON-OBJECT IN E: WAMP64 WWW DAGAZ SITE ESSENCIALS CONNECTION.INC.PHP ON LINE 170

  • My team Stamp this so 2016-10-25 22:36:52

  • the error has nothing to do with this code, it’s saying that $var_DateTime it’s not an object, so it can’t get the property sec

  • What Ricardo said is true. You have to better contextualize the question if it’s not the problem in question @Jasarorion. His code would work correctly. I just think it might be unnecessary to use utf8_encode in this case, but is the solution to your date problem.

  • I believe, then, by comment, could correct this response by transforming the value for the respective type of data (DateTime) as far as I could make out.

  • And what would be according to the Stamp team that is in db ? and $var_DateTime->sec if I put only $var_DateTime it says it is not a valid date. ( non well Formed number )

  • Puts var_dump( $var_DateTime ); just before the return and glue here what he prints on canvas.

Show 2 more comments

0

If your date comes like this 2016-10-25 22:36:52, then:

<?php

setlocale(LC_ALL, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');

$data = strtotime('2016-10-25 22:36:52');

echo utf8_encode(
    strtoupper(
    strftime('%A, %e de %B de %Y postado às %H:%M', 
        $data)
    ));

-2

found a function that solved my problem

function dataEmPortugues ($timestamp, $hours = FALSE, $timeZone = "Europe/Lisbon") {

    $dia_num = date("w", $timestamp);// Dia da semana.

    if($dia_num == 0){
    $dia_nome = "Domingo";
    }elseif($dia_num == 1){
    $dia_nome = "Segunda";
    }elseif($dia_num == 2){
    $dia_nome = "Terça";
    }elseif($dia_num == 3){
    $dia_nome = "Quarta";
    }elseif($dia_num == 4){
    $dia_nome = "Quinta";
    }elseif($dia_num == 5){
    $dia_nome = "Sexta";
    }else{
    $dia_nome = "Sábado";
    }

    $dia_mes = date("d", $timestamp);// Dia do mês

    $mes_num = date("m", $timestamp);// Nome do mês

    if($mes_num == 01){
    $mes_nome = "Janeiro";
    }elseif($mes_num == 02){
    $mes_nome = "Fevereiro";
    }elseif($mes_num == 03){
    $mes_nome = "Março";
    }elseif($mes_num == 04){
    $mes_nome = "Abril";
    }elseif($mes_num == 05){
    $mes_nome = "Maio";
    }elseif($mes_num == 06){
    $mes_nome = "Junho";
    }elseif($mes_num == 07){
    $mes_nome = "Julho";
    }elseif($mes_num == 08){
    $mes_nome = "Agosto";
    }elseif($mes_num == 09){
    $mes_nome = "Setembro";
    }elseif($mes_num == 10){
    $mes_nome = "Outubro";
    }elseif($mes_num == 11){
    $mes_nome = "Novembro";
    }else{
    $mes_nome = "Dezembro";
    }
    $ano = date("Y", $timestamp);// Ano

    date_default_timezone_set($timeZone); // Set time-zone
    $hora = date ("H:i", $timestamp);

    if ($hours) {
        return $dia_nome.", ".$dia_mes." de ".$mes_nome." de ".$ano." - ".$hora;
    }
    else {
        return $dia_nome.", ".$dia_mes." de ".$mes_nome." de ".$ano;
    }
}

echo dataEmPortugues(strtotime("2014-07-17 21:49:23"), TRUE, "America/Sao_Paulo");
  • 1

    -1 The solution below perfectly solves the problem, and this code has an unnecessary complication.

Browser other questions tagged

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