Jump to content

Format how date is displayed.


only1perky

Recommended Posts

Hi Guys,

 

I am trying to take a data from my database which is set as a 'date' in 'yyyy-mm-dd' format.

I would now like to query my database, but have the date echoed in the format 'dd-mm-yyyy'.

 

I have searched through dozens of websites and I either get a date displayed as 01-01-1970 or a bunch of crazy error which confuses me beyond believe.

 

Could someone please walk me through solving this.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/147288-format-how-date-is-displayed/
Share on other sites

At the moment I have this query to select all events which are currently open:

 


$query_rsevents = "SELECT * FROM events where start_date <= NOW() AND end_date >= NOW()";

 

I am then simply echoing the results with:

 

<strong>Start Date: </strong><?php echo ($row_rsevents['start_date']); ?>

 

This works but obviously displays the date as yyyy-mm-dd as is the mysql date format. I would like it displayed as dd-mm-yyyy.

 

Cheers.

 

a longer way around it i have been using is

 

$rsevents     = $row["rsevents"];
$date = explode("-","$rsevents");
$newdate = strftime("%d-%m-%Y",mktime(0,0,0,$date[1],$date[2],$date[0]));

print $newdate;

 

sorry but its the only way i know and then to store it again in the database have to explode it again and do the date arrays in a different order to put it back to yyyy-mm-dd format

 

$date2 = explode("-","$newdate");
$newdate2 = strftime("%Y-%m-%d",mktime(0,0,0,$date2[1],$date2[0],$date2[2]));

 

<?php echo date("d-m-Y",strtotime($row_rsevents['start_date'])); ?>

 

 

preferred method, because of optimization is to actually allow MySQL to format the date.

 

This is only preferred if you are only echoing out that date and not manipulating it further.

 

More prefered is a UNIX time stamp as this removes the need to strtotime the value, and seconds are considered the lowest unit of manipulation in MySQL

 

This will be useful to you

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.