Page 1 of 1

Format Date when pulled from Database for Display

Posted: Tue Sep 02, 2008 6:26 am
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 :)

Re: Format Date when pulled from Database for Display

Posted: Tue Sep 02, 2008 7:01 am
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

Re: Format Date when pulled from Database for Display

Posted: Tue Sep 02, 2008 7:05 am
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");
?>

Re: Format Date when pulled from Database for Display

Posted: Tue Sep 02, 2008 7:07 am
by starram
By the way instead of using %M try using %b

Re: Format Date when pulled from Database for Display

Posted: Tue Sep 02, 2008 8:19 am
by arunkar
Kool!! It worked like a charm :)


Thank You!

Re: Format Date when pulled from Database for Display

Posted: Tue Sep 02, 2008 8:44 am
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...

Re: Format Date when pulled from Database for Display

Posted: Tue Sep 02, 2008 9:00 am
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?

Re: Format Date when pulled from Database for Display

Posted: Tue Sep 02, 2008 9:10 am
by ahowell