Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Switch Case

  1. #1
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts

    Switch Case

    I have a switch statement and its probably not possible to do what I want to do but I thought I'd ask...

    I have 2 similar cases (date and datetime)
    so if its date or datetime I want to show the date, then after that only put the time if its datetime.

    Now if I wanted the time to go first, it would be easy... but the time has to go at the end not the beginning.

  • #2
    New to the CF scene
    Join Date
    Jun 2007
    Location
    NW Indiana, United States, Earth
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are your date and datetime values in the UNIX timestamp format?

  • #3
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    PHP Code:
    switch ($var_name)
    {
        case 
    'date':
        case 
    'datetime':
           
    // display date

          
    if ( $var_name == 'datetime' 
          {
             
    // display time
          
    }

          break;


  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Wouldn't just

    PHP Code:
    //display date (regardless)
    if($var_name == 'datetime' 
       {
        
    // display time
       

    work?

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    Must hve been late, didnt think of just stick and 'if' in there. Still seems so inelegant
    Quote Originally Posted by whizard View Post
    Wouldn't just

    PHP Code:
    //display date (regardless)
    if($var_name == 'datetime' 
       {
        
    // display time
       

    work?

    Dan
    Date and datetime are not the only cases, so no.

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Inelegant shouldn't be more important than "it works"

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    I don't get it .. what's wrong with this ??
    PHP Code:
    <?php
    switch($var){
        case 
    'date':      echo $date; break
        case 
    'datetime' : echo $date
                          echo 
    $time; break;
    }
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    I'm assuming it's more complex than just outputting date & time. I use this technique often when 2 (or more) cases share a good bit of code. For example, when building output, several cases may output data in the same format with just the variable information changing.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •