Skip to content

Instantly share code, notes, and snippets.

@fredbradley
Created April 11, 2013 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredbradley/5365402 to your computer and use it in GitHub Desktop.
Save fredbradley/5365402 to your computer and use it in GitHub Desktop.
Can anyone spot any errors with the code below? I need it to display an output that matches with this document from the client (see screenshot here: https://twitter.com/fredbradley/status/322399388146216961/photo/1 )
<?php
/*
* FOR THE PHP GEEKS!
------------------
Can anyone spot any errors with the code below?
I need it to display an output that matches with this document from the client (see screenshot here: https://twitter.com/fredbradley/status/322399388146216961/photo/1 )
I'm 99.9% that it works as needed. But alas I can't triple check it without staying up for every hour in a whole week!
Any questions, or comments please get in touch via email: hello@fredbradley.co.uk
Many thanks,
Fred
<Start>
/* ====================================================================
SET A COUPLE OF DEFINED PHRASES SO YOU DON'T HAVE TO TYPE TOO MUCH!
==================================================================== */
define("PLAY_NOW", "You can play right now!");
define("NEXT_CHANCE", "Your next chance to play is just after");
function nextPlay() {
date_default_timezone_set('Europe/London');
if (date("w") > "0" && date("w") < "7") {
// If it's Mon, Tue, Wed, Thu, Fri or Sat
if (date("Hi") < "0901") {
$output = NEXT_CHANCE." 9am";
} elseif (date("Hi") > "0901" && date("Hi") < "0941") {
$output = PLAY_NOW;
} elseif (date("Hi") > "0940" && date("Hi") < "1002") {
$output = NEXT_CHANCE." 10am";
}
for( $i=10; $i<16; $i++ ) {
// A cheeky for loop to save on the amount of writing IF statements you have to do!
if (date("Hi") > $i."01" && date("Hi") < $i."41") {
$output = PLAY_NOW;
} elseif (date("Hi") > $i."40" && date("Hi") < ($i+1)."02") {
$output = NEXT_CHANCE." ".nextPlay_simplifytime($i+1);
}
}
if (date("Hi") > "1541") {
echo NEXT_CHANCE." 9am tomorrow!";
}
}
if (date("w") == "6" && date("Hi") > "1240") {
// If it's Saturday and it's after 1240... (NB: This overrides the above)
$output = NEXT_CHANCE." 9am on Monday!";
}
if (date("w") == "0") {
// If it's Sunday... (NB: This overrides the above)
$output = NEXT_CHANCE." 9am tomorrow!";
}
return $output;
}
function nextPlay_simplifytime($i) {
if ($i > 11) {
// If the hour is midday or after
$m = "pm";
if ($i > 12) {
// If it's 1pm or later then deduct 12 to get the 'PM' time. (eg. 15 minus 12 equals 3)
$i = $i-12;
}
} else {
// Otherwise it must be morning!
$m = "am";
}
// Return giving me the hour (in 12 hour format, with AM/PM)
return $i.$m;
}
echo nextPlay();
/* Thanks for helping! :)
<End> */
?>
@samstarling
Copy link

If you made the function signature to be:

function nextPlay($day, $hour)

And swapped ‛date('w')‛ for ‛$day‛, then you could call it manually and see the result it gave. And to get the current value, you'd just call the function with those two ‛date‛ variables.

@fredbradley
Copy link
Author

Good suggestion! Thanks Sam! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment