Solved

How to show a formatted date?

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

18 Experts available now in Live!

Get 1:1 Help Now