Page 1 of 1

A little help needed with simple PHP code

Posted: Fri Sep 12, 2008 3:54 am
by kdidymus
Good morning. Long time no type. I hope you're all well.

Since I last posted to this forum (a long time ago now), I've managed to get my website up and running. http://www.didymus.org.uk/ is where you'll find it.

I need a little help with some simple code. Basically, I need to know if there's a way of condensing this code down a little. In particular the
if ( $date == "**" )
lines as there are 31 of them!

You can see the code in action at http://www.didymus.org.uk/date.php

Anway, here's my code:

Code: Select all

<?php
/* Program: date.php
 * Desc:    Displays date and time and custom poem.
 */
  // SET VARIABLES FOR DISPLAY
  $day = date ("l");
  $date = date ("j");
  $month = date ("F");
  $year = date ("Y");
  $hour = date ("H");
  $mins = date ("i");
  $secs = date ("s");
if ( $date == "1" )
{ $suffix = "st" ;}
if ( $date == "2" )
{ $suffix = "nd" ;}
if ( $date == "3" )
{ $suffix = "rd" ;}
if ( $date == "4" )
{ $suffix = "th" ;}
if ( $date == "5" )
{ $suffix = "th" ;}
if ( $date == "6" )
{ $suffix = "th" ;}
if ( $date == "7" )
{ $suffix = "th" ;}
if ( $date == "8" )
{ $suffix = "th" ;}
if ( $date == "9" )
{ $suffix = "th" ;}
if ( $date == "10" )
{ $suffix = "th" ;}
if ( $date == "11" )
{ $suffix = "th" ;}
if ( $date == "12" )
{ $suffix = "th" ;}
if ( $date == "13" )
{ $suffix = "th" ;}
if ( $date == "14" )
{ $suffix = "th" ;}
if ( $date == "15" )
{ $suffix = "th" ;}
if ( $date == "16" )
{ $suffix = "th" ;}
if ( $date == "17" )
{ $suffix = "th" ;}
if ( $date == "18" )
{ $suffix = "th" ;}
if ( $date == "19" )
{ $suffix = "th" ;}
if ( $date == "20" )
{ $suffix = "th" ;}
if ( $date == "21" )
{ $suffix = "st" ;}
if ( $date == "22" )
{ $suffix = "nd" ;}
if ( $date == "23" )
{ $suffix = "rd" ;}
if ( $date == "24" )
{ $suffix = "th" ;}
if ( $date == "25" )
{ $suffix = "th" ;}
if ( $date == "26" )
{ $suffix = "th" ;}
if ( $date == "27" )
{ $suffix = "th" ;}
if ( $date == "28" )
{ $suffix = "th" ;}
if ( $date == "29" )
{ $suffix = "th" ;}
if ( $date == "30" )
{ $suffix = "th" ;}
if ( $date == "31" )
{ $suffix = "st" ;}
echo "<p align='center'><font size='2' face='Arial'>$day $date$suffix $month $year | $hour:$mins:$secs<br><br>Monday's Child Poem";
if ( $day == "Monday" )
{ echo "<p align='center'><font size='2' face='Arial'><font color='#FF0000'>Monday's child is fair of 
face,</font><br>
Tuesday's child is full of grace,<br>
Wednesday's child is full of woe,<br>
Thursday's child has far to go,<br>
Friday's child is loving and giving,<br>
Saturday's child works hard for his living,<br>
And the child that is born on the Sabbath day<br>
Is bonny and blithe, and good and gay.</font></p>";}
if ( $day == "Tuesday" )
{ echo "<p><font size='2' face='Arial'>Monday's child is fair of face,<br>
<font color='#FF0000'>Tuesday's child is full of grace,</font><br>
Wednesday's child is full of woe,<br>
Thursday's child has far to go,<br>
Friday's child is loving and giving,<br>
Saturday's child works hard for his living,<br>
And the child that is born on the Sabbath day<br>
Is bonny and blithe, and good and gay.</font></p>";}
if ( $day == "Wednesday" )
{ echo "<p><font size='2' face='Arial'>Monday's child is fair of face,<br>
Tuesday's child is full of grace,<br>
<font color='#FF0000'>Wednesday's child is full of woe,</font><br>
Thursday's child has far to go,<br>
Friday's child is loving and giving,<br>
Saturday's child works hard for his living,<br>
And the child that is born on the Sabbath day<br>
Is bonny and blithe, and good and gay.</font></p>";}
if ( $day == "Thursday" )
{ echo "<p><font size='2' face='Arial'>Monday's child is fair of face,<br>
Tuesday's child is full of grace,<br>
Wednesday's child is full of woe,<br>
<font color='#FF0000'>Thursday's child has far to go,</font><br>
Friday's child is loving and giving,<br>
Saturday's child works hard for his living,<br>
And the child that is born on the Sabbath day<br>
Is bonny and blithe, and good and gay.</font></p>";}
if ( $day == "Friday" )
{ echo "<p><font size='2' face='Arial'>Monday's child is fair of face,<br>
Tuesday's child is full of grace,<br>
Wednesday's child is full of woe,<br>
Thursday's child has far to go,<br>
<font color='#FF0000'>Friday's child is loving and giving,</font><br>
Saturday's child works hard for his living,<br>
And the child that is born on the Sabbath day<br>
Is bonny and blithe, and good and gay.</font></p>";}
if ( $day == "Saturday" )
{ echo "<p><font size='2' face='Arial'>Monday's child is fair of face,<br>
Tuesday's child is full of grace,<br>
Wednesday's child is full of woe,<br>
Thursday's child has far to go,<br>
Friday's child is loving and giving,<br>
<font color='#FF0000'>Saturday's child works hard for his living,</font><br>
And the child that is born on the Sabbath day<br>
Is bonny and blithe, and good and gay.</font></p>";}
if ( $day == "Sunday" )
{ echo "<p><font size='2' face='Arial'>Monday's child is fair of face,<br>
Tuesday's child is full of grace,<br>
Wednesday's child is full of woe,<br>
Thursday's child has far to go,<br>
Friday's child is loving and giving,<br>
Saturday's child works hard for his living,<br>
<font color='#FF0000'>And the child that is born on the Sabbath day<br>
Is bonny and blithe, and good and gay.</font></font></p>";}
?>
Any help would be greatly appreciated.

KD.

Re: A little help needed with simple PHP code

Posted: Fri Sep 12, 2008 4:36 am
by Stryks
I feel like I must be missing something .... but wouldn't a better way to get the suffix be

Code: Select all

$suffix = date("S");
I haven't tested that, but it should work I think.

Re: A little help needed with simple PHP code

Posted: Fri Sep 12, 2008 4:40 am
by idevlin
Eeep that's long!

Not sure about Stryk's suggestion, he knows more PHP than I do os go check that!

If not:
You could check the last digit in the number and set the suffix based on that?
e.g.

Code: Select all

if (lastdigit==1)
   suffix="st"
else if (lastdigit==2)
   suffix="nd"
else if (lastdigit==3)
   suffix="rd"
else
   suffix="th"

Re: A little help needed with simple PHP code

Posted: Fri Sep 12, 2008 8:23 am
by kdidymus
Stryks and Idevlin - thank you both.

As it turns out, Stryks was absolutely right. Wish I'd seen THAT in my PHP manual this morning!

Code: Select all

$suffix = date ("S");
This returns the English ordinal suffix for the day. However, as I am on a tight learning curve, the lastdigit expression is very useful too.

KD.

Re: A little help needed with simple PHP code

Posted: Fri Sep 12, 2008 5:42 pm
by Stryks
Yeah, that is actually a nice way of thinking around the problem idevlin.

If I hadn't of been thinking about getting the suffix from date(), I probably wouldn't have arrived at that.

That's why I love this place ... so many approaches / solutions to the same problem. It's great to see how other people see your problem with fresh eyes.

Cheers