Date is important aspect in developing applications such as storing user registration date time ,last login time, date of birth etc.

Mysql has standard format that is YYYY-mm-dd and but we want that to be in regional or in some other format.

using strtotime and date function printing only date

Php Code
<?php

$mysqlDateTime = '2016-10-17 21:46:59';
echo date("m/d/Y", strtotime($mysqlDateTime));
echo "<br>";

?>

using strtotime and date function printing in the form of timestamp

Php Code
<?php

$old_date = date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d H:i:s', $old_date_timestamp);
echo $new_date;
echo "<br>";

?>
[ad type=”banner”]

strtotime and date printing only date in text format

Php Code
<?php

$mysqlDateTime = '2016-10-17 21:46:59';
$date = strtotime($mysqlDateTime);
echo date("j F Y", $date); //output will be 10 July 2013

?>

Using DateTime object in php and format function

Php Code
<?php

$old_date = date('l, F d y h:i:s');
$date = new DateTime($old_date);
echo $date->format('Y-m-d H:i:s');

?>

Using date_create and date_format format function

Php Code
<?php

$old_date = date('l, F d y h:i:s');
$date = date_create($old_date);
echo date_format($date, 'Y-m-d H:i:s');
echo "<br>";
[ad type=”banner”]

Categorized in: