Solved

PHP Time / Date message evaluation

Posted on 2012-03-26
7
260 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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

948 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now