Solved

How to show a formatted date?

Posted on 2012-03-14
4
270 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

733 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