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
Wouldn’t it be simpler to already have the date formatted? see that answer or that other
– rray
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
– Bacco
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.
– denis
@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.
– Bacco
@Could you give a
var_dump($timestamp)
and inform me of the result both on the qnt server on the localhost?– Jeferson Almeida