-
Howdy all,
I am pulling dates from mysql in yyyy-mm-dd format. I would like to display them in another format, either mm.dd.yyyy or month name, dd, yyyy.
I found the below script and it would work, but I have 5 "updates" being displayed on one page and I don't want to interfere with the display of the content. View http://www.plusoneonline.com/home to see what I am talking about.
Code:
$date = "2000-12-11";
$temp = explode("-", $date);
$year = $temp[0];
$month = $temp[1];
$day = $temp[2];
print date ("l, F jS, Y", mktime (0,0,0,$month,$day,$year));
What would be the best way of doing this? I am using this code to pull from the DB.
Code:
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$conn) {
echo "<P>Unable to connect to the database server at this time.</P>";
exit();
}
if (! @mysql_select_db($dbname) ) {
echo( "<P>Unable to locate the database at this time.</P>" );
exit();
}
$result = mysql_query("SELECT date, content FROM po_updates ORDER BY date DESC LIMIT 5;");
if (!$result) {
echo "<P>Error performing query on database";
exit();
}
while ($row = mysql_fetch_array($result)) {
echo "<span class=\"divider\" width=\"100%\">";
if ($row["date"] > 3) {
echo "<img src=\"../images/image_new.gif\" width=\"30\" height=\"15\" border=\"0\" align=\"left\">";
}
echo " ".$row["date"]."</span><br><br>";
echo $row["content"];
echo "<br><br>";
}
?>
The part in bold is a way for me to show a "NEW" image for entries that are newer than 3 days (I am still working on that part)
-
Yep, I was using a similar method until I discovered this method, courtesy of FreddyDoesPHP
$result = mysql_query("SELECT DATE_FORMAT(datefield, '%M, %D, %Y') as something, content FROM po_updates ORDER BY date DESC LIMIT 5;");
you can then access it by using $row["something"]
You can change the format by using these entities...
%M Month name (January..December)
%W Weekday name (Sunday..Saturday)
%D Day of the month with english suffix (1st, 2nd, 3rd, etc.)
%Y Year, numeric, 4 digits
%y Year, numeric, 2 digits
%a Abbreviated weekday name (Sun..Sat)
%d Day of the month, numeric (00..31)
%e Day of the month, numeric (0..31)
%m Month, numeric (01..12)
%c Month, numeric (1..12)
%b Abbreviated month name (Jan..Dec)
%j Day of year (001..366)
%H Hour (00..23)
%k Hour (0..23)
%h Hour (01..12)
%I Hour (01..12)
%l Hour (1..12)
%i Minutes, numeric (00..59)
%r Time, 12-hour (hh:mm:ss [AP]M)
%T Time, 24-hour (hh:mm:ss)
%S Seconds (00..59)
%s Seconds (00..59)
%p AM or PM
%w Day of the week (0=Sunday..6=Saturday)
%U Week (0..52), where Sunday is the first day of the week
%u Week (0..52), where Monday is the first day of the week
%% A literal `%'.
<Edited by PeterW on 01-15-2001 at 03:11 PM>
-
Peter, did you mean to have 2 SELECT's in your statement?
-
Weird!!!
Using this statement
SELECT DATE_FORMAT(date, '%m.%d.%Y') as date, content FROM po_updates ORDER BY date DESC LIMIT 5
I don't get any of the updates I have in my DB for January 2001. I only get the ones dating back to the end of December.
I have the datestamp entered in the DB as yyyy-mm-dd.
-
lol no ;)
I think it is because it will be now order by month.day.year, so the highest months will come first..
Thus, use something different to "date".
-
Another weird thing is that statement that I listed above works on my PHP page, but it does not work when I type it into the query field in phpMyAdmin 2.1.0.
-
is there way to ordet the dates first and THEN change them?
-
Peter ...never mind about that. I entered them into the DB in chronological order with an ID field so I will sort on the ID field.
-
or could you just select the date as a different name like prettyd?
<edit>Sounds just as good.. bit more hassle though ;)</edit>
Peter
-
actually, it was no more hassle. I just tried it and it works. That is more reliable anyway. Thanks Peter.
enjoy your Karma