Solved

How to show a formatted date?

Posted on 2012-03-14
4
265 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
4 Comments
 
LVL 24

Accepted Solution

by:
johanntagle earned 250 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 250 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculate values in an array 18 32
How do I fix this error:  Warning: strpos(): Empty needle on line 122 3 14
WordPress syntax 2 23
html input type 3 21
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

947 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now