Solved

PHP Time / Date message evaluation

Posted on 2012-03-26
7
257 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
htacces issue 2 41
Wordpress syntax error 1 29
.htaccess file settings 4 37
writting html or javascript in PHP 8 14
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

744 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

12 Experts available now in Live!

Get 1:1 Help Now