PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Retrieving formatted date in select query
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Retrieving formatted date in select query

Réponse
 
LinkBack Outils de la discussion
Vieux 05/01/2008, 02h24   #1
seastl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Retrieving formatted date in select query


Hello,

I am a novice (very novice) webmaster, and am trying to retrieve the
most recent record from a database to display on a homepage. It all
works well enough with one exception, which I am sure is just my
overlooking something - I cannot change the date format. When I change
the field "DTE" to a date_format in the query as below, my date
results are blank. Looking at the code below, can someone please
suggest what I might be doing incorrectly? Any is greatly
appreciated -

****
mysql_connect("localhost", "username", "password") or
die(mysql_error());
mysql_select_db("stjojone_primary") or die(mysql_error());
$data = mysql_query("SELECT
LTRGCL_DTE,date_format(DTE,'%W,%M,%D,%Y'),BLTN_FIL E FROM
bulletin_store ORDER BY DTE DESC LIMIT 0,1") or die(mysql_error());
Print "<div align=center><table class=\"SJTableInfo\">";
Print "<tr><td align=center><h5>Date</h5></td><td
align=center><h5>Liturgical Date</h5></td></tr>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<td class=\"SJTdInfo\" align=\"center\">".$info['DTE'] .
"</td><td class=\"SJTdInfo\" align=\"left\"><a
href=/bulletin_files/".$info['BLTN_FILE'] ." title=\"Click to view or
download the file\" target=\"_blank\">".$info['LTRGCL_DTE']
.."</a></td>";
}
Print "</table></div></br></br>";
****
  Réponse avec citation
Vieux 05/01/2008, 03h36   #2
seastl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Retrieving formatted date in select query

On Fri, 04 Jan 2008 20:24:53 -0600, seastl <<nospam@nospam.com>>
wrote:

>
>Hello,
>
>I am a novice (very novice) webmaster, and am trying to retrieve the
>most recent record from a database to display on a homepage. It all
>works well enough with one exception, which I am sure is just my
>overlooking something - I cannot change the date format. When I change
>the field "DTE" to a date_format in the query as below, my date
>results are blank. Looking at the code below, can someone please
>suggest what I might be doing incorrectly? Any is greatly
>appreciated -
>
>****
>mysql_connect("localhost", "username", "password") or
>die(mysql_error());
>mysql_select_db("stjojone_primary") or die(mysql_error());
>$data = mysql_query("SELECT
>LTRGCL_DTE,date_format(DTE,'%W,%M,%D,%Y'),BLTN_FI LE FROM
>bulletin_store ORDER BY DTE DESC LIMIT 0,1") or die(mysql_error());
>Print "<div align=center><table class=\"SJTableInfo\">";
>Print "<tr><td align=center><h5>Date</h5></td><td
>align=center><h5>Liturgical Date</h5></td></tr>";
>while($info = mysql_fetch_array( $data ))
>{
>Print "<tr>";
>Print "<td class=\"SJTdInfo\" align=\"center\">".$info['DTE'] .
>"</td><td class=\"SJTdInfo\" align=\"left\"><a
>href=/bulletin_files/".$info['BLTN_FILE'] ." title=\"Click to view or
>download the file\" target=\"_blank\">".$info['LTRGCL_DTE']
>."</a></td>";
>}
>Print "</table></div></br></br>";
>****


Ok, I changed to "date_format(DTE,'%W,%M %D,%Y') as 'DTE',", and seems
to work perfectly now. Then I find that it sorts on the literal date,
not the actual date value now! Is there an easy enough fix for that,
or should one just sort on the ID autonumber column?
  Réponse avec citation
Vieux 05/01/2008, 03h53   #3
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Retrieving formatted date in select query

seastl < wrote:
> On Fri, 04 Jan 2008 20:24:53 -0600, seastl <<nospam@nospam.com>>
> wrote:
>
>> Hello,
>>
>> I am a novice (very novice) webmaster, and am trying to retrieve the
>> most recent record from a database to display on a homepage. It all
>> works well enough with one exception, which I am sure is just my
>> overlooking something - I cannot change the date format. When I change
>> the field "DTE" to a date_format in the query as below, my date
>> results are blank. Looking at the code below, can someone please
>> suggest what I might be doing incorrectly? Any is greatly
>> appreciated -
>>
>> ****
>> mysql_connect("localhost", "username", "password") or
>> die(mysql_error());
>> mysql_select_db("stjojone_primary") or die(mysql_error());
>> $data = mysql_query("SELECT
>> LTRGCL_DTE,date_format(DTE,'%W,%M,%D,%Y'),BLTN_FIL E FROM
>> bulletin_store ORDER BY DTE DESC LIMIT 0,1") or die(mysql_error());
>> Print "<div align=center><table class=\"SJTableInfo\">";
>> Print "<tr><td align=center><h5>Date</h5></td><td
>> align=center><h5>Liturgical Date</h5></td></tr>";
>> while($info = mysql_fetch_array( $data ))
>> {
>> Print "<tr>";
>> Print "<td class=\"SJTdInfo\" align=\"center\">".$info['DTE'] .
>> "</td><td class=\"SJTdInfo\" align=\"left\"><a
>> href=/bulletin_files/".$info['BLTN_FILE'] ." title=\"Click to view or
>> download the file\" target=\"_blank\">".$info['LTRGCL_DTE']
>> ."</a></td>";
>> }
>> Print "</table></div></br></br>";
>> ****

>
> Ok, I changed to "date_format(DTE,'%W,%M %D,%Y') as 'DTE',", and seems
> to work perfectly now. Then I find that it sorts on the literal date,
> not the actual date value now! Is there an easy enough fix for that,
> or should one just sort on the ID autonumber column?
>


Of course it does - you overrode the column name and used the results of
the call to date_format().

Just change it to date_format(DTE,'%W,%M %D,%Y') as DTE2. Still order
by DTE. Or, if you're not using the column name, just leave the AS DTE
off entirely (I don't know what you're doing with it afterwards).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 05/01/2008, 11h08   #4
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Retrieving formatted date in select query

Jerry Stuckle wrote:
> seastl < wrote:
>> On Fri, 04 Jan 2008 20:24:53 -0600, seastl <<nospam@nospam.com>>
>> wrote:
>>
>>> Hello,
>>>
>>> I am a novice (very novice) webmaster, and am trying to retrieve the
>>> most recent record from a database to display on a homepage. It all
>>> works well enough with one exception, which I am sure is just my
>>> overlooking something - I cannot change the date format. When I
>>> change the field "DTE" to a date_format in the query as below, my
>>> date results are blank. Looking at the code below, can someone
>>> please suggest what I might be doing incorrectly? Any is
>>> greatly appreciated -
>>>
>>> ****
>>> mysql_connect("localhost", "username", "password") or
>>> die(mysql_error());
>>> mysql_select_db("stjojone_primary") or die(mysql_error());
>>> $data = mysql_query("SELECT
>>> LTRGCL_DTE,date_format(DTE,'%W,%M,%D,%Y'),BLTN_FIL E FROM
>>> bulletin_store ORDER BY DTE DESC LIMIT 0,1") or die(mysql_error());
>>> Print "<div align=center><table class=\"SJTableInfo\">";
>>> Print "<tr><td align=center><h5>Date</h5></td><td
>>> align=center><h5>Liturgical Date</h5></td></tr>";
>>> while($info = mysql_fetch_array( $data ))
>>> {
>>> Print "<tr>";
>>> Print "<td class=\"SJTdInfo\" align=\"center\">".$info['DTE'] .
>>> "</td><td class=\"SJTdInfo\" align=\"left\"><a
>>> href=/bulletin_files/".$info['BLTN_FILE'] ." title=\"Click to view
>>> or download the file\" target=\"_blank\">".$info['LTRGCL_DTE']
>>> ."</a></td>";
>>> }
>>> Print "</table></div></br></br>";
>>> ****

>>
>> Ok, I changed to "date_format(DTE,'%W,%M %D,%Y') as 'DTE',", and
>> seems to work perfectly now. Then I find that it sorts on the
>> literal date, not the actual date value now! Is there an easy enough
>> fix for that, or should one just sort on the ID autonumber column?
>>

>
> Of course it does - you overrode the column name and used the results
> of the call to date_format().
>
> Just change it to date_format(DTE,'%W,%M %D,%Y') as DTE2. Still order
> by DTE. Or, if you're not using the column name, just leave the AS
> DTE off entirely (I don't know what you're doing with it afterwards).


Yes you do Jerry, he is using the associative part of mysql_fetch_array() to
access the value (guess he could save a bit of resource by using
mysql_fetch_assoc()).
Anyway, Jerry's first hint is the one to go for, coupled with changing
$info['DTE'] to $info['DTE2']


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 21h29.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13418 seconds with 12 queries