Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

PHP Time / Date message evaluation

Hello ... Im trying to implement a status message based on the day of the week and time of day

ie:

if Sunday -> display "We are closed"
if Saturday between the hours of 0800 and 1800 -> display "We are open"
if Mon-Fri between the hours of 0800 and 1800 -> display "We are open"
else display "We are closed"

It works fine when i use either the day of week or the time of day, but it is giving odd results when i try to implement the two together.

heres what i have so far
$current_day = date(l);

if ($current_day == "Sunday") {
echo "Closed";
}
elseif ($current_day == "Saturday" && date('H') > 8 && date("H") < 19) {
echo "Open";

elseif ($current_day != "Saturday" || $current_day != "Sunday" || date("H") < 8) {
echo "Closed";

elseif ($current_day != "Saturday" || $current_day != "Sunday" && date("H") > 18) {
echo "Closed"
}

Open in new window


it seems like its hitting the day evaluation and stopping there.

Any suggestions?
0
nriddock
Asked:
nriddock
1 Solution
 
jrm213jrm213Commented:
The parameter of the date function is a string, you need quotes around your l.

$current_day = date("l");

Open in new window

0
 
Patrick TallaricoCommented:
It looks like you are missing your closing braces for the elseif statements


$current_day = date(l);

if ($current_day == "Sunday") {
echo "Closed";
}
elseif ($current_day == "Saturday" && date('H') > 8 && date("H") < 19) {
echo "Open";
}
elseif ($current_day != "Saturday" || $current_day != "Sunday" || date("H") < 8) {
echo "Closed";
}
elseif ($current_day != "Saturday" || $current_day != "Sunday" && date("H") > 18) {
echo "Closed"
}

You could also try...

if (date("H") > 8 && date("H"<18){
$status = "Open";
}else{
$status = "Closed";
}
if(date("l")=="Sunday"){
$status = "Closed";
}
echo $status;
0
 
Ray PaseurCommented:
Have a look at this article.  I'll try to show you how I might code this.  (Actually I would probably use a data base, but maybe there is a straight PHP solution).
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Ray PaseurCommented:
Maybe like this: http://www.laprbass.com/RAY_temp_nriddock.php
<?php // RAY_temp_nriddock.php
error_reporting(E_ALL);
echo "<pre>";

// PROBLEM DEFINITION
/*
if Sunday -> display "We are closed"
if Saturday between the hours of 0800 and 1800 -> display "We are open"
if Mon-Fri between the hours of 0800 and 1800 -> display "We are open"
else display "We are closed"
*/


// REQUIRED PHP 5.1+
date_default_timezone_set('America/Chicago');


// FUNCTION TO SAY IF WE ARE OPEN
function areWeOpen($datetime='Now')
{
    // TABLE OF HOURS OF OPERATION
    static
    $hop
    = array
    ( 'Sun' => FALSE
    , 'Mon' => array ('a' => '0800', 'z' => '1800')
    , 'Tue' => array ('a' => '0800', 'z' => '1800')
    , 'Wed' => array ('a' => '0800', 'z' => '1800')
    , 'Thu' => array ('a' => '0800', 'z' => '1800')
    , 'Fri' => array ('a' => '0800', 'z' => '1800')
    , 'Sat' => array ('a' => '0800', 'z' => '1500') // CHANGED THIS ON PURPOSE FOR THE DEMO
    )
    ;

    // CONVERT FUNCTION ARGUMENT TO A UNIX TIMESTAMP OR FAIL ON ERROR
    if (!$ts = strtotime($datetime))
    {
        error_log("Bogus input to AreWeOpen($datetime)");
        return 'We have no idea if we are open or not!';
    }

    $day = date('D', $ts);
    $now = date('Hi', $ts);
    if ($hop[$day])
    {
        $hrs = $hop[$day];

        // LOOK FOR $now BETWEEN OPENING AND CLOSING
        if ($now >= $hrs['a'])
        {
            if ($now <= $hrs['z'])
            {
                return "We are open";
            }
        }
    }

    // NOT OPEN
    return "We are closed";
}


// A FUNCTION TO TEST THE FUNCTION
function testAreWeOpen($x='Now')
{
    echo PHP_EOL . $x . ' ' . AreWeOpen($x);
}


// RUN SOME TESTS TO SEE IF IT WORKS
testareweopen();
testareweopen('Sunday');
testareweopen('Saturday 3:00pm');
testareweopen('Saturday 3:01pm');
testareweopen('Tuesday 2:00pm');
testareweopen('Yesterday 1:35pm');
testareweopen('Yesterday 1:35am');
testareweopen('Tomorrow 1:35pm');
testareweopen('Tomorrow 1:35am');
testareweopen('Now+84000 seconds');
testareweopen('May 15, 2012 10:00am');
testareweopen('Foobar');

Open in new window

HTH, ~Ray
0
 
nriddockAuthor Commented:
Works great! Thanks
0
 
Ray PaseurCommented:
Thanks for the points - it's a great question, ~Ray
0
 
nriddockAuthor Commented:
@Ray_Paseur

Quick question ... our on-call support number changes day by day and that info is stored in a database.

how would i echo $var_ContactInfo using the above function

ie: testareweopen('Tomorrow 9:35pm') echo $var_ContactInfo
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now