Generally we store the dates in mysql database with 'date' datatype, and when we have to show them in the front end, we just cant display it the same way.
Because it is difficult for the users to understand quickly when they read the date format as YYYY-MM-DD, which mysql uses.
So when displaying a mysql date in front end, you can just use this function, which takes the mysql date as parameter and returns the neatly formatted date, including the week day.
php code:
<?php
function format_date($date)
{
$date = getdate(strtotime($date));
return $date['mday'].' - '.$date['month'].' - '.$date['year'].', '.$date['weekday'];
}
$date = date("Y-m-d"); // Today's date (You get it from database in real time)
echo "<b>Date of birth: </b>".format_date($date); // format it
?>
For ex, a person's date of birth is stored as date type in database.
It is shown in the front end after passing through the function.
Because it is difficult for the users to understand quickly when they read the date format as YYYY-MM-DD, which mysql uses.
So when displaying a mysql date in front end, you can just use this function, which takes the mysql date as parameter and returns the neatly formatted date, including the week day.
php code:
<?php
function format_date($date)
{
$date = getdate(strtotime($date));
return $date['mday'].' - '.$date['month'].' - '.$date['year'].', '.$date['weekday'];
}
$date = date("Y-m-d"); // Today's date (You get it from database in real time)
echo "<b>Date of birth: </b>".format_date($date); // format it
?>
For ex, a person's date of birth is stored as date type in database.
It is shown in the front end after passing through the function.
Very informative and useful. Thanks.
ReplyDelete