+ Reply to Thread
Results 1 to 6 of 6

Thread: PHP Time and day display

  1. #1
    inductinve soul is offline x10Hosting Member inductinve soul is an unknown quantity at this point
    Join Date
    Mar 2010
    Location
    Ogden, UT, 84414
    Posts
    4

    Red face PHP Time and day display

    <?php
    $d=date("D"); if ($d=="Fri") echo "Today is Friday,";
    elseif ($d=="Sat") echo "Today is Saturday,";
    elseif ($d=="Sun") echo "Today is Sunday,";
    elseif ($d=="Mon") echo "Today is Monday,";
    elseif ($d=="Tue") echo "Today is Tuesday,";
    elseif ($d=="Wed") echo "Today is Wednesday,";
    elseif ($d=="Thu") echo "Today is Thursday,";
    else echo "How maddening the script has a error!";
    ?> <?php echo date("F j, Y, g:i a"); ?>

    Now the script works... but it would be nice if I didn't have two PHP scripts running. I am actually quite embarrassed that I could not figure out how to combine the two after half an hour of playing around, so any help would be appreciated..

  2. #2
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jul 2005
    Location
    Behind a keyboard.
    Posts
    8,925

    Re: PHP Time and day display

    Quote Originally Posted by inductinve soul View Post
    <?php
    $d=date("D"); if ($d=="Fri") echo "Today is Friday,";
    elseif ($d=="Sat") echo "Today is Saturday,";
    elseif ($d=="Sun") echo "Today is Sunday,";
    elseif ($d=="Mon") echo "Today is Monday,";
    elseif ($d=="Tue") echo "Today is Tuesday,";
    elseif ($d=="Wed") echo "Today is Wednesday,";
    elseif ($d=="Thu") echo "Today is Thursday,";
    else echo "How maddening the script has a error!";
    ?> <?php echo date("F j, Y, g:i a"); ?>

    Now the script works... but it would be nice if I didn't have two PHP scripts running. I am actually quite embarrassed that I could not figure out how to combine the two after half an hour of playing around, so any help would be appreciated..
    Code:
    <?php
    $d=date("D"); if ($d=="Fri") echo "Today is Friday,";
    elseif ($d=="Sat") echo "Today is Saturday,";
    elseif ($d=="Sun") echo "Today is Sunday,";
    elseif ($d=="Mon") echo "Today is Monday,";
    elseif ($d=="Tue") echo "Today is Tuesday,";
    elseif ($d=="Wed") echo "Today is Wednesday,";
    elseif ($d=="Thu") echo "Today is Thursday,";
    else echo "How maddening the script has a error!";
    
    echo date("F j, Y, g:i a"); ?>
    All I did was take out the closing ?> and the new <?php tag near the end; if you start it with <?php anything in the middle is processed as php until you put ?>, so if there's more than one script, put them all in the same <?php ?> brackets.


    Edit: Just to clarify though, my opinion is if it works, don't mess with it. I don't think there's much of a performance loss by having too much info.
    Last edited by Livewire; 03-18-2010 at 04:49 AM.


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  3. #3
    misson is offline Community Advocate misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,391

    Re: PHP Time and day display

    Why not simply:
    PHP Code:
    echo strftime('Today is %A, %B %e, %Y, %l:%M %p'); 
    or:
    PHP Code:
    echo 'Today is 'date('l, F j, Y, g:i a'); 
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    xgreenberetx is offline x10Hosting Member xgreenberetx is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    57

    Re: PHP Time and day display

    How bout using the php "case" to execute the different days

  5. #5
    inductinve soul is offline x10Hosting Member inductinve soul is an unknown quantity at this point
    Join Date
    Mar 2010
    Location
    Ogden, UT, 84414
    Posts
    4

    Re: PHP Time and day display

    Thank you so much! The reason I have it set up the way I do is so I can put custom messages in for each day as I want to. as for simplifying it... I like the date format like that so it shows more details.

  6. #6
    misson is offline Community Advocate misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,391

    Re: PHP Time and day display

    You can also use arrays for custom messages or switch statements as xgreenberetx suggests.

    PHP Code:
    $dayMessages = array(
        
    'Monday' => 'Born',
        
    'Tuesday' => 'Christened',
        
    'Wednesday' => 'Married',
        
    'Thursday' => 'Took ill',
        
    'Friday' => 'Grew worse',
        
    'Saturday' => 'Died',
        
    'Sunday' => 'Buried',
    );

    $day date('l');
    echo 
    $dayMessages[$day], ' on '$day'.'
    Arrays are more performant than a sequence of "if" statements; switch statements are clearer as to purpose and potentially more performant.
    Last edited by misson; 03-18-2010 at 07:06 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. MySQL system time and Apache time are different
    By midnighthearts in forum Free Hosting
    Replies: 6
    Last Post: 11-04-2009, 08:19 PM
  2. Service dont work from time to time
    By lassiter in forum Free Hosting
    Replies: 1
    Last Post: 06-30-2009, 01:07 AM
  3. Display IP
    By ryansenara in forum Programming Help
    Replies: 2
    Last Post: 01-02-2008, 03:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers