hide message only during the set time - php

I am trying to hide a message between 2 particular timings and rest of the timings it should display.
the timings are
07:30am to 10:30am
and
18:30pm to 00:00am midnight
currently i did the following code which works ok for 07:30am to 10:30am it does the job but now how do i combine the 18:30pm to 00:00am midnight??? please someone help me out with some good logic.
<?php
date_default_timezone_set('America/New_York');
$now = new DateTime();
$fromTime = new DateTime();
$fromTime->setTime(07,30);
$toTime = new DateTime();
$toTime->setTime(10,30);
$fromTime1 = new DateTime();
$fromTime1->setTime(18,30);
$toTime1 = new DateTime();
$toTime1->setTime(00,00);
if ( $now < $fromTime || $now > $toTime) {
?>
This is a test message
<?php
}
?>
I tried doing like this
$now < $fromTime || $now > $toTime && $now < $fromTime1 || $now > $toTime1
but it messes up and displays the message even during 07:30am.
Thanks for your help

It's prety simple, to check few dates interval, but, don't make mistakes with PHP logical operators (https://www.php.net/manual/en/language.operators.logical.php) :
if ( ($now >= $fromTime && $now <= $toTime) // If it's on the first interval
|| // Or
($now >= $fromTime1 && $now <= $toTime1) // If it's on the second one
) {
echo 'Appear on the interval'; // I display the message.
}
Also, you've a mistake with your last variable $toTime1, you must to check from 18::00 to 00:00 ... Not from the current day ! (but 00:00 to the next day).
You can edit your code like this, by addind +1 day to your last variable :
$toTime1 = new DateTime();
$toTime1->modify('+1 day');
$toTime1->setTime(00,00);
Like this, your timings are good ;)
Don't forget this code (php) is managed by the server side. So, if your user don't reload the page, your message will be visible during your intervals. Maybe you can use JavaScript for hide the message during your intervals on the client side, in the case that the user remains on the page ;)
Hope I help you.

Related

Condition for specific range of time to identify Shift

I have a piece of PHP program that would supposedly identify the Shift time of a user when they login.. see below actual script condition.
$dt = new DateTime(date('H:i:s'));
$dt->setTimezone( new DateTimeZone("Asia/Manila") );
$timeNow = $dt->format('H:i:s');
if (strtotime($timeNow) >= mktime(07,0,0) && strtotime($timeNow) < mktime(15,0,0)){
$shift = "2nd [0700H - 1500H]";
}elseif(strtotime($timeNow) >= mktime(15,0,0) && strtotime($timeNow) < mktime(23,0,0)){
$shift = "3rd [1500H - 2300H]";
}else{
$shift = "1st [2300H - 0700H]";
}
The script above is working but there are records that falls into wrong shift.. for example a user access the page in 7:10AM which means should be in 2nd Shift but instead.. it falls into 1st Shift
I don't know what have I missed in this control flow.. so if anyone from here can help and share ideas that would be very much appreciated. Thank you!
There's a difference between using DateTime::setTimezone() and applying a timezone in the DateTime constructor. The former, converts the existing time to the DateTimezone specified as the argument to DateTime::setTimezone(), and the latter, assumes that the supplied formatted date is already in that timezone.
You're saying you want to know whether right now it's between 7 am and 3 pm in Manila, or between 3 pm and 11 pm, etc... So the idea is to compare only within that timezone. There is no need to do any timezone conversion here whatsoever.
$now = new DateTime("now", new DateTimeZone("Asia/Manila"));
$shift2 = new DateTime("7:00 AM", new DateTimeZone("Asia/Manila"));
$shift3 = new DateTime("3:00 PM", new DateTimeZone("Asia/Manila"));
$shift1 = new DateTime("11:00 PM", new DateTimeZone("Asia/Manila"));
if ($now >= $shift2 && $now < $shift3) {
// It's shift 2
} elseif ($now >= $shift3 && $now < $shift1) {
// It's shift 3
} else {
// It's shift 1
}

Multiple Operators in PHP If Statement

Just wanted to double check and make sure my line of thinking is correct. This hook runs every 48 hours, so I need to check if an event is happening today or tomorrow.
$now = date('Y/m/d');
$today = explode("/", $now);
The event start date has the same format, but the value varies, and is stored the same way.
if ( $today[1] == $eventDate[1] &&
(intval($today[2]) == (intval($eventDate[2]-1)) || (intval($today[2]) == intval($eventDate[2])-2))) {
//run code
}
In my opinion, you should consider to work with DateTime objects :
$today = new \DateTime();
$eventDateTime = \DateTime::createFromFormat('Y/m/d', $eventDate);
if ($today->format('d-m-Y') === $eventDateTime->format('d-m-Y')) {
...
}
If you want to check that an event is happening today or tomorrow, you could do like this :
$start = new \DateTime();
$start->setTime(0,0,0);
$end = new \DateTime();
$end->add(new \DateInterval('P1D'));
$end->setTime(23,59,59);
$eventDateTime = \DateTime::createFromFormat('Y/m/d', $eventDate);
if ($eventDateTime >= $start && $eventDateTime <= $end) {
...
}
This way you check if a date is in a certain period. Here I added 1 day to the current date but you can adapt the code and set more than 1 day if you want.

(PHP) Display link ONLY during business hours?

I am trying to make a live chat link appear on the website only during business hours. I have the code below which seems to work in the afternoon, but won't work in the morning and I'm not sure why... $start and $end are values received from a MySQL database but in my example I've hard coded them to make the example simpler.
$LinkStatus = "on";
$start = 9:00:00;
$end = 23:00:00;
$current_time = date('G:i:s'); //9:35:00
if (($start > $current_time) || ($end < $current_time)) {
$LinkStatus = "off";
}
If the start time is greater than the current time, then the business is not open yet. If the end time is less than the current time, then it's after hours. Any time between 9am and 11pm (23:00) neither one of those conditions should be true, therefore $LinkStatus should remain "on". However, it does not seem to be doing that right now. Something is setting it to "off".
I've echoed the variable above the if statement and below it so I can confirm it's this if statement causing the variable to be set to "off".
As you can probably see from my code example, I'm not very knowledgeable when it comes to PHP. Any help is appreciated.
date('G:i:s') // 24 hours time without leading zero for hour
...won't sort well as a string, for example '9' > '10'.
Use 24 hour time with a leading zero instead, which makes the correct sort '09' < '10';
date('H:i:s') // 24 hour time with leading zero for hour
date_default_timezone_set('Europe/London');
$day_start = '09:00:00';
$day_end = '16:59:59';
$current_time = date('H:i:s'); // For 9-5 hours only
$current_day = date('N'); // For Monday to Friday only
if (($current_day <= 5) && ($current_time >= $day_start) && ($current_time <= $day_end)) {
echo 'ON';
} else {
echo 'OFF'
}

How can I check to see if a request is being made between certain hours on specific days?

I'd like to write a function that returns a boolean true if the request is being made between 8am and 5pm central timw monday-saturday, and false any other time. I know that I will probably be using date() and strtotime()but outside of that, I'm lost. Any pointers?
desired result
if (DuringBusinessHours()) {
// execute this
} else {
// execute this
}
I would suggest something like this:
// create start and end date time
$start = new DateTime('8:00am');
$end = new DateTime('5:00pm');
// get the current time
$now = new DateTime();
// note that you can use the < > operators
// to compare date time objects
if($now >= $start && $now < $end) {
echo 'during business hours';
}

how to check depending on hour base

I have a field that has time and date in following format
2010-03-26 10:06:11
What I need is that if this time is within 4 hours of current time then show but if its over 4 hour then done show this record.
thanks
$ts = strtotime($value);
$curtime = time();
if (($ts > $curtime - 4*3600) && ($ts < $curtime + 4*3600)) {
//show
}
else
//don't show
You can also make a one-side comparison by choosing only one of the conditions (it isn't clear what you want from the question).

Categories