Format A Date... quick question

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
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Format A Date... quick question

Post by seodevhead »

Hey guys... I have a database that has dates stored in the following format:

2006-04-05

Is there anyway in PHP to convert this to April 5, 2006? Even better, knock off the year and just get April 5? Thanks for your help!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You could use the databases' own functionality such at MySQL's DATE_FORMAT function.

For PHP to handle it correctly, explode() it then pass the data to mktime() and finally that result through date() in the format string you wish.
User avatar
irisblaze
Forum Newbie
Posts: 22
Joined: Sun Mar 19, 2006 3:24 am
Location: Palestine
Contact:

Post by irisblaze »

Code: Select all

<?php
$timestamp = strtotime("2006-04-05");
//print date as April 05, 2006
echo date("F d, Y", $timestamp);
echo "<br/>";
//print date as April 05
echo date("F d", $timestamp);
?>
Now it should work
Last edited by irisblaze on Wed Apr 05, 2006 2:18 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

unfortunately, strtotime() may read the date wrong, depending on locale and other things, so be very careful using it in this case.
User avatar
irisblaze
Forum Newbie
Posts: 22
Joined: Sun Mar 19, 2006 3:24 am
Location: Palestine
Contact:

Post by irisblaze »

feyd wrote:unfortunately, strtotime() may read the date wrong, depending on locale and other things, so be very careful using it in this case.

not if you pass the date in the right format!

I mean let's say that you passed the string as 2006-12-20, does strtotime returns time stamp for the date 2006-20-12 depending on the locale?
Post Reply