Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I change the date format using the echo command?

Status
Not open for further replies.

Chelsea7

Programmer
Aug 25, 2008
69
US
I have one basic question about date format. As we know mysql uses yyyy-mm-dd format. I'm trying to get the correct format using the echo command so the date reads mm-dd-yyyy.

Everything else works fine. Here's my code for the DATE field. The print date command works fine but this is in a table.

----------
echo "<tr>";
echo "<td>".$row['date']."</td> ";


print date('M d Y', strtotime($row['date']));
echo "</tr>";
----------
Can print be used instead or should I stick with echo?

Thanks,
Chelsea
 
print and echo can be used interchangeably.

mm-dd-yyyy is represented thus

Code:
echo date('m-d-Y', strtotime($row['date']));

more information here:
ensure that you have the default time zone set.
Code:
date_default_timezone_set('UTC'); //or whatever it is you need by way of timezone
more information here
 
You can also ask MySQL to do the date_format for you.

Example query:

Code:
SELECT DATE_FORMAT([i]your_date_field_name[/i], '%m-%d-%Y') FROM [i]your_table_name[/i] WHERE [i]some condition or however you select...[/i];

Best,

Jakob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top