Help With Elseif Please?

  • Joe Bloggs
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Nov 27, 2009
  • Posts: 7
  • Status: Offline

Post December 9th, 2009, 11:55 am

OK, I've been trying to work out how elseif works. Read lots of tutes but here’s where I’ve got to and hopefully this will work.

Lets say that each day of the week I want to place a notice on my home page about when UK Markets take place:

The notices would be as follows:

  • Mon = It Is London Market Day Today!
  • Tue = The next weekly market in UK is tomorrow in Glasgow!
  • Wed = It Is Glasgow Market Day Today!
  • Thu = The next weekly market in UK is tomorrow in Cardiff!
  • Fri = It Is Cardiff Market Day today and Leeds Market Day tomorrow!
  • Sat = It Is Leeds Market Day Today!
  • Sun = The next weekly market in UK is tomorrow in London!

What I need to know is - would this code actually do that?


Code: [ Select ]
<?php
  $d=date("D");
  if ($d=="Mon")
   echo "It Is London Market Day Today!";
  elseif ($d=="Tue")
   echo "The next weekly market in UK is tomorrow in Glasgow!";
  elseif ($d=="Wed")
   echo "It Is Glasgow Market Day Today!";
  elseif ($d=="Thu")
   echo "The next weekly market in UK is tomorrow in Cardiff!";
  elseif ($d=="Fri")
   echo "It Is Cardiff Market Day today and Leeds Market Day tomorrow!";
  elseif ($d=="Sat")
   echo "It Is Leeds Market Day Today!";
  elseif ($d=="Sun")
   echo "The next weekly market in UK is tomorrow in London!";
  ?>
  1. <?php
  2.   $d=date("D");
  3.   if ($d=="Mon")
  4.    echo "It Is London Market Day Today!";
  5.   elseif ($d=="Tue")
  6.    echo "The next weekly market in UK is tomorrow in Glasgow!";
  7.   elseif ($d=="Wed")
  8.    echo "It Is Glasgow Market Day Today!";
  9.   elseif ($d=="Thu")
  10.    echo "The next weekly market in UK is tomorrow in Cardiff!";
  11.   elseif ($d=="Fri")
  12.    echo "It Is Cardiff Market Day today and Leeds Market Day tomorrow!";
  13.   elseif ($d=="Sat")
  14.    echo "It Is Leeds Market Day Today!";
  15.   elseif ($d=="Sun")
  16.    echo "The next weekly market in UK is tomorrow in London!";
  17.   ?>


If that works - how about this:

Let’s say I want to create a new notice every hour. I tried this code and it definitely does not work:

Code: [ Select ]
<?php
  $d=h:i A ("h:i A");
  if ($d>="06:00")
   echo "It Is Between 6 and 7";
  elseif ($d>="07:00")
   echo "It Is Between 7 and 8";
  elseif ($d>="08:00")
   echo "It Is Between 8 and 9";
  elseif ($d>="09:00")
   echo "It Is Between 9 and 10";
else echo “What time is it?”;
  ?>
  1. <?php
  2.   $d=h:i A ("h:i A");
  3.   if ($d>="06:00")
  4.    echo "It Is Between 6 and 7";
  5.   elseif ($d>="07:00")
  6.    echo "It Is Between 7 and 8";
  7.   elseif ($d>="08:00")
  8.    echo "It Is Between 8 and 9";
  9.   elseif ($d>="09:00")
  10.    echo "It Is Between 9 and 10";
  11. else echo “What time is it?”;
  12.   ?>


What am I doing wrong?... apart from being dumb.

Thanks
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post December 9th, 2009, 11:55 am

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Joined: May 22, 2004
  • Posts: 3415
  • Loc: Richland, WA
  • Status: Offline

Post December 9th, 2009, 12:12 pm

Your first code should work but I might change it up and use a switch statement instead.

The first problem I see with your second set of code is how your finding the current hour.

Code: [ Select ]
$d=h:i A ("h:i A");


First of all your missing the date() function. Also if your just looking for the current hour I would just use G - 24 hour format, or g - 12 hour format. If your also planning on checking the minutes, I would store the minutes in a separate variable and keep the time format out of your logic.
#define NULL (::rand() % 2)
  • Joe Bloggs
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Nov 27, 2009
  • Posts: 7
  • Status: Offline

Post December 9th, 2009, 1:50 pm

Thanks.. got me on the right track...

Code: [ Select ]
<?php
  $d=date("G");
  if ($d=="18")
   echo "It Is Between 6 and 7";
  elseif ($d=="19")
   echo "It Is Between 7 and 8";
  elseif ($d=="20")
   echo "It Is Between 8 and 9";
  elseif ($d=="21")
   echo "It Is Between 9 and 10";
       elseif ($d=="22")
   echo "It Is Between 10 and 11";
else echo "What time is it?";
  ?>
  1. <?php
  2.   $d=date("G");
  3.   if ($d=="18")
  4.    echo "It Is Between 6 and 7";
  5.   elseif ($d=="19")
  6.    echo "It Is Between 7 and 8";
  7.   elseif ($d=="20")
  8.    echo "It Is Between 8 and 9";
  9.   elseif ($d=="21")
  10.    echo "It Is Between 9 and 10";
  11.        elseif ($d=="22")
  12.    echo "It Is Between 10 and 11";
  13. else echo "What time is it?";
  14.   ?>


I’ll take a look at the switch statement tomorrow... I’m thirsty and it’s beer time.

Cheers

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 119 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.