PHP Time and day display

inductinve soul

New Member
Messages
4
Reaction score
0
Points
0
<?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..
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
<?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:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Why not simply:
PHP:
echo strftime('Today is %A, %B %e, %Y, %l:%M %p');
or:
PHP:
echo 'Today is ', date('l, F j, Y, g:i a');
 

inductinve soul

New Member
Messages
4
Reaction score
0
Points
0
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.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
You can also use arrays for custom messages or switch statements as xgreenberetx suggests.

PHP:
$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:
Top