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