Format PT BR date and time return in PHP

Asked

Viewed 3,045 times

0

I’m using the function strtotime of PHP to format the date that come in the database SQL SERVER and is working normally on the local server, but on the web returns the date 12-31-1969 21:00 PM. Someone knows the reason why?

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

$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)
  • 1

    Wouldn’t it be simpler to already have the date formatted? see that answer or that other

  • 1

    The best would be to tidy up the bank, and use a date field without visual formatting. In the strtotime manual you have date interpretation rules, and if you do not want to depend on locale, you need to use yyyyyy-mm-dd hh:mm:ss format

  • It would be easier to have formatted but I don’t have access to the Inserts and the whole base is in this format. I’ll check the strtotime manual.

  • 1

    @In the worst case you can break the string into spaces and rearrange before you play strtotime, or even use another function. It’s far from ideal, but it might give you some greater control.

  • @Could you give a var_dump($timestamp) and inform me of the result both on the qnt server on the localhost?

2 answers

3

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 sequence of symbols for some date formatting function.

In the example below, the method createFromFormat() 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 is good to check if the return of DateTime::createFromFormat is valid. Otherwise, it may cause fatal error when calling the method format() of a missing object.

Note: The Datetime class is available from PHP5.3

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

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

http://php.net/manual/en/datetime.createfromformat.php

  • It works normally on the local server I use (Scriptcase) but on the UOL server on the web it gives this error Call to Undefined method Datetime::createFromFormat(). In fact everything that testo works locally and not on the Web. It must be the PHP version of UOL I don’t know .. thanks anyway I will continue the tests

  • That’s right! Thanks anyway !

1

Because the server does not support multiple functions by using version of PHP old, I’ll post how I resolved to eventually help someone who might have the same problem. As my friend Daniel says, it’s not the best solution, but it gets 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

Browser other questions tagged

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