Im thinking I should be using in_array() but for some reason it is giving me inaccurate information. I looked through the array_search() and array_key_exists() but it looks like that is only helpful if I have a key and value in my array which I dont. In a nutshell Im running a condition to get the current EST time and day and determine if it is "during hours" or "after hours".
So it is 19:00 on Tuesday, this should say "After Hours" but it is echoing "During Hours", am I missing something?
Sample code:
<?php
date_default_timezone_set('US/Eastern');
$current_time = date('A'); //AM or PM
$current_day = date('l'); // Sunday - Saturday
$current_hour = date('H'); // 08 / 24hr Time Format
$closed_days = array('Saturday','Sunday');
$closed_hours = array('17','18','19','20','21','22','23','00','01','02','03','04','05','06','07','08');
?>
<?php
echo $current_time . '<br />';
echo $current_day . '<br />';
echo $current_hour . '<br />';
//Operating Hours
if(!in_array($current_day, $closed_days) || !in_array($current_hour, $closed_hours)) {
echo 'During Hours';
} else {
echo 'After Hours';
} ?>
Is returning:
PM
Tuesday
19
During Hours
Change:
if(!in_array($current_day, $closed_days) || !in_array($current_hour, $closed_hours)) {
to
if(!in_array($current_day, $closed_days) && !in_array($current_hour, $closed_hours)) {
The || equates to "or" so the condition was returning true say it was an off day but during your normal hours
Using && equates to "and" which requires both conditional statements to be true to execute the code block
I'm pretty sure it is as simple as changing your if statement from:
!in_array($current_day, $closed_days) || !in_array($current_hour, $closed_hours)
To:
!in_array($current_day, $closed_days) && !in_array($current_hour, $closed_hours)
By using OR, you are saying only 1 condition has to be true
Change
if(!in_array($current_day, $closed_days) || !in_array($current_hour, $closed_hours)) {
echo 'During Hours';
} else {
echo 'After Hours';
}
to
if(in_array($current_day, $closed_days) || in_array($current_hour, $closed_hours)){
echo 'After Hours';
}
else{
echo 'During Hours';
}
Related
// holiday array
$holy = [
'2020-12-23',
'2020-12-24',
'2020-12-25',
'2020-12-28',
'2020-12-29',
];
$inputDate = '2020-12-23'; // input
$outputDate = get_date($inputDate);
echo "Winning Day: " . $outputDate . "<br />";
echo "<br />";
function get_date($chkDate)
{
global $holy;
$chkDateYoil = date("w", strtotime($chkDate)); // sat(6), sun(0)
if ($chkDateYoil == 6) {
// Saturday when + 2
$timestamp = strtotime($chkDate . " +2 days");
$chkDate = date("Y-m-d", $timestamp);
} else if ($chkDateYoil == 0) {
// Sunday when + 1
$timestamp = strtotime($chkDate . " +1 days");
$chkDate = date("Y-m-d", $timestamp);
}
// If it's a weekday, compare it array
foreach ($holy as $key => $holyday) {
if ($chkDate == $holyday) {
// holiday when + 1
$day_plus = 8 - $chkDateYoil;
$timestamp = strtotime($chkDate." +".$day_plus." days");
$chkDate = date("Y-m-d", $timestamp);
}
}
return $chkDate;
}
Hello, let me ask a question.
The following codes are:
Is the input value weekend?
Or are they included in the array?
in accordance with the judgment
Weekday extraction code.
But there is an error.
in my estimation
December 30th is supposed to come out.
By the way, January 2, 2021 is the result.
Why is that?
sorry
i don't write english very well
Thank you for reading.
The problem is in this line: $day_plus = 8 - $chkDateYoil;, which is calculating the date of the next Monday the first time it's executed.
You're then looping through the rest of the $holy array, and updating $chkDate if necessary, but you're not recalculating the value of $chkDateYoil, so the output depends on the day of the week you run this. Today (23rd December) it stops on 2nd January
Your code can be simplified by just incrementing the date by 1 day and performing the checks again, continuing until you get a result. I've also used the PHP function in_array() to simplify the search of the $holy array, and incorporated it into the same test as Saturday and Sunday.
// holiday array
$holy = [
'2020-12-23',
'2020-12-24',
'2020-12-25',
'2020-12-28',
'2020-12-29',
];
$inputDate = '2020-12-23'; // input
$outputDate = get_date($inputDate);
echo "Winning Day: " . $outputDate . "<br />";
echo "<br />";
function get_date($chkDate)
{
global $holy;
do {
$chkDateYoil = date("w", strtotime($chkDate)); // sat(6), sun(0)
if (($chkDateYoil == 0) || ($chkDateYoil == 6) || (in_array($chkDate, $holy))) {
$timestamp = strtotime($chkDate . " +1 days");
$chkDate = date("Y-m-d", $timestamp);
} else {
return $chkDate;
}
} while (true);
}
I'm trying to find a solution to a conditional based on the day of the week and a time range within that day. I've managed to hunt down the code for the day of the week but I can't find how to incorporate a time frame within the day?
For example:
IF today is Monday AND between 2pm and 4pm THEN do THIS
This is what I have...
<?php
date_default_timezone_set('Australia/Perth'); // PHP supported timezone
$script_tz = date_default_timezone_get();
// get current day:
$currentday = date('l'); ?>
<?php if ($currentday == Monday){ ?>
Monday
<?php } elseif ($currentday == Tuesday){ ?>
Tuesday
<?php } elseif ($currentday == Wednesday){ ?>
Wednesday
<?php } elseif ($currentday == Thursday){ ?>
Thursday
<?php } elseif ($currentday == Friday){ ?>
Friday
<?php } elseif ($currentday == Saturday){ ?>
Saturday
<?php } elseif ($currentday == Sunday){ ?>
Sunday
<?php } else { ?>
<?php } ?>
I'm not sure if this may help for the time frame?
Check day of week and time
Basically this:
<?php
if (date('l') === 'Monday' && date('G') >= 2 && date('G') < 4) {
// do something
}
You were missing quotes around the day names.
The condition I wrote will evaluate to true on Monday between 2 PM and 4 PM (while 4 PM itself will not, e.g. the last allowed values is 3:59 PM).
you could use different date/time components to streamline the code:
function between($value, $start, $end)
{
return $value > $start && $value <= $end;
}
$hours = date("G");
switch( date("N"))
{
case 1: //monday
if (between($hours,12 + 2,12 + 4 )) //using 24h format to avoid checking am/pm
{
// IF today is Monday AND between 2pm and 4pm THEN do THIS
}
break;
case 2: //tuesday
break;
//....
}
You'd better user date('N') as it is not language dependant and date('H') to take advantage of the 24h format that is better fit for time comparison.
function date_in_frame($test_date, $day, $start, $end){
$d = new Datetime($test_date);
return $d->format("N") == $day && $d->format("H") >= $start && $d->format("H") < $end;
}
//test if "now" is Monday between 2pm (14:00) and 4pm (15:59)
var_dump(date_in_frame("now", 0, 14, 16));
This code here checks a DateTime is between a start and end time.
<?php
$date = new DateTime('2019-11-18 12:49');
$start = new DateTime('2019-11-18 09:00');
$end = new DateTime('2019-11-18 17:00');
if ($date > $start && $date < $end) {
echo 'In the zone!';
}
Note.
$date->format('l'); will return Monday or whatever day it is.
$date->format('H:i'); will return 13:15 or whatever time it is.
Have a play! https://3v4l.org/XK5KR
All the conditions packed into an array is easier for maintenance.
A simplified example:
$ranges = [
['Monday',12,14, function(){echo "do something";}],
['Tuesday',12,14, function(){echo "do something on Tue";}],
//: more
];
$curWeekDay = date('l');
$hours = date("G");
foreach($ranges as $range){
if($curWeekDay == $range[0] AND $hours >= $range[1] AND $hours < $range[2]){
$range[3]();
}
}
Output on Tue 13:25:
do something on Tue
I have some problems with dates. I need make if like --->
if your activity is less than 1 day do somethink
else if your activity is more than 1 day and less than 3 do moething else
else if your activity is more than 3 do moething else
I need this in PHP. My actual code is:
if (strtotime(strtotime($last_log)) < strtotime('-1 day') ) {
$prom .= "" . json_encode('last_activity') . ": " . json_encode("inactive less than 1 day") . ",";
} else if (strtotime($last_log) > strtotime('-1 day') && strtotime($last_log) < strtotime('-3 day')) {
$prom .= "" . json_encode('last_activity') . ": " . json_encode("inactive more than 1 day and less than 3 days") . ",";
} else if (strtotime($last_log) > strtotime('-3 day')) {
$prom .= "" . json_encode('last_activity') . ": " . json_encode("inactive more than 3") . ",";
}
I think I really don't understand date calculations.
Date_diff is much easier in this case:
$datetime1 = date_create(); // now
$datetime2 = date_create($last_log);
$interval = date_diff($datetime1, $datetime2);
$days = $interval->format('%d'); // the time between your last login and now in days
see: http://php.net/manual/en/function.date-diff.php
Or in your way:
if(strtotime($last_log) < strtotime('-1 day')){
// it's been longer than one day
}
If you want to do it with strtotime, do it like this:
date_default_timezone_set('SOMETHING FOR YOU');
$last_log = '-0.5 day';
$last_log_time = strtotime($last_log);
$minus1day_time = strtotime('-1 day');
$minus3day_time = strtotime('-3 day');
echo $last_log_time . "<br>";
echo $minus1day_time . "<br>";
echo $minus3day_time . "<br>";
if ($last_log_time < $minus3day_time)
{
echo "inactive more than 3";
}
elseif ( ($last_log_time <= $minus1day_time) && ($last_log_time >= $minus3day_time) )
{
echo "inactive more than 1 day and less than 3 days";
}
elseif ($last_log_time > $minus1day_time)
{
echo "inactive less than 1";
}
Couple things I changed from your code:
remove the strtotime(strtotime()). Do not do it twice!
For your second if, I added parentheses to ensure correct evaluation of conditions.
I reversed the order of your if. First check if it is very old (so < -3). Then check if it is between -3 and -1. Then check between -1 and now.
Added <= and >=. The = cases were missing from your code. So if the last_log was == -1, it was not processed ever.
I replace "else if" by "elseif".
I used variables because recalculating strtotime all over is wasteful. And it makes the code less readable IMHO.
Then apply the json_encode comment.
To explain why the logic was reversed:
the last login of a user will always be before now.
lets say that the user's last_login is 5 days ago. strtotime($last_login) will be smaller than strtotime('-1 days'), so the if will be true. But that is not what the OP wants! He wants here the case where the last login is older than 3 days.
Remember that we are comparing numbers in the past, so the smaller, the older.
$dateLog = new DateTime($last_log); // format if needed
$tomorrow = new DateTime("tomorrow");
$yesterday = new DateTime("yesterday");
$threeDaysAgo = new DateTime("-3 days");
if ($dateLog < $yesterday) {
// Do what you want
} else if ($dateLog > $yesterday && $dateLog < $threeDaysAgo) {
// Do another thing
} else if ($dateLog > $threeDaysAgo) {
// ...
}
The doc is here : http://php.net/manual/en/datetime.diff.php
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 need to check if the day today is Saturday or Sunday. And i am trying to use simple if function for that but I don't know why it doesn't seem to work.
<?php
$tdate = date("D");
echo "Today is $tdate - ";
if ($tdate != "Sat" || $tdate != "Sun") {
echo "Weekday";
}
else {
echo "Weekend: $tdate";
}
?>
And the output I am getting is as follows:
Today is Sat - Weekday
What is exactly wrong with the if function?
You are performing a OR, which checks if one or the other statements are true. This means that while $tdate != "Sat" is false, $tdate != "Sun" is true. If you simply change your OR (||) to an AND (&&), your code will work fine.
if ($tdate != "Sat" && $tdate != "Sun") {
Its a logical error you need to fix
What you are saying is
If "today is NOT Saturday" OR "today is NOT Sunday", then its a Weekday
So yields TRUE because, one of the two conditions has satisfied (when the day is either Saturday or Sunday) and it goes into the true block and prints as weekday
The fix can be in two ways, 1st what xdazz gave OR the one below
<?php
$tdate = date("D");
echo "Today is $tdate - ";
if (!($tdate == "Sat" || $tdate == "Sun")) {
echo "Weekday";
}
else {
echo "Weekend: $tdate";
}
?>
if ($tdate != "Sat" || $tdate != "Sun")
when it's Sat, So it's not Sun and condition of if is true.use && instead.