Jump to content

Date/Time is the store open or closed?


manvaril

Recommended Posts

Ok so I got this page and it has different times on it of when we are open and closed, I'm trying to make some code that will tell the person viewing the page if we are opened or closed but there are different times for different days of the week. I have figured out how to do a switch from open to close based on the time but I need to figure out the times based on what day of the week it is. My code is below if someone can add to it that would be great.

<?php
date_default_timezone_set("US/Central");
$time = time();
$get_open_timestamp = mktime(11, 0, 0, date("m"), date("d"), date("Y"));
$get_close_timestamp = mktime(23, 0, 0, date("m"), date("d"), date("Y"));

if ($time >= $get_open_timestamp && $time <= $get_close_timestamp) {
echo "Open<br />";
} else {
echo "Closed<br />";
}
?>
Sunday-Thursday 11 AM - 11 PM<br />
Friday and Saturday 11AM - 12 AM

Link to comment
https://forums.phpfreaks.com/topic/208170-datetime-is-the-store-open-or-closed/
Share on other sites

Thanks here is what I came up with, it seems to work but I'll have to do some more testing with it, feel free to try it out but it's ugly lol

 

<?php
/* how to use:

Paste code into file and then call with:
get_open("11", "23", "11", "23", "11", "23", "11", "23", "11", "23", "11", "0", "11", "0", $open_close);
echo $open_close . "<br />";

Replace the numbers with the 24 hour time of opening and closing.
*/

function get_open($sun_open, $sun_close, $mon_open, $mon_close, $tue_open, $tue_close, $wed_open, $wed_close, $thur_open, $thur_close, $fri_open, $fri_close, $sat_open, $sat_close, &$result) {
        $time = time();
        date_default_timezone_set("US/Central");
        $dayofweek = date("w", $time);

        if ($dayofweek == 0) {
                /* Sun */
                $get_open_timestamp = mktime($sun_open, 0, 0, date("m"), date("d"), date("Y"));
                $get_close_timestamp = mktime($sun_close, 0, 0, date("m"), date("d"), date("Y"));
                if ($time >= $get_open_timestamp && $time <= $get_close_timestamp) {
                        $result = "Open";
                } else {
                        $result = "Closed";
                }
        } elseif ($dayofweek == 1) {
                /* Mon */
                $get_open_timestamp = mktime($mon_open, 0, 0, date("m"), date("d"), date("Y"));
                $get_close_timestamp = mktime($mon_close, 0, 0, date("m"), date("d"), date("Y"));
                if ($time >= $get_open_timestamp && $time <= $get_close_timestamp) {
                        $result = "Open";
                } else {
                        $result = "Closed";
                }
        } elseif ($dayofweek == 2) {
                /* Tues */
                $get_open_timestamp = mktime($tue_open, 0, 0, date("m"), date("d"), date("Y"));
                $get_close_timestamp = mktime($tue_close, 0, 0, date("m"), date("d"), date("Y"));
                if ($time >= $get_open_timestamp && $time <= $get_close_timestamp) {
                        $result = "Open";
                } else {
                        $result = "Closed";
                }
        } elseif ($dayofweek == 3) {
                /* Wed */
                $get_open_timestamp = mktime($wed_open, 0, 0, date("m"), date("d"), date("Y"));
                $get_close_timestamp = mktime($wed_close, 0, 0, date("m"), date("d"), date("Y"));
                if ($time >= $get_open_timestamp && $time <= $get_close_timestamp) {
                        $result = "Open";
                } else {
                        $result = "Closed";
                }
        } elseif ($dayofweek == 4) {
                /* Thur */
                $get_open_timestamp = mktime($thur_open, 0, 0, date("m"), date("d"), date("Y"));
                $get_close_timestamp = mktime($thur_close, 0, 0, date("m"), date("d"), date("Y"));
                if ($time >= $get_open_timestamp && $time <= $get_close_timestamp) {
                        $result = "Open";
                } else {
                        $result = "Closed";
                }
        } elseif ($dayofweek == 5) {
                /* Fri */
                $get_open_timestamp = mktime($fri_open, 0, 0, date("m"), date("d"), date("Y"));
                $get_close_timestamp = mktime($fri_close, 0, 0, date("m"), date("d"), date("Y"));
                if ($time >= $get_open_timestamp && $time <= $get_close_timestamp) {
                        $result = "Open";
                } else {
                        $result = "Closed";
                }
        } elseif ($dayofweek == 6) {
                /* Sat */
                $get_open_timestamp = mktime($sat_open, 0, 0, date("m"), date("d"), date("Y"));
                $get_close_timestamp = mktime($sat_close, 0, 0, date("m"), date("d"), date("Y"));
                if ($time >= $get_open_timestamp && $time <= $get_close_timestamp) {
                        $result = "Open";
                } else {
                        $result = "Closed";
                }
        }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.