You are here: Re: Sort after adding leading zero to date? « PHP Programming Language « IT news, forums, messages
Re: Sort after adding leading zero to date?

Posted by Kimmo Laine on 12/08/06 06:59

"GarryJones" <morack@algonet.se> wrote in message
news:1165529640.544809.174390@l12g2000cwl.googlegroups.com...
>I have code numbers in 2 fields from a table which correspond to month
> and date.
>
> (Month, Code number)
> Field name = ml_mna
> 1
> 2
> 3
> etc up to 12
> (Data is entered without a leading zero)
>
>
> Field name = ml_dya
> (Date, Code number)
> 1
> 2
> 3
> etc up to 31
> (Data is entered without a leading zero)
>
> I can sucessfully add a leading zero if necessary to each number after
> retrieving with select using the following....
>
> $ml_mna=mysql_result($ml_upg,$i,"ml_mna");
> $ml_mna++;
> if (strlen($ml_mna) < 2) {
> $ml_mna = '0' .$ml_mna;
> }
>
> for the month and
>
> $ml_dya=mysql_result($ml_upg,$i,"ml_dya");
> $ml_dya++;
> if (strlen($ml_dya) < 2) {
> $ml_dya = '0' .$ml_dya;
> }
>
> for the day
>
> However, I need to display the data in date order.
>
> Using "order by" the dates are sorted before I add this leading zero
> and the result is an incorrect and confusing display

Well the problem might be in the database structure rather than how yo use
the date. If the numbers are sorted like you claim - 1,30,4 - it indicates
that the data type of the field is a textual format such as varchar rather
than an integer. What you should be doing is use a datetime data type field
to store the date instead of a couple of varchar fields and you'll have no
worries at all with ORDER BY.

And for simpler leading zero formatting, just use sprintf:
echo sprintf('%02d', $m1_dya);

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti pδivittyvδ nettisarjis
spam@outolempi.net | rot13(xvzzb@bhgbyrzcv.arg)

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация