<?php
//get time of day
$time = date('H');
//convert time to integer
settype($time , "integer");
//set Am or Pm variable
$amPm = '';
//convert time from 24 hour to 12 hour time
if ($time >= 1 && $time <= 12) {
$amPm = 'am';
} elseif ($time >= 13 && $time <= 24) {
$time -= 12;
$amPm = 'pm';
}
// Add am or pm to define morning or afternoon
$timeOfDay = $time . $amPm;
//randomly set number of cups of coffee
$cupsOfCoffee = rand(0, 5);
//set variable for alertness
$alertness = '';
//set up switch for cups of coffee to states of alertness
switch ($cupsOfCoffee) {
case 0:
$alertness = 'Zero cups of coffee?, I don\'t think you\'re actually awake.';
break;
case 1:
$alertness = 'One cup of coffee, that is anough to wake up but you might want to grab another one before you go out in public';
break;
case 2:
$alertness = 'Two cups of coffee, now you\'re ready to put some pants on';
break;
case 3:
$alertness = 'Three cups of coffee, finally enough caffeine to fully wake up';
break;
case 4:
$alertness = 'Four cups of coffee? you might want to ease up there you don\'t want to get too jittery';
break;
case 5:
$alertness = 'Five cups of coffee, you\'re vibrating so fast you look fuzzy.';
break;
default:
$alertness = 'An unknown amount of coffee.';
break;
}
//set number of coffee cups to zero if you should be sleeping
if ($time <= 6 && $amPm == 'am') {
$cupsOfCoffee = 0;
}
// add variable to output message
$message = '';
// combine values into message
if ($time <= 6 && $amPm == 'am') {
$message = "It's $timeOfDay, \n why on earth are you awake at such an unholy hour? \n";
} elseif ($time >= 7 && $time <= 11 && $amPm == 'am') {
$message = "Good morning Justin it's $timeOfDay, \n you've had $alertness \n";
} elseif ($time >= 1 && $time <= 12 && $amPm == 'pm') {
$message = "Good afternoon Justin it's $timeOfDay, \n you've had $alertness \n";
} else {
$message = "Get to bed scallywag! it's $timeOfDay \n you probably shouldn't have had $alertness \n";
}
echo $message;
?>
This is a suggested coffee intake snippet. Using if else statements, switch statements and logic operators.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.