Forum Moderators: coopster

Message Too Old, No Replies

Return Ordinal Suffixes with the date function

Test my code in PHP5, Webhosts all use PHP4

         

Hester

12:34 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd be grateful if someone could test the following code in PHP5 as I do not have access to that, only version 4. (Amazing the amount of webhosts still running that!)

It is only a short piece of code.


<html>
<body>
<form>
<label>Date: </label>
<select>
<?php
while ($a < 100) {
$a++;
$a2 = mktime(1, 1, 1, 1, $a, 2005);
$a3 = date(S,$a2);
echo <<<HTML
<option value="$a">$a$a3</option>
HTML;
}
?>
</select>
</form>
</body>
</html>

Let me know what you see for numbers in the drop-down menu over 31. Do they have the correct suffix or not? I am seeing incorrect suffixes used for numbers over 31.

Why use numbers later than 31? Because they might be useful for database text such as streets. So instead of "42nd Street" you could just store "42". But PHP is unable to add the correct suffix.

I checked the string functions instead of date/time but couldn't find anything in there. If anyone knows of a way round this problem, let me know! Otherwise I will file a bug report.

JAB Creations

12:44 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hester, download XAMPP!
[apachefriends.org...]

It's pretty nifty, you can run files right off your computer (as it turns your box in to an actual server) and configuartion is minimal. I especially enjoy the XAMPP control which can be minimized to the tray and run in the background as a single proccess.

Oh and yeah, it uses PHP (5.05 if I'm correct).

Let me know how it works out for yah...

John

Hester

2:56 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have seen that before. Looks great! But surely when they say it is for XP, they mean Professional, not Home version? Alas XP Home can't run as a server - or am I wrong?! I cannot find anything that mentions which version of XP it will run on. Can it run on XP Home?

Let me know anyone!

coopster

3:11 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can run your own web server, database server, etc. on any version of Windows, including XP Home version.

Back to your question though, there is nothing PHP5-specific about your code there, why do you want to test it on PHP5? You haven't initialized your $a variable with anything for starters there ...

coopster

3:23 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Ah, I think I know what you are looking for here. You are trying to append the 2 character English ordinal suffix for the day of the month, but you are doing so thinking that it is going to be 32nd, 33rd, etc. What you aren't considering here is that "32nd" is actually the "1st" of February, "33rd" is the "2nd", etc. That is why your ordinal suffixes aren't matching correctly.

Make sense? Try this, you'll see exactly what I mean:

<?php 
$a = 0;
$list = '';
while ($a < 100) {
$a++;
$a2 = mktime(1, 1, 1, 1, $a, 2005);
$a3 = date('jS',$a2);
$list .= <<<HTML
<option value="$a">$a3</option>
HTML;
}
?>
<html>
<body>
<form>
<label>Date: </label>
<select><?php print $list;?></select>
</form>
</body>
</html>

Hester

11:55 am on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can run your own web server, database server, etc. on any version of Windows, including XP Home version.

You mean Microsoft have conned me all these years? I honestly thought only XP Professional could run as a server! I'm sure certain programs won't run on Home...

Back to your question though, there is nothing PHP5-specific about your code there, why do you want to test it on PHP5?

Well... because the PHP bug report screen only lists the latest versions of 4 and 5. If you try and get round this it refuses to accept the bug report, saying to upgrade first. Since I am unable to do this right now, I just wondered if the 'bug' had been fixed in later versions. OK, so perhaps in hindsight it's not really a bug!

So how do I go about solving the problem then? How to add the correct suffix for numbers after 31? Or can it not be done?

Maybe it should be a feature request.

coopster

5:39 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Maybe I should have been more specific. I know for certain you can run certain server processes on Win OS, for example, Apache and MySQL without issue.

Back to your issue though, ...


So how do I go about solving the problem then? How to add the correct suffix for numbers after 31? Or can it not be done?

Sure, it can be done, but not using dates like you are trying to. Have a closer look at the code I posted and you'll see what is happening. If you just want to use English ordinal suffixes on a number range you'll have to use a different solution as opposed to running it through the date() function. Is this what you are really after?

Hester

9:22 am on Nov 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure, it can be done, but not using dates like you are trying to... you'll have to use a different solution as opposed to running it through the date() function. Is this what you are really after?

Exactly. If I can't use dates, how can it be done? If it's a lenghty function, forget it. I was just after a ready-made solution from within PHP.

dreamcatcher

10:18 am on Nov 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Amazing the amount of webhosts still running that!

Its mainly because scripts that use some of the PHP5 syntax would not work on servers running PHP4. Most shared servers run version 4, for 5 you would probably have to get a dedicated server.

dc

coopster

6:49 pm on Nov 1, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Nothing ready-made that I am aware. Fairly easy though, have a look at this and see if it meets your needs.
function getEnglishOrdinalSuffix($n) 
{
$nn = $n % 100;
if ($nn > 3 && $nn < 21) {
return $n . 'th';
}
switch ($nn % 10) {
case '1': return $n . 'st';
case '2': return $n . 'nd';
case '3': return $n . 'rd';
default: return $n . 'th';
}
}
$list = '';
for ($n = 1; $n < 150; $n++) {
$list .= "$n: " . getEnglishOrdinalSuffix($n) . "\n";
}
print "<pre>$list</pre>";

Hester

9:42 am on Nov 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep, that does the trick. Thanks!