Format date and time return PT BR in PHP

0

I am using the strtotime function of PHP to format the date that comes in the SQL SERVER database and is working normally on the local server , but on the web it returns the date 12-31-1969 21:00 PM. Does anyone know why?

(As it is recorded in the bank) = Jan 31 2017 4:36 PM

$timestamp = $row['DtPedido'];
echo date('d/m/Y H:i:s', strtotime($timestamp)); 
//imprime 31/01/2017 16:36:00  (LOCAL - CORRETO)
//imprime 12-31-1969 21:00:00  (WEB)
    
asked by anonymous 01.02.2017 / 15:30

2 answers

2

The format presented in the question has the following symbols:

M d Y H: iA

M -> Representação textual do mês, abreviado
d -> Dia, 2 dígitos
Y -> Ano, 4 dígitos
H -> Hora, 2 dígitos
i -> Minuto, 2 dígitos
A -> Período em letra maiúscula (AM/PM)

With this, you can enter the symbol sequence for some date formatting function.

In the example below, the createFromFormat() method of class DateTime :

$str = 'Jan 31 2017 4:36PM';
if ($date = DateTime::createFromFormat('M d Y H:iA', $str)) {
    //echo $date->format('Y-m-d H:i:s'); // Formato ISO 8601
    echo $date->format('d/m/Y H:i:s'); // O formato que você quer.
}

It's good to check if the return of DateTime::createFromFormat is valid. Otherwise, it may cause fatal error when invoking the format() method of a faulted object.

Note: The DateTime class is available from PHP5.3

Alternatively , you can do the formatting with the functions date() and strtotime() .

$str = 'Jan 31 2017 4:36PM';
echo date('d/m/Y H:i:s', strtotime($str));

link

    
01.02.2017 / 17:01
1

Because the server does not support multiple functions by using the old PHP version, I'll post it as I decided to eventually help someone who may have the same problem. As Daniel says, it's not the best solution, but it's friendlier to show the date.

$timestamp = $row['DtPedido'];
$sliceDt = explode(" ",  $timestamp);
$sliceH = explode("  ",  $timestamp);
$data =  $sliceDt[1]." de ".$sliceDt[0]." de ".$sliceDt[2] . " - ".$sliceH[1];  
//imprime 31 de Jan de 2017 - 4:36PM
    
02.02.2017 / 14:29