Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date display problem

Status
Not open for further replies.

meeble3

Programmer
Nov 8, 2004
52
GB
Hello,

I have a date in the database as a date field.

Instead of dd-mm-yyyy I just want it to say mm-yyyy.

Is this possible?

Cheers

James
 
Sure, it's possible. But it's difficult to get more specific than that without knowing what kind of database you're using.

How you do it depends on the database you're using. MySQL, for example, has a date_format() function which allows you to format a date value as you need. With another data source, you may have to use PHP's strtotime() and date() functions.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
As far as I'm aware, most databases require the day of the month as part of the date. However, you can retrieve just the month and the year if that's all you need.

If I am using just the month and year, I always set the day to 1 (i.e. 01-mm-yyyy for whatever mm and yyyy) in the database, and then disregard the 01 after retrieving the date.

Hope this helps.
 
Oof. Thanks aardvark92. Your post pointed out that mine wasn't clear.

The sentence in my last post which read:

MySQL, for example, has a date_format() function which allows you to format a date value as you need.

should read:

MySQL, for example, has a date_format() function which allows you to format a date value as you need when you fetch date values from a table.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Here ya go

<?
// Date formatting
// Month Types: M = May m = 05
// Day Types: D = Tue d = 02
// Year Types: Y = 2004 y = 04

function formatDate($val)
{
$date = date("M-Y"); // Put layout requirements here
return $date;
}
$date = formatDate('timestamp');
echo "$date \n";
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top