I'm Write code for greeting message using php. and its correct. but not changing messages. only one message shown "Good morning". there is another way to write code for greeting message or. can i changes in my code and how to do? please help.
<?php
function greeting_msg()
{
$hour = date('h');
if ($hour >= 20) {
echo "Good Night!";
} elseif ($hour > 17) {
echo "Good Evening!";
} elseif ($hour > 11) {
echo "Good Afternoon!";
} elseif ($hour < 12) {
echo "Good Morning!";
}
}
?>
Related to the documentation of the PHP date function, your code is not correct.
date('H'); // 24 hours with leading zeros
date('h'); // 12 hours with leading zeros
date('G'); // 24 without leading zeros
So your code is ending up with values up to twelve and it 's correct, that the 'Good Morning!' message is displayed.
Next you have to check the value of your $hour variable. Just dump the value with code looking something like this ...
echo "<pre>";
var_dump($hour);
echo "</pre>";
As long as the PHP date() function is returning strings as a result of the given format, you can run into several problems.
date('h'); // could be '03' ('03' != 3)
date('H'); // could also be '03' ('03' != 3)
date('G'); // results into '3' (3 == 3)
Then look at your if/else conditions. Does the expected value fit in your conditions? Guess the value is 20.
The value 20 would fit your first three conditions. The first condition is executed and so you 'll get the expected result 'Good night!'.
Just test your code this way.
First you need to change H instead of h to get 24 hour i.e
date('H'); // 24 hours
date('h'); // 12 hours
then you need to add few more condtions to get correct messages
$hour = date('H');
if ($hour > 5 && $hour < 10) {
echo 'Good Morning';
} elseif ($hour > 11 && $hour < 17) {
echo 'Good Afternoon';
} elseif ($hour > 17 && $hour < 20) {
echo 'Good Evening';
} else {
echo 'Good Night';
}
die;
I've made small testing of your code and here is what we have
function greeting_msg($hour)
{
$hour = $hour ?: date('H');
if ($hour >= 20) {
return "Good Night!";
} elseif ($hour > 17) {
return "Good Evening!";
} elseif ($hour > 11) {
return "Good Afternoon!";
} elseif ($hour < 12) {
return "Good Morning!";
}
}
$hoursRange = range(1, 24); // contain range between 1 to 24
foreach ($hoursRange as $range) {
echo sprintf('For %d message is: %s <br>', $range, greeting_msg($range));
}
Output in your case is
For 1 message is: Good Morning!
For 2 message is: Good Morning!
For 3 message is: Good Morning!
For 4 message is: Good Morning!
For 5 message is: Good Morning!
For 6 message is: Good Morning!
For 7 message is: Good Morning!
For 8 message is: Good Morning!
For 9 message is: Good Morning!
For 10 message is: Good Morning!
For 11 message is: Good Morning!
For 12 message is: Good Afternoon!
For 13 message is: Good Afternoon!
For 14 message is: Good Afternoon!
For 15 message is: Good Afternoon!
For 16 message is: Good Afternoon!
For 17 message is: Good Afternoon!
For 18 message is: Good Evening!
For 19 message is: Good Evening!
For 20 message is: Good Night!
For 21 message is: Good Night!
For 22 message is: Good Night!
For 23 message is: Good Night!
For 24 message is: Good Night!
Which mean that your system is saying "Good morning" starting from 01:00, which isn't true. I've made some minor changed to your conditions:
function greeting_msg()
{
$hour = date('H');
if ($hour >= 20 || $hour < 5) {
return "Good Night!";
} elseif ($hour > 17) {
return "Good Evening!";
} elseif ($hour > 11) {
return "Good Afternoon!";
} else {
return "Good Morning!";
}
}
I don't know is 05:00 is Morning for somebody, but I've set it :) And now range from 20:00 to 05:00 is showing "Good Night".
Also, don't forget that server time can have different timezone from yours.
The difference between date('h') and date('H') is that one is using the 24-hour format (H) and one is using the 12-hour format (h). So, when you do date('h'), you will always get values from 0-12.
From the date manual,
| Format | Description | Values |
|--------|--------------------------------------------------|---------------|
| h | 12-hour format of an hour with leading zeros | 01 through 12 |
| H | 24-hour format of an hour with leading zeros | 00 through 23 |
This means that the behavior you are describing is correct, as date('h') will be no more than 12, and you are checking for date('h') < 12, a condition that will always be true.
Using date('H'), and some minor adjustments is shown below (we echo the result of the function, not inside it, and some logical changes to simplify readability).
function greeting_msg() {
$hour = date('H');
if ($hour > 20 || $hour < 5) {
return "Good night";
} elseif ($hour > 17) {
return "Good evening";
} elseif ($hour > 12) {
return "Good afternoon";
} else {
return "Good morning";
}
}
echo greeting_msg();
Test of all hours where we mimic the 24 hours at https://3v4l.org/B8qC0
PHP.net on date()
public function greeting_msg() {
date_default_timezone_set('Europe/Brussels');
$hour = date('H');
if ($hour >= 04 && $hour < 11) {
$greeting = "Goedenmorgen";
} elseif ($hour == 11) {
$greeting = "Goedemiddag";
} elseif ($hour >= 11 && $hour <= 17) {
$greeting = "Goedemiddag";
} elseif ($hour >= 17 && $hour <= 23) {
$greeting = "Goedenavond";
} else {
$greeting = "Goedennacht";
}
return $greeting;
}
Related
Whenever I put in the number 12 or 16, for example, it will not say "It's time for lunch" or "It's time for dinner." It will always say "It's time for breakfast"
I've tried putting quotes around the numbers, I've tried looking at my greater than, less than, etc. signs, and it looks like that is not the problem i think..
<?php
$time = 4;
if(($time >= 4) || ($time <= 11))
{
echo "It's time for breakfast";
}
elseif(($time >= 12) || ($time <= 15))
{
echo "It's time for lunch";
}
else
{
echo "It's time for dinner";
}
?>
I want it to run so when I make 4 - 11 in $time it will echo "It's time for breakfast." I want from 12 - 15 in $time to echo "It's time for lunch." All other numbers should print It's time for dinner.
You need to change your conditions. Right now, you're asking:
"If the time is greater than or equal to 4 OR time is less than or equal to 15..."
It will always be true if the time is greater than or equal to 4 OR less than or equal to 15 so the else if will never be reached. You must change to AND so both conditions must be met to stay in the if block.
<?php
$time = 16;
if(($time >= 4) && ($time <= 11))
{
echo "It's time for breakfast";
}
elseif(($time >= 12) && ($time <= 15))
{
echo "It's time for lunch";
}
else
{
echo "It's time for dinner";
}
?>
Its easy to get lost in many if ifelse else conditions. A simple way would I like to think about them is to think of them systematically, like this for example:
$time = 4;
if ( $time >= 15 || $time < 4 ) {
echo "It's time for dinner";
} elseif( $time >= 12 ) {
echo "It's time for lunch";
} elseif ( $time >= 4 ) {
echo "It's time for breakfast";
}
I am writing a small php program that greets according to the time of day on the server. The rules are as follows:
Between 3:00:00 AM and 11:59:59 AM say Good morning!.
Between 12:00:00 PM and 4:59:59 PM say Good afternoon!.
Between 5:00:00 PM and 2:59:59 AM say Good evening!.
First, to simply matters, I decide to use military time. My program is as follows:
function greetingWord()
$hour = date("G");
if($hour >= 15 && $hour < 24)
{
echo "<p>Good Morning. Today is: </p>";
} else if($hour >= 12 && $hour < 17)
{
echo "<p>Good afternoon. Today is: </p>";
}
else if($hour >= 17 && $hour < 3)
{
echo "<p>Good evening. Today is: </p>";
}
}
my question regards that last else if. I suspect there is something wrong with my logic there so I'd apperciate it if someone would help me out with that final condition, the case of it being between 5pm and 2:59am.
Thank you!
DB
I think the easiest way to do it is leaving the last condition with a simple "else", so:
function greetingWord(){
$hour = date("G");
if($hour > 0 && $hour < 24){
if($hour >= 3 && $hour < 12)
{
echo "<p>Good Morning. Today is: </p>";
}else if($hour >= 12 && $hour < 17){
echo "<p>Good afternoon. Today is: </p>";
}else{
echo "<p>Good evening. Today is: </p>";
}
}
}
Hope it works.
I used ternary operators to display a part of day with a small modification of the code.
function greetingWord(int $hour): string
{
return ($hour >= 3 && $hour < 12)
? "Morning: "
: (($hour >= 12 && $hour < 17) ? "afternoon: " : "evening:");
}
foreach(range(0, 23) as $hour) {
echo "<p> Good " . greetingWord($hour) . " Today is: ". $hour ."</p>\n";
}
//or
echo "<p> Good " . greetingWord((int) date('G')) . " Today is: ". $hour ."</p>\n";
I am trying to get the minutes to work to echo "open" or "closed" depending on time. It works if i do not add the minutes, but not when adding minutes.
<?php
date_default_timezone_set('America/New_York');
$hour = (int) date('H:i');
if ($hour >= 1215 && $hour <=1735) {
// between 8:15am and 5:35pm
echo "Open";
} else {
echo "Closed";
}
?>
Given a time, for example 18:55, your date('H:i') will output a string, 18:55 when using the format H:i. If you try to cast this to an integer, like you are with (int), it will evaluate just the first number, 18.
Simply remove the colon in your date() function! This will return a string 1855 from the date, which can be cast to an integer of that value. Although, it's not strictly needed to cast it to an integer, as PHP will treat a string of numbers as an integer.
So basically you just need
$hour = date("Hi");
if ($hour >= 1215 && $hour <= 1735) {
// between 12:15 and 17:35
}
final code for future learners. thanks Qirel
<?php
date_default_timezone_set('America/New_York');
$hour = (int) date('Hi');
if ($hour >= 0815 && $hour <= 1735) {
// between 8:15am and 5:35pm
echo "Open";
} else {
echo "Closed";
}
?>
How can I insert a specific minute into the following to show a message:
if ($current_day == "Monday") {
if ($current_time >= 15 && $current_time <= 16) {
echo "message";
}
}
For example I want "message" to show at 3:45 or 15:45 and not between the 15th and 16th hour.
I figured out how to do it with javascript:
// Sunday is 0 Monday is 1 and so on
// January is 0 February is 1 and so on
var Digital=new Date()
var month=Digital.getUTCMonth()
var day=Digital.getUTCDate()
var year=Digital.getUTCFullYear()
var hours=Digital.getUTCHours()
var minutes=Digital.getUTCMinutes()
if (month==2&&day==16&&year==2015&&hours==01&&minutes==30 || hours==01&&minutes==45 || hours==01&&minutes==47)
document.write('<b>message here</b>')
else if (month==2&&day==17&&year==2015&&hours==01&&minutes==30 || hours==01&&minutes==45 || hours==01&&minutes==47)
document.write('<b>message 2 here</b>')
else
document.write('<b></b>')
Works perfectly no matter what timezone you're in. There's probably a less crazy way to do this but this is what I have so far.
In order to show a message at exactly 3:45 AM and again at 3:45 PM, you could try this:
$current_day = date('l');
$current_time = date('G:i');
if ($current_day == "Monday") {
if ($current_time == '03:45' || $current_time == '15:45') {
echo "message";
}
}
Or a little cleaner way (especially if you wanted to include a longer list of times):
if (date('l') == "Monday") {
if (in_array(date('G:i'), array('03:45','15:45')) {
echo "message";
}
}
EDIT: To specify a timezone, try inserting this line before either of the above pieces of code:
date_default_timezone_set('America/Los_Angeles');
Where 'America/Los_Angeles' could be any supported string representing your desired timezone from this page of the PHP manual: http://php.net/manual/en/timezones.php
I have asked this question before and accepted the answer but now I found that the php version on our server is 5.2 and DateTime::diff() is not working on that.
I want to calculate person's age in months plus days using date of birth and a given date.
Date Format Input: Y-m-d (example: 1986-08-23)
Output:
5 months and 20 days old.
150 months and 4 days old.
285 months and 30 days old.
Thanks
Here's a solution that'll accurately determine the number of months and number of days, including leap years. It assumes that things like July 21 to August 21 is 1 month 0 days, not 1 month 1 day, and that March 21 to April 20 is 0 months 30 days, not 1 month 0 days. The latter in both cases is what occurs when you just do a straight divide by 30 to calculate months.
I'm sure there's a better way to optimize the function, but it gets the job done:
function diff_date($start_date, $end_date) {
list($start_year, $start_month, $start_day) = explode('-', $start_date);
list($end_year, $end_month, $end_day) = explode('-', $end_date);
$month_diff = $end_month - $start_month;
$day_diff = $end_day - $start_day;
$months = $month_diff + ($end_year - $start_year) * 12;
$days = 0;
if ($day_diff > 0) {
$days = $day_diff;
}
else if ($day_diff < 0) {
$days = $end_day;
$months--;
if ($month_diff > 0) {
$days += 30 - $start_day;
if (in_array($start_month, array(1, 3, 5, 7, 8, 10, 12))) {
$days++;
}
else if ($start_month == 2) {
if (($start_year % 4 == 0 && $start_year % 100 != 0) || $start_year % 400 == 0) {
$days--;
}
else {
$days -= 2;
}
}
if (in_array($end_month - 1, array(1, 3, 5, 7, 8, 10, 12))) {
$days++;
}
else if ($end_month - 1 == 2) {
if (($end_year % 4 == 0 && $end_year % 100 != 0) || $end_year % 400 == 0) {
$days--;
}
else {
$days -= 2;
}
}
}
}
return array($months, $days);
}
list($months, $days) = diff_date('1984-05-26', '2010-04-29');
print $months . ' months and ' . $days . ' days old.';
Output:
314 months and 3 days old.
Edit: I tried to get rid of redundancy in the code, and forgot to rename a variable. This function will now work correctly for diff_date('2010-06-29', '2011-07-01').
Edit: Now correctly works for end months occurring after months with 31 or 28/29 days.
Use your favorite date parsing function (strtotime, strptime, mktime) to get an UNIX timestamp out of the date, then the interval ($now - $then)... and then work out how many seconds there are in a month and use that to calculate how many months the person has lived (division and remainder are your friends).
This'll give you a mathematically precise value that should be close enough to real life too.