/    Sign up×
Community /Pin to ProfileBookmark

if/else statement cleanup. It works, but I think it could be much tidier.

Ok so I wanted to write some PHP that would display if a store was open or closed during its operation hours. My current code displays this: “The time is: hh:mm am/pm and we are currently open/closed.” Which is exactly what I want it to do, but it just seems overly complicated and messy to me. I just started learning PHP this week and am looking for a little insight to clean coding I guess.

Here is what I have:

[code=php]<?php

$date = new DateTime(date(‘h:i’));
$date -> modify(‘-25200 seconds’);
$day = date(‘w’);
$time = date(‘H’);
$hoursTueWedFri = array(16, 17, 18, 19, 20, 21, 22, 23);
$hoursThu = array(00, 16, 17, 18, 19, 20, 21, 22, 23);
$hoursSat = array(16, 17, 18, 19, 20, 21, 22);
//time is 7 hrs ahead

<p>The time is: <?php echo $date->format(‘g:i a’); ?> and we are currently

<?php

if ($day == 0) {
echo “closed”;
}

if ($day == 1) {
echo “closed”;
}

if ($time == $hoursTueWedFri) {
echo “open”;
}

elseif ($time == $hoursThu) {
echo “open”;
}

elseif ($time == $hoursSat) {
echo “open”;
}

else {
echo “closed”;
}

//Store Hours:
//Sunday – CLOSED
//Monday – CLOSED
//Tuesday 9:00 AM – 6:00 PM = 16 – 23
//Wednesday 9:00 AM – 6:00 PM = 16 – 23
//Thursday 9:00 AM – 7:00 PM = 16 – 00
//Friday 9:00 AM – 6:00 PM = 16 – 23
//Saturday 9:00 AM – 5:00 PM = 16 – 22

?>.</p>[/code]

You’ll notice that I modified the date for my time zone. This isn’t urgent or anything, I just want to try and learn how to clean my code up so I can get better!

Thoughts?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 29.2013 — I won't say this is the "best" way, but it's certainly different, and just one if. ?
[code=php]
<?php

date_default_timezone_set('America/New_York');
$storeHours = array(
array(), // Sunday
array(),
range(16, 23),
range(16, 23),
array(0, 16, 17, 18, 19, 20, 21, 22, 23),
range(16, 23),
range(16, 22)
);
$status = 'closed';
if(in_array(date('G'), $storeHours[date('w')])) {
$status = 'open';
}
echo "We are $status";
[/code]
Copy linkTweet thisAlerts:
@badhimAug 30.2013 — It's a good idea to start using some PHP Template Engine from the very beginning of your learning. It allows you to keep HTML and PHP code in separate files. HTML can look something like that:

[code=html]
<html>
<body>
The time is: {Time} and we are currently {Status}
</body>
</html>[/code]


And the PHP script like that:

[code=php]<?php
require_once("phemplate.class.php");

$tpl = new phemplate("__website_root_path__", "remove_nonjs");
$tpl->vars['main'] = file_get_contents("__filename_of_your_html_template__");

$tpl->set_var('Time', date("h:i", time()));
$tpl->set_var('Status', getStatus());

echo $tpl->process('out', 'main', true);
?>[/code]
Copy linkTweet thisAlerts:
@rootAug 30.2013 — Neat code NogDog, only thing I would change is the way you allocate the $status, something like this.
[code=php]$status = (in_array(date('G'), $storeHours[date('w')]))? "open" : "closed";[/code]
Copy linkTweet thisAlerts:
@NogDogAug 30.2013 — I work with some people who hate ternary operators, so I avoid them out of habit now.
Copy linkTweet thisAlerts:
@rootAug 31.2013 — IDK what is to hate about them, maybe they just don't understand them, especially when they are chained together...
Copy linkTweet thisAlerts:
@NogDogAug 31.2013 — IDK what is to hate about them, maybe they just don't understand them, especially when they are chained together...[/QUOTE]

It's purely the readability issue. When scanning through unfamiliar code to find a bug (which we often spend more time doing than actually creating code), it is easier for the eye to see the logic of a properly indented/bracketed if/else block than a one-liner using ternary operators. Sure: they understand the ternary syntax and how to read it, it just slows you down a bit (especially if viewing code in a terminal window or such that does not have syntax-highlighting). Of course, you can spread it out over multiple lines, which helps a bit:
[code=php]
$foo = ($something == $nothing)
? getSomeValue($ix)
: getSomeOtherValue($ix)
;
[/code]

I still sneak in the occasional simple one-liner, though, like:
[code=php]
$foo = isset($_GET['foo']) ? (int) $_GET['foo'] : 0;
[/code]

(If they can't deal with that, tough. ?
×

Success!

Help @FL4PJ4CKS spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...