William.John William.John - 2 years ago 115
PHP Question

How to echo the information depending on what day it is? PHP

I am very new to php and I am trying to get out the info on one particular day. So if it is monday I will echo "its monday" and I have started with a code below. I have read that you get the day by the number 0-6 if iam not misstaken: 1 = monday and 6 = saturday.

<?php
$day = strftime ('%A', time ());
if ($day == ("1") )
{
echo "Its monday";
}

else if ($day == ("6"))
{
echo "Its saturday";
}
else
{
echo "Not saturday or monday";
}

?>


UPDATED:

<?php
$day = strftime ('%A', time ());
if ($day == ("1") )
{
echo "It's " . date('l');
}

else if ($day == ("6"))
{
echo "It's " . date('l');
}
else
{
echo "It's " . date('l');
}

?>

Answer Source

Read the manual on http://php.net/manual/en/function.strftime.php

%A A full textual representation of the day Sunday through Saturday

You're checking for an integer rather than a string using %A.

If you want to use numbers for representation, use %e

%e Day of the month, with a space preceding single digits. Not implemented as described on Windows. See below for more information. 1 to 31

$day = strftime ('%e', time ());
if ($day == ("7")  )
{
echo "Its monday";
}

else if ($day == ("6"))
{
echo "Its saturday";
}
else
{
echo "Not saturday or monday";
}

Otherwise, use %A and check for a string and use a string ("Monday"):

$day = strftime ('%A', time ());
if ($day == ("Monday")  )
{
echo "Its monday";
}

else if ($day == ("Saturday"))
{
echo "Its saturday";
}
else
{
echo "Not saturday or monday";
}

Foonotes:

Be careful though, Monday and monday are not treated the same and they are case-sensitive. Same thing goes for every other days of the week if you are going to use strings.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download