Format Date when pulled from Database for Display

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arunkar
Forum Commoner
Posts: 50
Joined: Mon Feb 25, 2008 10:37 pm

Format Date when pulled from Database for Display

Post by arunkar »

Hi Experts

i'm trying to format the dates when I retrieve it from Mysql db. I what to display the date as '02-Mar-2008'.

How do I format the date, when I pull it from the database?

I looked at the PHP manule.. but I couldn't work out the way...

This is how the date is stored in my table:

Code: Select all

Field                                    Type
ContributionDateTimeStamp         datetime
thanks Experts :)
arunkar
Forum Commoner
Posts: 50
Joined: Mon Feb 25, 2008 10:37 pm

Re: Format Date when pulled from Database for Display

Post by arunkar »

I did this below, but I'm getting

Code: Select all

 
    $query = mysql_query("Select p.id, c.ContributionType, date_format(c.ContributionStartDate,'%d%-%M%-%Y') as ContributionStartDate, date_format(c.ContributionEndDate,'%d%-%M%-%Y') as ContributionEndDate, date_format(c.ContributionDateTimeStamp,'%d%-%M%-%Y') as ContributionDateTimeStamp, pj.ProjectTitle  From tbl_partnering_data As p")
 
 
I get the output as '02-August-2008' ,

What can I do to display the date as '02-Aug-2008' ??

Thank
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: Format Date when pulled from Database for Display

Post by starram »

Hello,

You can use the following function:

Code: Select all

<?php
function formatDate2($cmpdate)
{
    $cmpdate1=explode("-",$cmpdate);
    return $cmpdate=$cmpdate1[2].'-'.date("M",mktime(0,0,0,$cmpdate1[1],$cmpdate1[2],$cmpdate1[0])).'-'.$cmpdate1[0];
}
echo formatDate2("2008-07-15");
?>
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: Format Date when pulled from Database for Display

Post by starram »

By the way instead of using %M try using %b
arunkar
Forum Commoner
Posts: 50
Joined: Mon Feb 25, 2008 10:37 pm

Re: Format Date when pulled from Database for Display

Post by arunkar »

Kool!! It worked like a charm :)


Thank You!
ahowell
Forum Newbie
Posts: 17
Joined: Mon Sep 01, 2008 9:18 pm

Re: Format Date when pulled from Database for Display

Post by ahowell »

You can also use the date object...

Code: Select all

<?php
 
$date = date_create('02-August-2008');
 
echo $date->format('d-M-Y');     // 02-Aug-2008
echo $date->format('m/d/Y');     // 08/02/2008
echo $date->format('U');         // 1217649600
//etc...
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: Format Date when pulled from Database for Display

Post by starram »

# $date = date_create('02-August-2008');
#
# echo $date->format('d-M-Y'); // 02-Aug-2008
# echo $date->format('m/d/Y'); // 08/02/2008
# echo $date->format('U'); // 1217649600
I guess this is supported by version 5 and later only?
Post Reply