Show a message at specific times on a specific day - php

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

Related

Show text if current time is between 9:00pm and 10:30pm on friday

I am trying to show a text in a page only on Thursday between 9pm to 10pm.
At the moment I wrote this code:
if((date('N') == 4 && date('G') >= 21) || (date('N') == 4 && date('G') < 22)) {
echo "Text";
}
but not work.
The or was causing the problem. So it always echo out.
if(date('N') == 4 and ( date('G') >= 21 and date('G') < 22)) {
echo "Text";
}

Conditional based on day of week and time range

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

Time dependent URL redirection script, should work differently on Monday - How?

This is the top part of a script I am using for redirection.
It connects to two txt based databases depending on time and redirects the user to a particular link.
There are two databases
'db1.txt' and 'db2.txt'
I only want to use the 'db2.txt' on Mondays.
All other day it should work normally, but on Mondays only, it should not change to urls1.txt.
How can I achieve that ?
$time = date("Hi", time());
if ($time >= 2224 && $time <= 2359)
{
$db = "db1.txt";
}
elseif ($time >= 0000 && $time <= 729)
{
$db = "db1.txt";
}
else
{
$db = "db2.txt";
}
Edit:
I am from India, and I am using server time in my script to avoid more complications. The script is designed to load db1.txt from 9:00AM to 6:00PM IST, and db2.txt from 6:01PM to 8:59AM. So using the "if (date('N') !=1 " wont work. I will need to change the time-zone as well.
I think you need to add this condition != monday like this
$time = date("Hi", time());
if (date('N') !=1 && $time >= 2224 && $time <= 2359)
{
$db = "db1.txt";
}
elseif (date('N') !=1 && $time >= 0000 && $time <= 729)
{
$db = "db1.txt";
}
else
{
$db = "db2.txt";
}
note : N ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
Check the argument documentation for the date function. With:
date('N');
you should be able to check if it's Monday and execute your desired code conditionally, ex.:
if(date('N') === 1){
}else{
}

PHP is date between 2 other dates - Ignoring month

I am trying to determine if a day and time are between two others, I have the following...
$currentdate = date("N h:i:s A");
This returns the day of the week as a number and then the current time in 24 hour format.
I want to check if the $currentdate is between 9am on a Friday and 9am on a Monday.
What is the best way to tackle this?
I believe this should give you what your asking for but I'm sure there are better ways to implement. So basically the time has been converted into an INT for comparing and the hours are configured not to have a leading zero hence why $timeOne and $timeTwo is shorter. I've then used an if statement to test days and time on that specific day leaving you a slot to add your code if those conditions are met.
function checkDayTime() {
$day = date(w); //0 (for Sunday) through to 6 (for Saturday)
$timeOne = 90000;
$timeTwo = 90000;//Added for easier reading
$currentTime = (int) date('Gis'); //Time as INT 00000 > 240000
if (($day == 5 && $currentTime > $timeOne) || ($day == 6 || $day == 0) || ($day == 1 && $currentTime < $timeTwo)) {
//Between those hours
return TRUE;
} else {
//Not between those hours
return FALSE;
}
}
Just removed the extra if statement as it was not needed

Get time before noon

I am practicing with dates in php. I a bit of a newbie so bear my ignorance
I am trying to see when a time is before noon.
So I have a variable coming in with this format 2014-03-07 13:28:00.000
I get the time like this
$submissonTime = date('H:i:s', strtotime($value['job_submission_date']));
then I want to set another variable as $noon and i am doing this:
$noon = date('H:i:s', '12:00:00.000');
However the value of noon is 12:00:12
what i want to do is basically:
if($submissionTime <= $noon){
//do my stuff
}
NB I want to enter the if statement when even when it is 12:00:00 and stop entering when it is 12:00:01
Any help please?
Try
$noon = date('Y-m-d 12:00:00'); // today noon with date
$submissonTime = date('Y-m-d H:i:s', strtotime($value['job_submission_date']));
if(strtotime($submissonTime) <= strtotime($noon)){
//do my stuff
}
if you want to compare only time use both format
$noon = date('12:00:00');
$submissonTime = date('H:i:s', strtotime($value['job_submission_date']));
if (date("A") == "AM")
{
// AM-Code
} else {
// PM-Code
}
Why don't you go with only one string of code getting the hour?
$Hour = date("G"); //24-hour format of an hour without leading zeros
if($Hour < 12) {
// do the code
}
Or in your case
$Hour = date("G", strtotime($value['job_submission_date']));
update
If you need 12:00:00 and not 12:00:01 and later on, you will need to define minutes and seconds:
$Hour = date("G"); //24-hour format of an hour without leading zeros
$Minute = intval(date("i")); // will give minutes without leading zeroes
$Second = intval(date("s"));
if(($Hour < 12) || ($Hour == 12 && $Minute == 0 && Second == 0)) {
// do the code
}

Categories