I am storing date in yyyy-mm-dd in database. Now i want to display that date in frontend but while displaying date it should be in dd-mm-yyyy format. How to display it in this format please help <?php echo $data2['emp_dob'];?>
. I dont want to change my current format of storing date in yyyy-mm-dd. please help me to display it in dd-mm-yyyy format only
Asked
Active
Viewed 2,140 times
0

Jens
- 67,715
- 15
- 98
- 113

mahadev sutar
- 171
- 2
- 16
-
1http://stackoverflow.com/a/2487938/2286537 – krishn Patel Mar 27 '17 at 07:11
-
this question is duplicate to a duplicate to a duplicate – Nodir Rashidov Mar 27 '17 at 07:13
-
its already varchar bro. – mahadev sutar Mar 27 '17 at 07:13
6 Answers
1
I don't think it's strictly correct to say that you're storing dates in your MySQL database as yyyy-mm-dd
. The internal representation may be something very different than this, but in any case your real question is how to format the date in a certain way. One option is to handle this in your actual MySQL query using DATE_FORMAT()
, e.g.
SELECT DATE_FORMAT(date_col, '%d-%m-%Y')
FROM yourTable

Tim Biegeleisen
- 502,043
- 27
- 286
- 360
0
if (preg_match("/([0-9]{4})\-([0-9]{2})\-([0-9]{2})/", $data2['emp_dob'], $rg))
$data2['emp_dob'] = $rg[3]."-".$rg[2]."-".$rg[1]

diavolic
- 722
- 1
- 4
- 5
-
-
i provided universal method, if we don't know how data stored in mysql, as a string or as data type – diavolic Mar 27 '17 at 07:33
0
use this function, which uses inbuilt function 'substr(str,start,length)'.
function getFormatDate($date){
$date = substr($date,8,2).'-'.substr($date,5,2).'-'.substr($date,0,4);
return $date;
}
getFormatDate($data2['emp_dob']);

reoxey
- 684
- 1
- 7
- 18
-
`echo date("d-m-Y", strtotime($data2['emp_dob']));` You can pass date format specifier to make date format using `date()` Function. Here is the doc http://php.net/manual/en/function.date.php – Sumon Sarker Mar 27 '17 at 07:20