My goal is to subtract the two times and then generate a logged in checker from there.
What I need to know, is if I have correctly subtracted the two times in minutes.
PHP Version: 7.0
Time is entered into the DB using NOW() and shows as (Example: 2016-07-23 15:01:34)
For some reason, where this code is included in HTML, is just blank.
Code (everything is defined higher up in the code) :
<?php
include ('../includes/connection.php');
include ('../scripts/functions.php');
$getOnlineAdmins = $handler->prepare('SELECT * FROM Admins WHERE AdminLevel >= :al AND Status= :stat');
$statn = 1;
$aln = 1;
$getOnlineAdmins->bindParam(':al', $aln);
$getOnlineAdmins->bindParam(':stat', $statn);
$getOnlineAdmins->execute();
echo "
<table class='table-fill'>
<thead>
<tr>
<th class='text-left' style='padding-left: 12px; padding-right: 12px;';></th>
</tr>
</thead>
<tbody class='table-hover'>";
if ($getOnlineAdmins->rowCount() >=1){
echo ("These is one or more rows.");
while ($results = $getOnlineAdmins->fetch()){
if((strtotime($results['LastVisited']) + 900) >= time()){
echo ("Time requirement met.");
switch ($results['AdminLevel']) {
case 3:
$rank = 'In-Training/Intern';
break;
case 6:
$rank = 'Community Moderator';
break;
case 9:
$rank = 'Senior Moderator';
break;
case 12:
$rank = 'Supervisor';
break;
case 15:
$rank = 'Administrator';
break;
case 18:
$rank = 'Senior Administrator';
break;
case 21:
$rank = 'Staff Advisor';
break;
case 24:
$rank = 'Engineer';
break;
case 27:
$rank = 'Vice Chairman';
break;
case 30:
$rank = 'Chairman';
break;
case 33:
$rank = 'Website Engineer';
break;
default:
$rank = 'Unknown';
break;
}
echo "<tr>";
echo "<td>" . $results['Username'] . " - " . $rank . "</td>";
} else {
echo "<tr><td>{$results['Username']} – $rank</td>";
}
}
}else{
echo "<tr><td>There are no staff members online.</td>";
}
echo " </tbody>
</table>";
?>
As far as I understand it, now() is not getting the current time as you expect it to. See this question for what I believe to be the root of your misunderstanding.
Assuming $results['LastVisited'] is in the format 2016-07-23 14:11:02 – you could convert it to a UNIX timestamp with strtotime.
So if the time 15 minutes ago is less than or equal to your last visited time:
if (strtotime('-15 minutes') <= strtotime($results['LastVisited'])) {}
Alternative format with time() – that you would use instead of the MySQL equivalent UNIX_TIMESTAMP():
if ((time() - 900) <= strtotime($results['LastVisited'])) {}
Related
I am trying to insert data into database by checking the days in a week but I am unable to insert if I check only one day in a week, if I check two or more days I am able to insert.
<?php
//other parameters
$days_only = $_REQUEST['days_only'];
using an array to split days in a week
$day_array = explode(",",$days_only);
$count_sample = count($day_array);
if($count_sample==1) {
$in_sample='('.$days_only.')';
}
elseif($count_sample>0) {
$in_sample="('".str_replace(",","','" , $days_only)."')";
}
$test='IN'.$in_sample;
$sth = "SELECT e_id FROM `schedule` WHERE
user_id = '$user_id' AND
date BETWEEN '$start_date' AND '$end_date' AND
(
TIME(start_time) >= '$start_time' AND
TIME(start_time) <= '$end_time' OR
TIME(end_time) >= '$start_time' AND
TIME(end_time) <= '$end_time'
) AND days_only $test";
$res = $db->prepare($sth);
$res->execute();
$count = $res->fetchAll();
if ($count) {
echo "すでにスケジュールが登録されています";
} else {
for($i = strtotime($start_date); $i <= strtotime($end_date); $i +=86400 ){
$date = date("Y-m-d", $i);
$days_only = date('l', $i);
switch ($days_only) {
case 'Monday': $days_only = "月"; break;
case 'Tuesday': $days_only = "火"; break;
case 'Wednesday': $days_only = "水"; break;
case 'Thursday': $days_only = "木"; break;
case 'Friday': $days_only = "金"; break;
case 'Saturday': $days_only = "土"; break;
case 'Sunday': $days_only = "日"; break;
}
echo "$date";
echo "$days_only";
if (in_array($days_only,$day_array)){
query for inserting into database
}
else{
echo "wrong";
}
}
}
?>
Here's the issue:
if( $count_sample==1 ) {
$in_sample='('.$days_only.')';
}
The string that you're passing in $days_only is not being escaped here, but it is when the $count_sample value is greater than one. The above code will give you something like this: (金)
Perhaps you can replace the two outermost single-quotes with a double-quote and drop the concat period:
$in_sample= "('$days_only')";
Or you can replace a good bit of code with a single implode():
if ( $count_sample > 0) {
$in_sample="('" . implode("','", $days_only) . "')";
}
Be sure to remember to add validation to your inputs before sending this to the database, otherwise you leave yourself open to SQL injection 👍🏻
i am trying to create dynamic price calculation by distance.My requirment similar like this
so I need a function where I can pass
distance and then get price by distance
From 0 -10km = Rs10rs/km (fixed price)
10-15km = rs10Rs/km
15 -20km = 9rs pre/km
20 -30km = 8.5rs per/km
25 -30km = 8rs per/km
30 -40km = 7.5rs per/km
30km -50km above = 7rs / km
foreach ($usersInfo as $index=> $users)
{
$km= 0;
$ridefrom=$users['ride_from'];
$rideto=$users['ride_to'];//this is from lat long
$tempLatLong = explode(',',$users['ride_from']);
$tempLatLong1 = explode(',',$users['ride_to']);
$key = array('lat','long');
$to = array_combine($key,$tempLatLong);
$from = array_combine($key,$tempLatLong1);
$km=distanceCalculation($from['lat'],$from['long'],$to['lat'],$to['long']).'km';
$usersInfo[$index]['distance'] =$km;
$carprice=10;// price will be change dynamical from back end admin can change this.
if($carprice){
$price=$km*10;
}
else{
$price=$km*9;
}
$usersInfo[$index]['price'] =$price;
return $usersInfo;
}
Try something like this:
function price_per_km($distance, $mode) {
if ($mode == 'car') {
switch (true) {
case $distance <= 10:
return 10*$distance;
case $distance <= 15:
return 10*$distance;
case $distance <= 20:
return 9*$distance;
case $distance <= 25:
return 8.5*$distance;
case $distance <= 30:
return 8*$distance;
case $distance <= 40:
return 7.5*$distance;
default:
return 7*$distance;
}
}
else {
// bike
switch (true) {
case $distance <= 10:
return 12*$distance;
case $distance <= 15:
return 11*$distance;
case $distance <= 20:
return 10*$distance;
case $distance <= 25:
return 9*$distance;
default:
return 8*$distance;
}
}
}
echo price_per_km(5, 'car') . "\n";
echo price_per_km(12, 'car') . "\n";
echo price_per_km(25, 'car') . "\n";
echo price_per_km(35, 'car') . "\n";
echo price_per_km(5, 'bike') . "\n";
echo price_per_km(15, 'bike') . "\n";
echo price_per_km(35, 'bike') . "\n";
Output:
50
120
212.5
227.5
60
165
270
if (isset($_POST["submit"])){
$oride='';
$count = "25";
$origin = $_POST["origin"];
$destinataion = $_POST["destination"];
$oride = ($destination = $_POST["destination"] - $origin= $_POST["origin"]);
switch (true) {
case ($count<="0"):
echo "invalid";
break;
case ($count==="15"):
echo $count;
break;
case ($count==="16"):
$total = $count + "1";
echo $total;
break;
default:
echo "hello";
} }
The code will compute 1st then execute switch depending on what is the result of the computation. I tried if else but it will be too long because the case will go up to 130.
You must use the var $count in switch statement and the constant in case this way
switch ($count) {
case "0" :
echo "invalid";
break;
case "15":
echo $count;
break;
case "16":
$total = $count + "1";
echo $total;
break;
default:
echo "hello";
break;
}
You have to provide an expression to the switch statement, while the case statements are just "versions" of the result of that expression. The only thing you can NOT do directly is the "<= 0" expression, but you can work around it:
if (isset($_POST["submit"])){
$oride='';
$count = "25";
$origin = $_POST["origin"];
$destinataion = $_POST["destination"];
$oride = ($destination = $_POST["destination"] - $origin= $_POST["origin"]);
// --- normalize $count:
$count = $count <= 0 ? 0 : $count;
// use $count as expression:
switch ($count) {
case 0:
echo "invalid";
break;
case "15":
echo $count;
break;
case "16":
$total = $count + "1";
echo $total;
break;
default:
echo "hello";
} }
I have a coding matter i need your aid with. I have attempted to solve it for a few days, but all it results in is frustration. I haven't found any existing examples or help on the internet, and all i have done so far is staring at my code, my mind blank.
I need a countdown to 17:00 (5 PM) each day, echoing a message saying that a certain amount of time is remaining until 17:00. After 17:00, i want to echo another message. Then, the timer needs to reset after 23.59. How can i do this with PHP?
Any help would be appreciated! Below is the code i already have, but i don't think any part of it would affect what i need help with:
<?php
session_start();
#ADD ITEM
if( isset( $_POST['newitem'] ) ){
$_SESSION['todo'][]=$_POST['newitem'];
}
#REMOVE ITEM
if( isset( $_POST['remove_id'] ) ){
$id = $_POST['remove_id'];
unset( $_SESSION['todo'][$id] );
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
$i = 0;
echo "<h1>To Do</h1>";
#SUBMIT
echo "
<form action='' method='post'>
<input type='text' name='newitem'>
<input type='submit'>
</form>
";
foreach( $_SESSION['todo'] as $id => $item ){
$i++;
#REMOVE
echo "
<form action='' method='post'>
<input type ='hidden' name='remove_id' value='$id'>
<input type='submit' value='-'>
$item
</form>";
}
echo $i;
#CHANGE BACKGROUND COLOR DEPENDING ON DAY
$day=date("l");
switch($day) {
case 'Monday':
$bg_color = "red";
break;
case 'Tuesday':
$bg_color = "blue";
break;
case 'Wednesday':
$bg_color = "purple";
break;
case 'Thursday':
$bg_color = "gray";
break;
case 'Friday':
$bg_color = "yellow";
break;
case 'Saturday':
$bg_color = "green";
break;
case 'Sunday':
default:
$bg_color = "beige";
break;
}
echo "<body style='background-color:$bg_color'>";
?>
</body>
</html>
I need a countdown to 17:00 (5 PM) each day, echoing a message saying
that a certain amount of time is remaining until 17:00. After 17:00, i
want to echo another message. Then, the timer needs to reset after
23.59. How can i do this with PHP?
if(date("H") < "17") echo "There are " . (16 - date("H")) . " hours and " . (60 - date("i")) . " minutes left until 17:00.";
else if(date("H") == "17" && date("i") == "00") echo "It is 17:00.";
else echo "17:00 is over for today.";
(sorry for my bad English writing). I've got a problem where I'm stuck with an if and else system that returns the level that you're.
Everything is good, if you have 20 clicks it will tell you that you're level 2, if you have 550 clicks it will tell you're level 6. But when you go up to 1500 clicks it needs to say level 9, but it will still say level 8.
My code:
$levelown = 'Level 1';
function ifElse() {
global $levelown;
global $arrayIP;
if($arrayIP['clicks'] >= 0 && $arrayIP['clicks'] <= 49)
{
$levelown = 'Level 2';
}
/* .... More if and elses with levels */
// This is the problem, this will keep telling me that I'm level 8.
elseif($arrayIP['clicks']>=3000)
{
$levelown = 'Level ' . floor(($score['clicks']/1000)+8);
}
and you are <strong><?php echo $levelown; ?></strong>
Thank you for helping!
The problem that i saw from your current code is that you have
elseif($arrayIP['clicks']>=3000)
{
$levelown = 'Level ' . floor(($score['clicks']/1000)+8);
}
which i think should be
elseif($arrayIP['clicks']>=3000)
{
$levelown = 'Level ' . floor(($arrayIP['clicks']/1000)+8);
}
since $score['clicks'] isn't present you will always end up with level 8
It's probably easier and more readable to make it a switch/case statement like so:
$highscore = $mysqli->query("SELECT id,name,clicks,ip,factory FROM highscore ORDER BY clicks DESC LIMIT 0,50 ");
$ipquery = $mysqli->query("SELECT id,name,clicks,ip,factory FROM highscore WHERE ip = '".$_SERVER['REMOTE_ADDR']."'");
$arrayIP = $ipquery->fetch_array();
$levelown = 1;
function ifElse() {
global $levelown;
global $arrayIP;
switch (true) {
case $arrayIP['click'] > 3000:
$levelown = floor(($arrayIP['click']/1000)+8);
break;
case $arrayIP['click']== 3000:
$levelown = 11;
break;
case $arrayIP['click'] >= 2000:
$levelown = 10;
break;
case $arrayIP['click'] >= 1500:
$levelown = 9;
break;
case $arrayIP['click'] >= 1000:
$levelown = 8;
break;
case $arrayIP['click'] >= 750:
$levelown = 7;
break;
case $arrayIP['click'] >= 500:
$levelown = 6;
break;
case $arrayIP['click'] >= 350:
$levelown = 5;
break;
case $arrayIP['click'] >= 200:
$levelown = 4;
break;
case $arrayIP['click'] >= 50:
$levelown = 3;
break;
case $arrayIP['click'] >= 0:
$levelown = 2;
break;
}
$levelown = "Level " . $levelown;
}