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
Kriek
Forum Contributor
Posts: 238 Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:
Post
by Kriek » Mon Feb 24, 2003 7:58 pm
Converting date format from MySQL (yyyy-mm-dd) field (type=date) into a word format using PHP.
Example: 2003-02-24 into February 24th 2003
The problem is when I echo $date It displays as December 31st 1969 instead of February 24th 2003
Code: Select all
<?php
include "config.php";
$db = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db ($db_name) or die ("Cannot connect to database");
$query = "SELECT title, news, author, date FROM news ORDER BY id DESC LIMIT 10";
$result = mysql_query($query);
while ($r = mysql_fetch_array($result)) {
$title = $r["title"];
$news = $r["news"];
$author = $r["author"];
$date = date("F jS Y", $r["date"]);
}
mysql_close($db);
?>
hob_goblin
Forum Regular
Posts: 978 Joined: Sun Apr 28, 2002 9:53 pm
Contact:
Post
by hob_goblin » Tue Feb 25, 2003 12:28 am
Code: Select all
<?php
$date = "2003-02-24";
$date = strtotime($date);
echo date("F jS Y", $date);
?>
works for me!
http://www.php.net/strtotime
So try this:
Code: Select all
<?php
include "config.php";
$db = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db ($db_name) or die ("Cannot connect to database");
$query = "SELECT title, news, author, date FROM news ORDER BY id DESC LIMIT 10";
$result = mysql_query($query);
while ($r = mysql_fetch_array($result)) {
$title = $rї"title"];
$news = $rї"news"];
$author = $rї"author"];
$date = strtotime($rї"date"]);
$date = date("F jS Y", $date);
}
mysql_close($db);
?>
Always_Stuck
Forum Newbie
Posts: 5 Joined: Mon Feb 24, 2003 5:56 am
Post
by Always_Stuck » Tue Feb 25, 2003 4:19 am
I have never been able to help anyone with code before, but will give this one a shot... this is the code I use
Code: Select all
<?php
// The following $Date_Posted is actually stored in mysql
$Date_Posted = "2002-01-17";
// Break up the date and store the year, month and date into an array
list($y,$m,$d) = explode('-', $Date_Posted);
// Use mktime to convert the date into the format "17th January 2003"
$phptodaysdate = date("jS F Y", mktime(0, 0, 0, $m, $d, $y));
?>
The previous code will turn "2002-01-17" into "17th January 2003"
Hope that helps
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Tue Feb 25, 2003 5:43 am
Although with MySQL doing the work you do cut down on a few lines of code.
Mac
Kriek
Forum Contributor
Posts: 238 Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:
Post
by Kriek » Tue Feb 25, 2003 12:09 pm
Thanks guys!
Dan, that was exactly what I was looking for =)