So I have this code that works perfectly:
<?php
$time = date("H");
$timezone = date("e");
if ($time < "13") {
echo "message1";
} else
if ($time >= "13" && $time < "19") {
echo "message2";
} else
if ($time >= "19" && $time < "24") {
echo "message3";
}
?>
The problem is that I can only use hours: 1, 5, 10, 19 and etc.
I need to use minutes as well, eg. 13:30.
I have tried changing to 13:30. Also tried changing to 1350.
But can't make it work.
Need help.
Create a new variable that hold the current minute,
DateTime::format shows us that date("i") gives:
Minutes with leading zeros
With the current hour and minute, you can calculate any time, for example:
<?php
$hour = date("H");
$minute = date("i");
// Between 13:30 and 13:30
if ($hour === 13 && $minute <= 30) {
echo "message1";
}
// Between 13:30 and 19:00
elseif (($hour >= 13 && $minute >= 30) && $hour < 19) {
echo "message2";
}
// After 19:00
else {
echo "message3";
}
?>
Related
I want to run my code from 21:00 to 08:00, for example. But my code does not work properly because when the clock reaches 24:00 (00:00), my code gets confused and cannot set 01:00. check 00:00 because it is a 24-hour clock.
I need this code for the dark mode of the site, so that my site will be in dark mode between a desired hour, for example, from 10 pm to 6 am.
Note: I may specify any hour for this task and I do not choose just one hour. For example, I may choose 21:00 to 3:00 am. It is random because the hours are chosen by my clients and I do not know what hours they choose. !!!
Can you guide me to make my code work correctly in 24 hours if I specify any hour?
$hour = date('H');
//$hour = 23;
if( $hour > 22 && $hour < 8) {
echo "Good night";
}else{
echo "Good Morning";
}
You can use the modulo operator to handle the 24-hour clock.
$start_hour = 21;
$end_hour = 8;
$current_hour = date('H');
// handle the 24-hour cycle
$current_hour = $current_hour % 24;
if($current_hour >= $start_hour || $current_hour < $end_hour) {
echo "Good night";
} else {
echo "Good morning";
}
This way it will work for any hours range you choose, as long as the start hour is greater than or equal to the end hour.
For HH:MM:
$start_time = "21:00";
$end_time = "08:00";
$current_time = date('H:i');
$start_hour = (int)substr($start_time, 0, 2);
$start_minute = (int)substr($start_time, 3, 2);
$end_hour = (int)substr($end_time, 0, 2);
$end_minute = (int)substr($end_time, 3, 2);
$current_hour = (int)substr($current_time, 0, 2);
$current_minute = (int)substr($current_time, 3, 2);
// handle the 24-hour cycle
$current_hour = $current_hour % 24;
if(($current_hour > $start_hour || ($current_hour == $start_hour && $current_minute >= $start_minute)) || ($current_hour < $end_hour || ($current_hour == $end_hour && $current_minute < $end_minute))) {
echo "Good night";
} else {
echo "Good morning";
}
When you reverse the intervals (21:00 to 08:00 vs 08:00 to 21:00) you also need to reverse the logic:
If $start < $end, you need to check $current >= $start && $current <= $end
Otherwise, $current >= $start || $current <= $end
You'll also want to consider minutes and seconds as well. For that, you can pass a full properly formatted H:M:S string and convert everything to the same unit so comparisons make sense. It's easier to see if you create a dedicated functions that do only one thing:
// Error checking omitted for brevity
function hmsToSeconds(string $hms): int
{
[$h, $m, $s] = array_pad(preg_split('/[^\d]+/', $hms), 3, 0);
return 3600 * $h + 60 * $m + $s;
}
function inRange(string $start, string $end, string $current = null): bool
{
if ($current === null) {
$current = date('H:i:s');
}
$start = hmsToSeconds($start);
$end = hmsToSeconds($end);
$current = hmsToSeconds($current);
if ($start < $end) {
return $current >= $start && $current <= $end;
}
return $current >= $start || $current <= $end;
}
if (inRange('21:00:00', '8:00:00')) {
echo 'Good night';
} else {
echo 'Good morning';
}
Demo
Needless to say, since we're doing this on the server we're using server clock, so it may or may nor make sense for the user. If you know the user's time zone, you can take it into account when calculating current time.
The code below is meant to show my visitors something like "Good Morning, Today is ------ and the time is -----"
But I keep getting "Welcome" instead of the main greetings. Why is this?
<?php
$hour = date ("G");
$minute = date ("i");
$second = date ("s");
$msg = " Today is " . date ("l, M. d, Y.") . " And the time is " . date ("g:i a");
if ($hour = 00 && $hour <= 9 && $minute <= 59 && $second <= 59)
{ $greet = "Good Morning,"; }
else {
if ($hour >= 10 && $hour <= 11 && $minute <= 59 && $second <= 59)
{ $greet = "Good Day,"; }
if ($hour >= 12 && $hour <= 15 && $minute <= 59 && $second <= 59)
{ $greet = "Good Afternoon,"; }
if ($hour >= 16 && $hour <= 23 && $minute <= 59 && $second <= 59)
{ $greet = "Good Evening,"; }
else { $greet = "Welcome,"; }
}
echo $greet.$msg;
?>
Any idea on how to get this done?
How do I get timezones of my visitors automatically using php. My server's default timezone is Lagos/Africa. I want the code above to show my visitors their local time and date.
Your problem is here
if ($hour = 00 && $hour <= 9 && $minute <= 59 && $second <= 59)
this way you assign 00 to $hour and all the other condition fails
you need
if ($hour == 0 && $hour <= 9 && $minute <= 59 && $second <= 59)
solved the if problem there are others
You should keep in mind that php date() function return a string
there are several way for solve your code problem these are just some suggestion
so you should commpare string (not number) or convert the string in number for commparision using a proper cast
Looking to your if you can also avoid the check for minute ( are always <= 59) this mean that you code could be
<?php
$hour = date("G");
$minute = date("i");
$second = date("s");
$msg = " Today is " . date("l, M. d, Y.") . " And the time is " . date("g:i a");
if ( (int)$hour == 0 && (int)$hour <= 9 ) {
$greet = "Good Morning,";
} else if ( (int)$hour >= 10 && (int)$hour <= 11 ) {
$greet = "Good Day,";
} else if ( (int)$hour >= 12 && (int)$hour <= 15 ) {
$greet = "Good Afternoon,";
} else if ( (int)$hour >= 16 && (int)$hour <= 23 ) {
$greet = "Good Evening,";
} else {
$greet = "Welcome,";
}
echo $greet.$msg;
?>
try this....
<?php
$hour = date ("G");
$minute = date ("i");
$second = date ("s");
$msg = " Today is " . date ("l, M. d, Y.") . " And the time is " . date ("g:i a");
if ($hour == 00 && $hour <= 9 && $minute <= 59 && $second <= 59){
$greet = "Good Morning,";
}else if ($hour >= 10 && $hour <= 11 && $minute <= 59 && $second <= 59){
$greet = "Good Day,";
}else if ($hour >= 12 && $hour <= 15 && $minute <= 59 && $second <= 59){
$greet = "Good Afternoon,";
}else if ($hour >= 16 && $hour <= 23 && $minute <= 59 && $second <= 59){
$greet = "Good Evening,";
}else {
$greet = "Welcome,";
}
}
echo $greet.$msg;
?>
I recommend you read some tuts
I'm trying to make a script to display specific messages at specific day and time. I've got the script working to display at a specific day and at a specific time, but not the two combined.
How can I make the script display a specific message at mondays between 3pm and 4pm?
Look at the script I made so far;
<?php
//Messages
$afternoon = "It's afternoon";
$evening = "It's evening";
$night = "It's night.";
$morning = "It's morning!";
$friday = "Soon weekend!";
//Get time
$current_time = date(G);
//Get day
$current_day = date(l);
//12PM - 4PM
if ($current_time >= 12 && $current_time <= 16) {
echo $afternoon;
}
//5PM - 11PM
elseif ($current_time >= 17 && $current_time <= 24) {
echo $evening;
}
//00AM - 05AM
elseif ($current_time >= 1 && $current_time <= 5) {
echo $night;
}
//6AM - 11AM
elseif ($current_time >= 6 && $current_time <= 11) {
echo $morning;
}
//Friday, display message
if ($current_day == "Friday") {
echo $friday;
}
?>
I appreciate any help I can get!
if ($current_day == "Monday") {
if ($current_time >= 15 && $current_time <= 16) {
echo "It's Monday and the time is between 3PM and 4PM.";
}
}
if($current_day == "Monday" && $current_time >= 3 && $current_time <= 4){
//Monday text
}
Try this:
Set the flag true when its afternoon and later if the day is monday set the message as needed.
$afternoonFlag = False;
if ($current_time >= 12 && $current_time <= 16) {
echo $afternoon;
$afternoonFlag = True;
}
if ($current_day == "Monday" && $afternoonFlag) {
echo "Your Message";
}
I have this script that is working every day between 2 hours. Now, I have 2 questions:
1) how do I make it work only every friday, not all days?
2) how do I use minutes to m code? $hour:$min > 8:15 is not working
<?php
$hour = date('G');
if ($hour >= 8 && $hour <= 10) {
include('facut_mine2.php');
}
?>
Thanks!
use date('w') to get weekday
<?php
$start = strtotime('8:15');
$end = strtotime('9:45');
if(date('w') == 5){ // day 5 = Friday
$timenow = date('U');
if ($timenow >= $start && $timenow <= $end) {
include('facut_mine2.php');
}
}
?>
<?php
$hour = date('G');
$day = date('w');
if ($day == 5 && $hour >= 8 && $hour <= 10) {
include('facut_mine2.php');
}
?>
by using D parameter in date() function you can retrieve the current week day:
$today = date('D');// $today = 'Wed'
and for minute you can again use date() function with i
$current_min = date('i');
if(date('D') == 'Fri') {
$hourMin = date('H:i');
if ($hourMin >= '08:15' && $hourMin <= '10:00') {
include('facut_mine2.php');
}
}
Can anyone extrapolate on how to implement a basic "good evening" or "good morning" based on the user's time setting?
Perhaps PHP will fetch the server time, but I'm looking to greet the site visitor with a time-based appropriate greeting that considers their time of day.
E.G.: good morning, good night, good afternoon.
Base it on .getHours() of the date object. Using javascript's Date object will automatically use the user's local time, rather than the server-time:
var now = new Date();
alert( now.getHours() );
A couple conditional checks, and you're in business. For instance, the following is a very simple and easy-to-understand example:
var now = new Date();
var hrs = now.getHours();
var msg = "";
if (hrs > 0) msg = "Mornin' Sunshine!"; // REALLY early
if (hrs > 6) msg = "Good morning"; // After 6am
if (hrs > 12) msg = "Good afternoon"; // After 12pm
if (hrs > 17) msg = "Good evening"; // After 5pm
if (hrs > 22) msg = "Go to bed!"; // After 10pm
alert(msg);
It's currently 2:56am here, so I see "Mornin' Sunshine!" when I run this. You can test your own local time with this online demo: http://jsbin.com/aguyo3/edit
<?php
// I'm India so my timezone is Asia/Calcutta
date_default_timezone_set('Asia/Calcutta');
// 24-hour format of an hour without leading zeros (0 through 23)
$Hour = date('G');
if ( $Hour >= 5 && $Hour <= 11 ) {
echo "Good Morning";
} else if ( $Hour >= 12 && $Hour <= 18 ) {
echo "Good Afternoon";
} else if ( $Hour >= 19 || $Hour <= 4 ) {
echo "Good Evening";
}
?>
For more information about date see function.date. I Hope this will help.
<?php
/* This sets the $time variable to the current hour in the 24 hour clock format */
$time = date("H");
/* Set the $timezone variable to become the current timezone */
$timezone = date("e");
/* If the time is less than 1200 hours, show good morning */
if ($time < "12") {
echo "Good morning";
} else
/* If the time is grater than or equal to 1200 hours, but less than 1700 hours, so good afternoon */
if ($time >= "12" && $time < "17") {
echo "Good afternoon";
} else
/* Should the time be between or equal to 1700 and 1900 hours, show good evening */
if ($time >= "17" && $time < "19") {
echo "Good evening";
} else
/* Finally, show good night if the time is greater than or equal to 1900 hours */
if ($time >= "19") {
echo "Good night";
}
?>
$hour = date('H');
if ($hour >= 20) {
$greetings = "Good Night";
} elseif ($hour > 17) {
$greetings = "Good Evening";
} elseif ($hour > 11) {
$greetings = "Good Afternoon";
} elseif ($hour < 12) {
$greetings = "Good Morning";
}
echo $greetings;
const now = new Date().getHours();
switch (true) {
case (now<=12): console.log("Good Morning");break;
case (now>12 && now<16): console.log("Good Afternnon");break;
case (now>=16 && now<20): console.log("Good Evening");break;
case (now>=20 && now<=24): console.log("Good Night");break;}
This code is based on JavaScript, Hope this will help you.
function greeting_msg() {
$hour = date('H');
if ($hour >= 18) {
$greeting = "Good Evening";
} elseif ($hour >= 12) {
$greeting = "Good Afternoon";
} elseif ($hour < 12) {
$greeting = "Good Morning";
}
return $greeting;
}