I want to set $a = 5 between 09.00h and 16.59h, Monday to Friday.
$time = time()%86400;
if( (date("D") == "Mon") || (date("D") == "Tue") || (date("D") == "Wed") ||
(date("D") == "Thu") || (date("D") == "Fri") && $time >= 32400 && $time <= 61119){
$a = 5;
}
Have does the && time read at the end? Does it read "Mon OR Tue OR Wed OR Thu OR Fri..." AND "Time...", or instead "Fri AND Time"?
You're close, you'll need a set of parenthesis around the ORs, but a slightly cleaner way to write this might be:
if( in_array((date("D"), array("Mon","Tue","Wed","Thu","Fri"))
&& $time >= 32400 && $time <= 61119 ){
or even:
!in_array((date("D"), array("Sat","Sun"))
Related
I am trying to have a web form use different times on the calendar based on if the form is a certain ID, if it is use the first group, if not use the default. I am hoping someone can help me here. I am a novice at php and intermediate at best in coding, I would appreciate any help. Thank you!
public function is_time_disabled($dt, $h, $m) {
$monday = 1;
$tuesday = 2;
$wensday = 3;
$thursday = 4;
$friday = 5;
$saturday = 6;
if (($form_id == 31)
return (
(($dt->format('w') == $monday && $h < 9) || ($dt->format('w') == $friday && $h > 17) || ($dt->format('w') == $friday && $h == 17 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);}
) if else return (
(($dt->format('w') == $monday && $h < 11) || ($dt->format('w') == $friday && $h > 18) || ($dt->format('w') == $friday && $h == 18 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);
}
endif;))
This could be optimized (alot) and this is just a pointer in the right direction using your existing code, one funtion and two returns:
public function is_time_disabled($dt, $h, $m) {
$monday = 1;
$tuesday = 2;
$wensday = 3;
$thursday = 4;
$friday = 5;
$saturday = 6;
if ($form_id == 31) {
return (
(($dt->format('w') == $monday && $h < 9) || ($dt->format('w') == $friday && $h > 17) || ($dt->format('w') == $friday && $h == 17 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);
} else {
return (
(($dt->format('w') == $monday && $h < 11) || ($dt->format('w') == $friday && $h > 18) || ($dt->format('w') == $friday && $h == 18 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);
}
}
If you anticipate adding other forms then a switch might be better. If $form_id is not found in any of the case statements then default will be executed:
switch ($form_id) {
case 10:
//if the same as 31 then fall into the next one (no break no return)
case 31:
return (
(($dt->format('w') == $monday && $h < 9) || ($dt->format('w') == $friday && $h > 17) || ($dt->format('w') == $friday && $h == 17 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);
case 99:
//return something else
default:
return (
(($dt->format('w') == $monday && $h < 11) || ($dt->format('w') == $friday && $h > 18) || ($dt->format('w') == $friday && $h == 18 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);
}
I need to display a "Live News" div based on the time. The div will only say "watch the news" and be a link to the news site. Here is the div:
<div class="live-news">Watch the Live News</div>
Here are the times:
Mon - Fri
430am - 8am
noon - 1
5pm - 6:30pm
11 - 11:30pm
Saturday
7-830am
5-6pm
630-7pm
11-1130pm
Sunday
6-7am
5-6pm
630-7pm
11-12am
How would I build the array and then pick from the array to display the element?
******* UPDATED FOR PHP *******
Please help!
If you want to do that on the server use that.
$week_day = date('w');
$current_time = date('Hi');
$show_news_div = false;
if ($week_day >= 1 && $week_day <= 5)
{
if (($current_time >= '0430' && $current_time <= '0800') || ($current_time >= '1200' && $current_time <= '1300') || ($current_time >= '1700' && $current_time <= '1830') || ($current_time >= '2300' && $current_time <= '2330'))
{
$show_news_div = true;
}
}
if ($week_day == 6)
{
if (($current_time >= '0700' && $current_time <= '0830') || ($current_time >= '1700' && $current_time <= '1800') || ($current_time >= '1830' && $current_time <= '1900') || ($current_time >= '2300' && $current_time <= '2330'))
{
$show_news_div = true;
}
}
if ($week_day == 0)
{
if (($current_time >= '0600' && $current_time <= '0700') || ($current_time >= '1700' && $current_time <= '1800') || ($current_time >= '1830' && $current_time <= '1900') || ($current_time >= '2300' && $current_time <= '2359'))
{
$show_news_div = true;
}
}
if ($show_news_div)
{
?>
<div class="live-news">Watch the Live News</div>
<?php
}
I'm trying to make an messages appear on my website when we are open and when the phones are "closed" but I can't seem to get it working.
This is what I have so far (This is my inspiration http://codewalkers.com/c/a/Date-Time-Code/Display-message-according-to-hour-of-day/)
<?php
//Change message of the day
$open = 'We are open for business';
$closed = 'We are closed';
//Get the current time
$current_time = date(G);
//Get the current day
$current_day = date(I);
if ($current_day == "Monday" && $current_time >= 9 && $current_time <= 21) {
echo $open;
}
elseif ($current_day == "Monday" && $current_time >= 21 && $current_time <= 9) {
echo $closed;
}
if ($current_day == "Tuesday" && $current_time >= 9 && $current_time <= 21) {
echo $open;
}
elseif ($current_day == "Tuesday" && $current_time >= 21 && $current_time <= 9) {
echo $closed;
}
if ($current_day == "Wednesday" && $current_time >= 9 && $current_time <= 21) {
echo $open;
}
elseif ($current_day == "Wednesday" && $current_time >= 21 && $current_time <= 9) {
echo $closed;
}
if ($current_day == "Thursday" && $current_time >= 9 && $current_time <= 21) {
echo $open;
}
elseif ($current_day == "Thursday" && $current_time >= 21 && $current_time <= 9) {
echo $closed;
}
if ($current_day == "Friday" && $current_time >= 9 && $current_time <= 19) {
echo $open;
}
elseif ($current_day == "Friday" && $current_time >= 19 && $current_time <= 9) {
echo $closed;
}
if ($current_day == "Saturday") {
echo $closed;
}
if ($current_day == "Sunday") {
echo $closed;
}
?>
and im getting this error message
Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in - on line 7 Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in - on line 9
It would be nice to have some qualified help :)
The qualified help you requested comes in the form of the script below:
Also note that in your code you have to use date('l') - lowercase L.
The list of timezones you can use for date_default_timezone_set() can be found here:
http://php.net/manual/en/timezones.php
date_default_timezone_set('Europe/Amsterdam'); // set it to the right value
$weAreOpen = areWeOpen(date('l'), date('G'));
if($weAreOpen) {
echo 'We are open for business';
} else {
echo 'We are closed';
}
/**
* Test if we're open for business
* #param string $day - day of week (ex: Monday)
* #param string $hour - hour of day (ex: 9)
* #return bool - true if open interval
*/
function areWeOpen($day, $hour) {
$hour = (int)$hour;
switch($day) {
case 'Monday':
case 'Tuesday':
case 'Wednesday':
case 'Thursday':
if($hour >= 9 && $hour < 21) {
return true;
}
break;
case 'Friday':
if($hour >= 9 && $hour < 19) {
return true;
}
break;
}
return false;
}
date() takes a string, and your code could be shorter:
//Change message of the day
$open = 'We are open for business';
$closed = 'We are closed';
$current_time = date('G'); //Get the current time
$current_day = date('w'); //Get the current day
if ($current_day > 0 && $current_day < 6) //Monday to Friday
{
if ($current_time >= 9 && $current_time <= 21 && $current_day != 5) //Monday to Thursday between 9 and 21
echo $open;
else if ($current_time >= 9 && $current_time <= 19 && $current_day == 5) //Friday between 9 and 19
echo $open;
else
echo $closed;
}
else //Saturday and Sunday
echo $closed;
About your warning, open your php.ini and add this block if you don't have it:
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Seoul
You can also just add this line at the beginning of your code: date_default_timezone_set('Asia/Seoul');.
See http://php.net/manual/en/timezones.php for the list of supported timezones.
So perhaps this will solve your problem:
//Get the current time
$current_time = date('G');
//Get the current day
$current_day = date('l');//This is lowercase 'L' and NOT 'I' as in "India".
And as far as I know, php date() accepts string as first argument. And the G and L can only work well if you had earlier defined them as constants...
EDIT
Seeing your updated question with the error message, you can try this:
ini_set('date.timezone', 'Africa/Lagos');' //somewhere at the top of your code.
Or else you could place this in you php configuration file (e.g. php.ini in windows):
date.timezone = Africa/Lagos
Then, of course, replace "Africa/Lagos" with the timezone of your choice. And for a list of supported timezones, see here.
try this
<?php
date_default_timezone_set('UTC');
//Change message of the day
$open = 'We are open for business';
$closed = 'We are closed';
//Get the current time
$current_time = date("G");
//Get the current day
$current_day = date("l");
if ($current_day == "Monday" && $current_time >= 9 && $current_time <= 21) {
echo $open;
}
elseif ($current_day == "Monday" && $current_time >= 21 && $current_time <= 9) {
echo $closed;
}
if ($current_day == "Tuesday" && $current_time >= 9 && $current_time <= 21) {
echo $open;
}
elseif ($current_day == "Tuesday" && $current_time >= 21 && $current_time <= 9) {
echo $closed;
}
if ($current_day == "Wednesday" && $current_time >= 9 && $current_time <= 21) {
echo $open;
}
elseif ($current_day == "Wednesday" && $current_time >= 21 && $current_time <= 9) {
echo $closed;
}
if ($current_day == "Thursday" && $current_time >= 9 && $current_time <= 21) {
echo $open;
}
elseif ($current_day == "Thursday" && $current_time >= 21 && $current_time <= 9) {
echo $closed;
}
if ($current_day == "Friday" && $current_time >= 9 && $current_time <= 19) {
echo $open;
}
elseif ($current_day == "Friday" && $current_time >= 19 && $current_time <= 9) {
echo $closed;
}
if ($current_day == "Saturday") {
echo $closed;
}
if ($current_day == "Sunday") {
echo $closed;
}
?>
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";
}
High Season:
April 28 - September 30,
December 27 - January 3
Low Season:
October 1 - December 26,
January 4 - April 27
I gave a date to check: 2014-02-18 and I want to have TRUE or FALSE in case of which season includes it. How to do it regardless of the current year?
Try with simple date comparing:
function is_high_season($date) {
$md = gmdate('m-d', strtotime($date));
return
('03-28' <= $md && $md <= '09-30') ||
('12-27' <= $md && $md <= '12-31') ||
('01-01' <= $md && $md <= '01-03');
}
demo
$day = $d->format('j');
$month = $d->format('n');
if($month == 1 && $day <= 3) {
return 'high';
} elseif $month < 4 || ($month == 4 && $day < 28)) {
return 'low';
} elseif($month == 4 && $day >= 28) {
return 'high';
} elseif($month < 10) {
return 'high';
} elseif($month < 12 || ($month == 12 && $day < 27)) {
return 'low';
} elseif($month == 12 && $day >= 27) {
return 'high';
}
$Date = date('Y-m-d');
$Date=date('Y-m-d', strtotime($Date));;
//echo $Date; // echos today!
$hiDateBegin = date('Y-m-d', strtotime("28/04/2014"));
$hiDateEnd = date('Y-m-d', strtotime("30/09/2014"));
$hiDateBegin2 = date('Y-m-d', strtotime("27/12/2014"));
$hiDateEnd2 = date('Y-m-d', strtotime("03/01/2015"));
$lowDateBegin = date('Y-m-d', strtotime("01/10/2014"));
$lowDateEnd = date('Y-m-d', strtotime("26/12/2014"));
$lowDateBegin2 = date('Y-m-d', strtotime("04/01/2015"));
$lowDateEnd2 = date('Y-m-d', strtotime("27/04/2015"));
if (($Date > $hiDateBegin) && ($Date < $hiDateEnd))
{
echo "is Hi Season";
}
if (($Date > $hiDateBegin2) && ($Date < $hiDateEnd2))
{
echo "is Hi Season";
}
if (($Date > $lowDateBegin) && ($Date < $lowDateEnd))
{
echo "is Low Season";
}
if (($Date > $lowDateBegin2) && ($Date < $lowDateEnd2))
{
echo "is Low Season";
}
else
{
echo "Date doesn't fall in a season!";
}