Solved

PHP Time / Date message evaluation

Posted on 2012-03-26
7
267 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 110

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
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!

 
LVL 110

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 110

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Php - How to get the value of json file using PHP? 29 58
Make checkboxes execute queries on click 8 37
How do I remove "" from json_encode 5 22
JSON decode 5 21
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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