PDA

Click to See Complete Forum and Search --> : Format PHP Date


JamesNZ
Jan 21st, 2005, 03:33 PM
I save dates to the database in the following format - "1979-12-28"

When displaying them on the website I want to display it as "28 Dec 1979" so I tried this:

echo "Born:" . date("d M Y",$myrow["DOB"]) . "<br>";

but that converts "1979-12-28" to "Born:31 Dec 1969"

What am I doing wrong?

visualAd
Jan 21st, 2005, 03:48 PM
Are you using MySql? Databases often store dates in a different format. In MySql can convert the date in your database to a UNIX timestamp inside your query:

SELECT FROM table UNIX_TIMESTAMP(date) date;


Once this is done you can use it in the date function.

JamesNZ
Jan 21st, 2005, 03:50 PM
Thanks for your response. I'd rather keep it stored in my MYSQL database in date format for ease of use.

Isn't there anyway to convert "1979-12-28" to "28 Dec 1979"?

visualAd
Jan 21st, 2005, 03:56 PM
Its still going to be stored in date format. You will be converting it to the format PHP understands inside the SELECT query, it won't change the format in which the data is stored in your database.

Post your query and I'll show you what I mean if you still don't follow.

Phenix
Jan 21st, 2005, 03:57 PM
Yes, the timestamp argument "$myrow["DOB"]" is in the wrong format.
string date ( string format [, int timestamp] )

Try:
echo "Born:" . date("d M Y",mktime(0, 0, 0, substr($myrow["DOB"],5,2), substr($myrow["DOB"],8,2), substr($myrow["DOB"],0,4))) . "<br>";

Edit: Actually, changing the query may be cleaner, but you'll need say an alias like UNIX_TIMESTAMP(Your_DOB_column) AS DOB_AsTimeStamp and use $myrow["DOB_AsTimeStamp"]
echo "Born:" . date("d M Y",$myrow["DOB_AsTimeStamp"]) . "<br>";

JamesNZ
Jan 21st, 2005, 04:12 PM
Thanks Phenix.

Used:

<b>Born: </b> <?php echo date("d M Y",mktime(0, 0, 0, substr($DOB,5,2), substr($DOB,8,2), substr($DOB,0,4))); ?> <br>

and it worked a treat.

Thanks for your help also visualAd :)

McCain
Jan 21st, 2005, 07:36 PM
You could try the strtotime() (http://www.php.net/manual/en/function.strtotime.php) function.

It would look something like this:
echo 'Born: ' . date('d M Y',strtotime($myrow['DOB'])) . '<br />';

visualAd
Jan 25th, 2005, 03:56 PM
Thanks Phenix.

Used:

<b>Born: </b> <?php echo date("d M Y",mktime(0, 0, 0, substr($DOB,5,2), substr($DOB,8,2), substr($DOB,0,4))); ?> <br>

and it worked a treat.

Thanks for your help also visualAd :) James,

Most databases store dates in a different format, all these databases have the functions requied to convert form their native date types to unix timestamps.

Its all very well using the solution posted by Penix, but it will be both slower and will break should MySql one day decide to change thier date format or you need to use another database, it will break. So what at first, looks good and easy to use, in the long run it will probably give you a headache.

JamesNZ
Jan 25th, 2005, 09:44 PM
James,

Most databases store dates in a different format, all these databases have the functions requied to convert form their native date types to unix timestamps.

Its all very well using the solution posted by Penix, but it will be both slower and will break should MySql one day decide to change thier date format or you need to use another database, it will break. So what at first, looks good and easy to use, in the long run it will probably give you a headache.

What solution would you suggest visualAd?

McCain
Jan 26th, 2005, 02:56 AM
I think this is what he is suggesting:

Are you using MySql? Databases often store dates in a different format. In MySql can convert the date in your database to a UNIX timestamp inside your query:

SELECT FROM table UNIX_TIMESTAMP(date) date;


Once this is done you can use it in the date function.

visualAd
Jan 27th, 2005, 11:53 AM
What solution would you suggest visualAd? The one I posted which involves doing the conversion inside the SELECT query using the UNIX_TIMESTAMP() function. This will be quicker and more efficient than using PHP's string manipulation functions and make your code mroe portable.

r_amin
Sep 23rd, 2007, 06:27 AM
I need It, But from 2005 This problem not solved !!!!!!!!!!
How can I show a date in the format of 11/12/2007 from database?

penagate
Sep 23rd, 2007, 08:43 AM
Start your own thread for your own questions please.