Function Running When Not Called In PHP - php

Hi I am running the following script, the problem I am having is that the monthly functions are getting called even though it isn't the first Monday of the month ?. What is supposed to happen is every day the every day runs, every Monday the every Monday runs, every month the monthly runs.
By the way, I havnt called these script with cron on those dates as I have to ask the server owner to setup a cron job each time which is a major pain in the rear as he takes ages to do it, so its easier to just have cron hit the script every day and I control the rest with php.
// SETS THE TIMEZONE TO UK TIME
date_default_timezone_set('Europe/London');
// DEFINES WEEKDAY AND DAY OF THE MONTH
$weekday = date('D');
$dayOfMonth = date('d');
// RUNS THE EVERY DAY FUNCTIONS TO MAIL USERS IN THAT GROUP EVERY DAY
gasoildailyemailer();
dervdailyemailer();
kerodailyemailer();
if ($weekday == 'Mon')
{
// RUNS THE WEEKLY FUNCTIONS TO MAIL USERS IN THAT GROUP EVERY WEEK
gasoilweeklyemailer();
dervweeklyemailer();
keroweeklyemailer();
if ($dayOfMonth <=6)
// RUNS THE MONTHLY FUNCTIONS TO MAIL USERS IN THAT GROUP EVERY MONTH
gasoilmonthlyemailer();
dervmonthlyemailer();
keromonthlyemailer();
}
?>
<?php
function gasoildailyemailer() {
echo 'GAS OIL DAILY';
};
function dervdailyemailer() {
echo 'DERV DAILY';
};
function kerodailyemailer() {
echo 'KERO DAILY';
};
?>
<?php
function gasoilweeklyemailer() {
echo 'GAS OIL WEEKLY';
};
function dervweeklyemailer() {
echo 'DERV WEEKLY';
};
function keroweeklyemailer() {
echo 'KERO WEEKLY';
};
?>
<?php
function gasoilmonthlyemailer() {
echo 'GAS OIL MONTHLY';
};
function dervmonthlyemailer() {
echo 'DERV MONTHLY';
};
function keromonthlyemailer() {
echo 'KERO MONTHLY';
};
?>

This is what you get for not always using braces:
if ($dayOfMonth <=6)
gasoilmonthlyemailer(); // THIS IS INSIDE THE IF
dervmonthlyemailer(); // BUT THIS IS OUTSIDE!
keromonthlyemailer(); // THIS ONE TOO!
Fix it by adding braces as you shoud always do (warning: possible religious argument here; this is just my own view):
if ($dayOfMonth <=6) {
gasoilmonthlyemailer();
dervmonthlyemailer();
keromonthlyemailer();
}
As an aside: you would know that only two of the three functions were being incorrectly called. Why not spell that out explicitly so that everyone else benefits from that knowledge up front?

You're missing the brackets after if ($dayOfMonth <=6).

Related

How to validation the starting date and ending date from database so it will not collide with time

Currently, I had some data in my database which was
I had do some action to take out the class_time. Which was later became
$Starttimein=$_POST['startTime'];
$Endtimein=$_POST['endTime'];
$varstart=preg_split('/[\s:\s]+/', $Starttimein);
$totvarstart=$varstart[0]*100+ $varstart[1];
$varend=preg_split('/[\s:\s]+/', $Endtimein);
$totvarend=$varend[0]*100+ $varend[1];
$sql8="select * from class where sup_id='$idselect' ";
$result8=mysqli_query($con,$sql8);
while($row8=mysqli_fetch_assoc($result8))
{
$preclasstime=$row8['class_time'];
$keywords = preg_split('/[\s,\s:\s-\s:\s]+/', $preclasstime);
$day = $keywords[0];
$tottimestart = $keywords[1]*100 + $keywords[2];
$tottimeend = $keywords[3]*100 + $keywords[4];
if($day==$_POST['csday'])
{
if((($totvarstart>= $tottimestart) && ($totvarstart<= $tottimeend)) || (($totvarend>= $tottimestart) && ($totvarend<= $tottimeend)))
{
?>
<script type="text/javascript">
alert("Time crash");
</script>
<?php
}
else
{
echo "correct";
}
}
}
The "day,starting and ending time" was separate using preg_split function. And I wish to validate if user select the same day, it will compare the input starting and ending time whether the input time crash with the time which store in database. For example: if user select Mon, 10.30-11.30, it will display a alert message say that cannot add class time as it is crash with previous time. I had tried using the if((($totvarstart>= $tottimestart) && ($totvarstart<= $tottimeend)) || (($totvarend>= $tottimestart) && ($totvarend<= $tottimeend)))
to validate it, but cannot work.
Don't save these three values "day", "start time" and "end time" in one column, which you have to split later anyway. Keep them separated in three columns "dayofweek", "starttime" and "endtime". Use the TIME type for the latter so you can run comparison operators on them.
There are 6 ways how a time span can be arrange in relation to another time span. You can check this in the database to get only the rows which are in conflict with your input values. The checks should look like this:
[...]
WHERE
dayOfWeek = :dayOfWeek AND
(
(:starttime < starttime AND starttime < :endtime) OR
(:starttime < endtime AND endtime < :endtime) OR
(starttime < :startime AND :endtime < endtime)
)
Note: :starttime and :endtime are the bound variables from your prepared statement. Depending on your API you might need to use ? instead.
This way you don't need to read the whole database just to check if they are overlapping or not.

How can I query mysql table and display result using php conditional statements

I want to show user that he has vaccinated his goat after successfully payment confirmation.
Here is what I tried:
<?php
$treatperiod1=$product_row['treat1'];
$currentDate = date("Y-m-d");
$totalDays = $treatperiod1;
$usedDays = round(abs(strtotime($currentDate)-strtotime($orderdate))/60/60/24);
$remainingDays = $totalDays-$usedDays;
if($remainingDays==0) {
echo "<a target = '_blank' href ='product_addon.php?treatp=$treatperiod1' class='btn btn-success'><i class='fa fa-pencil'></i>Vaccinate Gaoat</a>";
} elseif($remainingDays==$treatperiod1) {
echo 'Vaccinated';
} else {
echo $remainingDays.' Days Left';
}
?>
I have displayed remaining days for vaccination intervals I want to also display 'Vaccinated' if user payment for vaccination is confirmed. $product_row['treat1']; is a column where the number of days for vaccination is specified. order_details is a table for orders with column confirm for confirmed orders.
Goat Vaccination:
Because Goats don't get Autism
Please read how to use PHP DateTime objects because they will make your life a lot easier on this project.
I will rewrite your code and then tell you what the new code does:
//establish DateTime objects.
$currentDate = new DateTime(); //now.
$orderDate = new DateTime($orderdate); //$orderdate format Y-m-d
//Establish how may days since order date and today.
$difference = $orderDate->diff($currentDate);
$usedDays = $difference->days;
/***
Total Period paid for,
in days, integer.
$product_row['treat1'];
***/
$remainingDays = $product_row['treat1'] - $usedDays;
//Returns an integer value of remaining days.
if($remainingDays < 1) {
echo "<a target = '_blank'
href ='product_addon.php?treatp=".$treatperiod1."'>
Vaccinate Goat</a>";
} elseif($remainingDays==$product_row['treat1']) {
//This will only fire if the goat was vaccinated TODAY.
echo 'Vaccinated';
} else {
echo 'Vaccinated:' .$remainingDays.' Days Left';
}
Hopefully with the help of the link above you should be able to see the basics of what I wrote out for you.
Some notes:
Stop making variables that are simply copies of other variables, it's inefficient and confusing.
(Try to) Stop sending data to new PHP pages as GET URL values such as /newpage.php?treatp=".$var.". This is very probably very insecure.
A more specific answer will need a full explanation from you of your MySQL tables, if nessecary please edit your question and add them.
Try and move away from using timestamps and treating timestamp variables as mathematical entities, and use the DateTime object instead.
Really, to determine if a payment is made you should have a payment date value in your database (set when a payment is successful) and simply compare this date to the value of the integer of the number of days payment covers, using the ->diff() method illustrated above.
Further information on Goat Vaccinations

Wordpress Delay content being published to non-members

I have a wordpress site that uses the WPMU Membership plugin. I want to add code to my template that displays a new post to members 5 days before it is shown to non-members.
Something like this:
if (current_user_on_level(17) && CODE TO DETERMINE IF POST IS LESS THAN 5 DAYS OLD) {
echo POST CONTENT;
}
else {
echo 'Info not available yet';
}
;
What code would I use to determine whether the published post is less than 5 days old??
I figured out the answer. First had to convert the post time to a UNIX code using this code:
(True sets it to UNIX epoch time rather than set to local time zone)
Then I used this code to get the UNIX time 5 days ago
strtotime("-5 day")
Put the two together with an if statement that displays the content or if it's not time it displays a message that the content isn't available yet.
<?php
$post_timestamp = get_post_time('U', true);
if (current_user_on_level(17) && $post_timestamp <= strtotime("-1 day"))
{
get_template_part( 'content', get_post_format() );
}
else {
echo 'No post available yet <br />';
}?>
The only issue with this is that because my posts are ordered in chronological order, the top of my blog is filled with the line "No post available yet". Can anyone think of how I can get these statements to sit at the bottom?

Disable PHP count?

So, I am trying to modify a piece of code that I have, it is a calendar that is used for taking reservations. I am posting events on the calendar that users will be able to book by clicking on the link on any given day. It currently allows me to only post 2 events for booking, on any given day. These 2 cannot be at the exact same time. I want to change that, I need it to be able to post multiple spots available at the exact same time. For example, if it is a martial arts class and there are 10 open spots, it should show 10 spots available at the same time of day... You get the point...
I believe this is what is preventing me from achieving that, but im not certain...
Any help is much appreciated. Thanks is advance.
<script>
<?php
if($numslots>0) {
?>
alert('<?php echo $numslots; ?> slots added');
<?php
} else if($numslots == -1) {
?>
alert('Days selected are holidays. Cannot make these changes');
<?php
} else {
?>
alert('Duplicate slots. Cannot make these changes');
<?php
}
?>
document.location.href="calendar_manage.php?calendar_id=<?php echo $_GET["calendar_id"]; ?>&ref=slots";
</script>
In your example, where you have ten slots available for one time, you need to populate a new variable.. lets call it $available_slots.. you need to populate this with the number of slots, in this case ten. So you would end up with ...
$available_slots = 10;
if($numslots<$available_slots) {
?>
alert('<?php echo $numslots; ?> slots added');
<?php
} else if($numslots == -1) {
etc...
Notice the first if condition.. we now check if $numslots is less than $available_slots, rather than greater than zero.

how to disable a button for a specific time in php

I have an edit button for users to edit their posted jobs. I want that button to disable or disappear a week after published day.how can I do this in php. thanks advance...
<?php
$timePublished = 1334301250; // unix time when job posted
$afterOneWeek = $timePublished+(60*60*24*7); // 1 week
$dateNow = date('U'); // unix time for Date Now
if($dateNow > $afterOneWeek){ // if Date Now is greater than our "After One Week"
echo "<button>Button is now visible</button>";
} else {
echo "No button, one week not passed";
}
?>

Categories