?
Solved

How to show a formatted date?

Posted on 2012-03-14
4
Medium Priority
?
275 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

765 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