Hi,
I was wanting to write some code to work out whether I needed st, nd rd or
th in my dates.
While the code below works, I am certain that there is a better way....
if (($daterep == "1") || ($daterep == "21") || ($daterep == "31")){
$letters = "st";
} elseif (($daterep == "2") || ($daterep == "22")){
$letters = "nd";
} elseif (($daterep == "3") || ($daterep == "23")){
$letters = "rd";
} else {
$letters = "th";
}
I tried:
if ($daterep == "1" || "21" || "31") { ... etc
but it did not work... I don't understand why though?
I cast $daterep into a string, but now I find that I could have left it
alone....
Any suggestions as to how to get this code working a bit smoother?
Robert 12 1685
*** Robert escribió/wrote (Sat, 18 Sep 2004 13:03:06 GMT): I was wanting to write some code to work out whether I needed st, nd rd or th in my dates. While the code below works, I am certain that there is a better way....
A switch may suit:
switch($daterep){
case '1':
case '21':
case '31':
$letters='st';
break;
case '2':
case '22':
$letters='nd';
break;
case '3':
case '23':
$letters='rd';
break;
default:
$letters='th';
}
if ($daterep == "1" || "21" || "31") { ... etc
but it did not work... I don't understand why though?
"21" is not a valid expression. It should be:
if( ($daterep == "1") || ($daterep == "21") || ($daterep == "31") ){
In any case, check the manual page for date(), you have format characters
that make what you want:
S
English ordinal suffix for the day of the month, 2 characters
st, nd, rd or th.
--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--
Thanks Alvaro,
The case method works well.
Also I didn't know about the S. It is definatley easy to use!
My text is missing it! I have found it on the php site now.
Robert
"Alvaro G. Vicario" <kA*****************@terra.es> wrote in message
news:6i*****************************@40tude.net... *** Robert escribió/wrote (Sat, 18 Sep 2004 13:03:06 GMT): I was wanting to write some code to work out whether I needed st, nd rd or th in my dates. While the code below works, I am certain that there is a better way....
A switch may suit:
switch($daterep){ case '1': case '21': case '31': $letters='st'; break; case '2': case '22': $letters='nd'; break; case '3': case '23': $letters='rd'; break; default: $letters='th'; }
if ($daterep == "1" || "21" || "31") { ... etc
but it did not work... I don't understand why though?
"21" is not a valid expression. It should be:
if( ($daterep == "1") || ($daterep == "21") || ($daterep == "31") ){ In any case, check the manual page for date(), you have format characters that make what you want:
S English ordinal suffix for the day of the month, 2 characters st, nd, rd or th.
-- -+ Álvaro G. Vicario - Burgos, Spain +- http://www.demogracia.com (la web de humor barnizada para la intemperie) ++ Las dudas informáticas recibidas por correo irán directas a la papelera -+ I'm not a free help desk, please don't e-mail me your questions --
Robert wrote: if (($daterep == "1") || ($daterep == "21") || ($daterep == "31")){ $letters = "st"; } elseif (($daterep == "2") || ($daterep == "22")){ $letters = "nd"; } elseif (($daterep == "3") || ($daterep == "23")){ $letters = "rd"; } else { $letters = "th"; }
Perhaps a lookup table:
$map[1] = 'st';
$map[21] = 'st';
$map[31] = 'st';
$map[2] = 'nd';
....
--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
Good Idea,
But then I'd have to put in an item for every day wouldn't I?
Say I wanted the leetters for the 15th.....
Robert
"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in message
news:10**********************@k26g2000oda.googlegr oups.com... Robert wrote: if (($daterep == "1") || ($daterep == "21") || ($daterep == "31")){ $letters = "st"; } elseif (($daterep == "2") || ($daterep == "22")){ $letters = "nd"; } elseif (($daterep == "3") || ($daterep == "23")){ $letters = "rd"; } else { $letters = "th"; }
Perhaps a lookup table: $map[1] = 'st'; $map[21] = 'st'; $map[31] = 'st'; $map[2] = 'nd'; ...
-- | Just another PHP saint | Email: rrjanbiah-at-Y!com
On Sat, 18 Sep 2004 15:19:36 +0200, "Alvaro G. Vicario"
<kA*****************@terra.es> wrote: if ($daterep == "1" || "21" || "31") { ... etc
but it did not work... I don't understand why though? "21" is not a valid expression. It should be:
"21" is certainly a valid expression, it's a non-empty, non-"0" string, which
in a Boolean context becomes bool(true).
if ($daterep == "1" || "21" || "31")
->
if ($daterep == true)
->
if ($daterep)
So if $daterep is non-zero and non-empty, the condition is always true.
if( ($daterep == "1") || ($daterep == "21") || ($daterep == "31") ){
Clearly this is what the OP meant to do, but "21" is still an expression.
--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
On Sat, 18 Sep 2004 13:03:06 GMT, "Robert" <lo*********@yahoo.com.au> wrote: I was wanting to write some code to work out whether I needed st, nd rd or th in my dates. While the code below works, I am certain that there is a better way....
if (($daterep == "1") || ($daterep == "21") || ($daterep == "31")){ $letters = "st"; } elseif (($daterep == "2") || ($daterep == "22")){ $letters = "nd"; } elseif (($daterep == "3") || ($daterep == "23")){ $letters = "rd"; } else { $letters = "th"; }
I tried:
if ($daterep == "1" || "21" || "31") { ... etc
but it did not work... I don't understand why though? I cast $daterep into a string, but now I find that I could have left it alone.... Any suggestions as to how to get this code working a bit smoother?
<?php
function ordinal($n)
{
$ordinalSuffix = array('th', 'st', 'nd', 'rd', 'th',
'th', 'th', 'th', 'th', 'th');
if ($n >= 10 && $n <= 19)
return $n . 'th';
else
return $n . $ordinalSuffix[$n % 10];
}
for ($i = 1; $i <= 31; $i++)
{
print ordinal($i) . '<br>';
}
?>
--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
*** Andy Hassall escribió/wrote (Sat, 18 Sep 2004 15:20:28 +0100): "21" is certainly a valid expression
Of course, you are right.
--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--
.oO(Robert) Good Idea,
But then I'd have to put in an item for every day wouldn't I?
Say I wanted the leetters for the 15th.....
With the modulo-operator you can get the last digit (mod 10):
$last = $value % 10;
For $value = 15 this would be 5. A lookup-table could look like this:
$map[0] = 'th';
$map[1] = 'st';
$map[2] = 'nd';
$map[3] = 'rd';
You would simply have to check if there's a key in that table for the
last digit, if not use the element with the key 0 ('th').
Only little problem are numbers ending on 11, 12 or 13, they need a
special treatment.
Another simple algorithm is described in http://en.wikipedia.org/wiki/How_to_...rdinal_numbers
A possible quick 'n dirty implementation, using both algorithms:
$lookup = array('th', 'st', 'nd', 'rd');
$letters = ($value / 10 % 10) != 1
? isset($lookup[$last = $value % 10]) ? $lookup[$last] : $lookup[0]
: $lookup[0];
This should work for all numbers, but if you only have to take care of
dates the date() function should be fine.
Micha
Robert wrote: Hi,
I was wanting to write some code to work out whether I needed st, nd rd or th in my dates. While the code below works, I am certain that there is a better way....
if (($daterep == "1") || ($daterep == "21") || ($daterep == "31")){ $letters = "st"; } elseif (($daterep == "2") || ($daterep == "22")){ $letters = "nd"; } elseif (($daterep == "3") || ($daterep == "23")){ $letters = "rd"; } else { $letters = "th"; }
I tried:
if ($daterep == "1" || "21" || "31") { ... etc
but it did not work... I don't understand why though? I cast $daterep into a string, but now I find that I could have left it alone.... Any suggestions as to how to get this code working a bit smoother?
Robert
A little math is in order:
$daterepl = $daterep % 10;
switch ($daterepl) {
case 1:
$letters="st";
break;
case 2:
$letters="nd";
break;
case 3:
$letters="rd";
break;
default:
$letters="th";
}
A nice side effect of this is that it will work for any (positive) integer.
On Sat, 18 Sep 2004 13:02:44 -0400, Robert Stearns <rs**********@charter.net>
wrote: A little math is in order: $daterepl = $daterep % 10; switch ($daterepl) { case 1: $letters="st"; break; case 2: $letters="nd"; break; case 3: $letters="rd"; break; default: $letters="th"; } A nice side effect of this is that it will work for any (positive) integer.
11st, 12nd, 13rd...
--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
"Robert" <lo*********@yahoo.com.au> wrote in message
news:<em*****************@news-server.bigpond.net.au>... I was wanting to write some code to work out whether I needed st, nd rd or th in my dates.
$date = strtotime('2004-09-22');
echo date('F jS', $date); // prints "September 22nd"
Cheers,
NC
Robert wrote: Hi,
I was wanting to write some code to work out whether I needed st, nd rd or th in my dates. While the code below works, I am certain that there is a better way....
if (($daterep == "1") || ($daterep == "21") || ($daterep == "31")){ $letters = "st"; } elseif (($daterep == "2") || ($daterep == "22")){ $letters = "nd"; } elseif (($daterep == "3") || ($daterep == "23")){ $letters = "rd"; } else { $letters = "th"; }
I tried:
if ($daterep == "1" || "21" || "31") { ... etc
but it did not work... I don't understand why though? I cast $daterep into a string, but now I find that I could have left it alone.... Any suggestions as to how to get this code working a bit smoother?
Robert
here's my 2cents worth:
function
numToOrdinal($num)
{
$n = $num % 100;
$suff = array("th", "st", "nd", "rd", "th"); // suff for suffix
$ord = $n<21?($n<4 ? $suff[$n]:$suff[0]): ($n%10>4 ? $suff[0] : $suff[$n%10]);
return $num . $ord;
}
for($i = 0; $i < 200; $i++)
{
echo numToOrdinal($i) . "<br>";
}
handles 0th and all positive integers.
[error checking should be implemented]
Fox
*************** This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andrea Griffini |
last post by:
I did it.
I proposed python as the main language for our next CAD/CAM
software because I think that it has all the potential needed
for it. I'm not sure yet if the decision will get through, but...
|
by: Michael |
last post by:
Hi,
I'm fairly new at Python, and have the following code that works but isn't
very concise, is there a better way of writing it?? It seems much more
lengthy than python code i have read..... :-)...
|
by: brian.digipimp |
last post by:
I turned this in for my programming fundamentals class for our second
exam. I am a c++ newb, this is my first class I've taken. I got a good
grade on this project I'm just wondering if there is a...
|
by: Jean-David Beyer |
last post by:
I have six hard drives (4 SCSI and 2 EIDE) on my main machine with parts
of a database on each drive. The main index is on one SCSI drive all to
itself. The main data are on the other three SCSI...
|
by: Christopher Benson-Manica |
last post by:
Of course one can get the effect with appropriate use of existing
operators, but a ^^ operator would make for nice symmetry (as well as
useful to me in something I'm working on). Am I the only one...
|
by: Fred Hebert |
last post by:
I am trying to learn VC.NET and convert some Borland C++ applications. The
syntax differences are killing me...
Is there an easy way to convert a hex string, entered by the user, to a
binary...
|
by: Rob R. Ainscough |
last post by:
I realize I'm learning web development and there is a STEEP learning curve,
but so far I've had to learn:
HTML
XML
JavaScript
ASP.NET using VB.NET
..NET Framework
ADO.NET
SSL
|
by: Christoph Zwerschke |
last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I
haven't found a satisfying answer yet: Why isn't there an "ordered
dictionary" class at least in the standard list? Time and again...
|
by: Tolga |
last post by:
As far as I know, Perl is known as "there are many ways to do
something" and Python is known as "there is only one way". Could you
please explain this? How is this possible and is it *really* a...
|
by: student |
last post by:
hi, C is an evergreen programming language developed by denis richie.
C++ is only an extension to C with some extra added features and VC++
provides an IDE for programming. still C is not obselete....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |