Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to show a formatted date?

Posted on 2012-03-14
4
Medium Priority
?
276 Views
Last Modified: 2012-03-14
I have a value from a field datetime in a mysql database and I want to show it formatted as a mm-dd-yyyy using php, how could be possible?
0
Comment
Question by:dimensionav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 24

Accepted Solution

by:
johanntagle earned 1000 total points
ID: 37723207
You can use the date_format function in mysql (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format) when you do your select i.e.

SELECT date_format(column_name,'%m-%d-%Y') ....
0
 
LVL 13

Assisted Solution

by:Hugh McCurdy
Hugh McCurdy earned 1000 total points
ID: 37723208
First convert your MySQL date to PHP format with strtotime()

$phpdate = strtotime( $mysqldate );

http://www.w3schools.com/php/func_date_strtotime.asp

Then you want to format it with date

$formatted = date ( m-d-Y );

http://www.w3schools.com/php/func_date_date.asp
0
 

Author Comment

by:dimensionav
ID: 37723238
If the date appears in english like this 01-jan-2012 and I want it in a different language what could I do?
0
 
LVL 13

Expert Comment

by:dsmile
ID: 37723261
If you want to display date in different languages, you need to create your own function for that.

Eg:

function getMonthText($month, $lang) {
     global $config;
     return $config['$lang']['month'][$month'];
}

Open in new window


and you have your $config prepared like this (in some config file)

//month text for english
$config['en']['month'][1] = 'January';
$config['en']['month'][2] = 'February';
$config['en']['month'][3] = 'March';
...

Open in new window


and then the formatted date (where $timestamp is result of strtotime() with your date from DB)

$date = getMonthText(date('j', $timestamp), 'en') .'-'. date ('d-Y', $timestamp);

Open in new window

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question