Ash Ash - 6 months ago 21
SQL Question

Changing date format from MySQL in php

Currently, this code :

<?php echo $user->last_login?>
is showing this date format
YYYY-MM-DD
from MySQL. How do i change it to this format
DD-MM-YYYY
?

Thanks.

Edit : Problem solved.
$date = new DateTime($user->last_login);
echo $date->format('Y-m-d H:i:s');


Thanks guys!

Answer

You can do this:

date(d-m-Y", strtotime($user->last_login));

Or this:

$date = new DateTime($user->last_login);
echo $date->format('d-m-Y');

Or use Carbon (http://carbon.nesbot.com/docs/):

Carbon::createFromFormat('Y-m-d', $user->last_login)->format('d-m-Y');
Comments