Solved

PHP Time / Date message evaluation

Posted on 2012-03-26
7
263 Views
Last Modified: 2012-03-28
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
Comment
Question by:nriddock
7 Comments
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 37768124
The parameter of the date function is a string, you need quotes around your l.

$current_day = date("l");

Open in new window

0
 
LVL 6

Expert Comment

by:Patrick Tallarico
ID: 37768154
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37768668
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37768789
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
 

Author Closing Comment

by:nriddock
ID: 37777197
Works great! Thanks
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37777463
Thanks for the points - it's a great question, ~Ray
0
 

Author Comment

by:nriddock
ID: 37777486
@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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP and google maps 13 45
ignore other .htaccess 2 45
PHP strings vs array errors 13 19
Doing something wrong with this PDO Select Statement... 3 10
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question