How to compare the time in from the nearest scheduled time - php

can someone please help me how to fix this code. What I need is to let the current time compare it to the nearest saved schedule in the column "from"
because the current time is only comparing it to the first added schedule
code:
<?php
//Include the database configuration
include 'config.php';
//Get the data of the selected teacher
$teacher = $dbconnect->prepare("SELECT * FROM time WHERE IMEI = ? AND NFC = ?");
$teacher->bindValue(1,$_GET['IMEI']);
$teacher->bindValue(2,$_GET['NFC']);
$teacher->execute();
//Get the data
$time = $teacher->fetch();
//Store the current time
$current_time = new DateTime();
//Placeholder variables
$time_difference = 0;
$remark = "";
//If there is such a teacher let the teacher enter
if(!empty($time))
{
//Get the time difference
$time_difference = round( (strtotime($current_time->format('H:i:s')) - strtotime($time['From'])) / 60 );
//Check if the professor is late or not
//If the time difference is negative he/she is early
if($time_difference < 0)
{
$remark = 'Sir why are you so early?';
}
//If the prof is on time which is 0 time difference or less than 15 minutes
else if($time_difference == 0 || $time_difference < 15)
{
$remark = "Sir it's a miracle you are on time";
}
//Oh come on why are you so late Sir?Porn marathon at night?
else if($time_difference == 15 || $time_difference > 15)
{
$remark = 'Sir why are you late?';
}
$time_in = $dbconnect->prepare("INSERT INTO time_in (teacher_id,name,NFC,IMEI,time_in,remarks) VALUES (?,?,?,?,?,?)");
$time_in->bindValue(1,$time['teacher_id']);
$time_in->bindValue(2,$time['name']);
$time_in->bindValue(3,$time['NFC']);
$time_in->bindValue(4,$time['IMEI']);
$time_in->bindValue(5,$current_time->format('H:i:s'));
$time_in->bindValue(6,$remark);
$time_in->execute();
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome!</title>
</head>
<body>
<h1>
<?php
//If there is such a teacher,welcome him/her
if(!empty($time))
{
echo 'Welcome '.$time['name'].'! Your NFC is '.$time['NFC'];
echo '</br>';
echo 'Time in at: '.$current_time->format('H:i:s');
//This is just a temporary display to show the time difference for testing.Kindly delete it later
echo '</br>';
echo 'Time difference: '.$time_difference;
//Display remark
echo '</br>';
echo 'Remark:'.$remark;
}
else
{
echo 'You are not registered.';
}
?>
</h1>
</body>
</html>

Related

AUTO Age Script

The issue is the cPanel Error message.
I have a PHP script that auto shows (Sara Smile Is * Years of Age), and I have in (.htaccess) the line (AddType application/x-httpd-php .html), and that runs properly, however cPanel is giving this Error Message: Expected tag name. Got '?' instead. (HTML doesn't support processing instructions).
<?php
$bday = new DateTime('11.4.2010'); // Persons Date of Birth
$today = new Datetime(date('m.d.y'));
$diff = $today->diff($bday);
printf(' %d ', $diff->y, $diff->m, $diff->d);
printf("\n");
?>
Is there a way to auto get (Sara Smile is * Years of Age), Without a cPanel Error message?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<?php
function AutoAge($birthday)
{
$today = new DateTime(date('d-m-Y'));
$bday = new DateTime($birthday); // Persons Date of Birth
$diff = $bday->diff($today);
return $diff->format('%y');
}
?>
<p>some content here</p>
<p>Alice has an age of <?php echo AutoAge("11-4-2000"); ?></p>
<p>other content here</p>
<p>Bob has an age of <?php echo AutoAge("23-9-2004"); ?></p>
</body>
</html>
UPDATE
Here is an Easier JavaScript Code:
function AutoAge(birthYear, birthMonth, birthDay)
{
var birthdate = new Date(birthYear, birthMonth - 1, birthDay);
var today = new Date();
return Math.floor((today.getTime() - birthdate.getTime())
/ 1000 / 60 / 60 / 24 / 365);
}
function showBirthday()
{
var i, elem, items = document.getElementsByClassName('birthday');
for(i=0; i<items.length; i++)
{
elem = items[i];
elem.innerHTML = AutoAge(elem.dataset.year || 2000, elem.dataset.month || 0, elem.dataset.day || 1) + ' years';
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body onload="showBirthday()">
<p>some content here</p>
<p>Alice has an age of <span class="birthday" data-year="2000" data-month="4" data-day="11"></span></p>
<p>other content here</p>
<p>Bob has an age of <span class="birthday" data-year="2004" data-month="9" data-day="23"></span></p>
</body>
</html>
I always use this code to calculate in a elegant way the user age and works fine...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Age Information</title>
</head>
<body>
<?php
function Calc_Age($myday) {
date_default_timezone_set('Europe/Rome');
$birth = new DateTime($myday);
$birth->format('Y-m-d H:i:s');
$today = new DateTime('NOW');
$today->format('Y-m-d H:i:s');
$diffs = $today->diff($birth);
$myage = $diffs->y . ($diffs->y == 1 ? ' year, ' : ' years, ');
$myage .= $diffs->m . ($diffs->m == 1 ? ' month, ' : ' months, ');
$myage .= $diffs->d . ($diffs->d == 1 ? ' day, ' : ' days, ');
$myage .= $diffs->h . ($diffs->h == 1 ? ' hour and ' : ' hours and ');
$myage .= $diffs->i . ($diffs->i == 1 ? ' minute' : ' minutes');
return $myage;
}
?>
<h1>Actual age of Angela</h1>
<p>Angela is <?php echo Calc_Age("1967-01-23 05:00:00"); ?> old</p>
<h1>Actual age of Jhon</h1>
<p>Jhon is <?php echo Calc_Age("1977-04-14 09:10:00"); ?> old</p>
</body>
</html>
Output:
Answer with Only Years
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Age Information</title>
</head>
<body>
<?php
function Calc_Age($myday) {
date_default_timezone_set('Europe/Rome');
$birth = new DateTime($myday);
$birth->format('Y-m-d');
$today = new DateTime('NOW');
$today->format('Y-m-d');
$diffs = $today->diff($birth);
$myage = $diffs->y . ($diffs->y == 1 ? ' year' : ' years');
return $myage;
}
?>
<h1>Actual age of Angela</h1>
<p>Angela is <?php echo Calc_Age("1967-01-23"); ?> old</p>
<h1>Actual age of Jhon</h1>
<p>Jhon is <?php echo Calc_Age("1977-04-14"); ?> old</p>
</body>
</html>
Final Notes
with m-d-Y the php engine cannot calculate the diff dates, to use properly this php class you have to use the standard connotation Y-m-d. This cannot create problem since you know now that you have to call the function with this format
Calc_Age("1967-01-23"); // Y-m-d (Year-month-day)
Hope this help.

Submit form with multiple conditions not working

Situation
I have a submit form. With this form, users are able to reserve a table in a restaurant. With each submit there is some data send to the database with the wp_insert_post function. For example the user_ip and the datum_nieuwe_reservering(explained later). I want to use this to check if the user is allowed to submit a new reservation(prevent spamming). I have tried a couple of thing, but it's not working. See code below.
Question
How can I get this to work? What am I doing wrong?
CODE
reserving.php
<!-- CODE WHEN POST = SUBMIT, MAKE A NEW RESERVATION.
=========================================== -->
<?php
global $wpdb;
$user_ip_db = getUserIP();
$date = date("d-m-Y H:i:s");
//echo $date;
$check_user_ip = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'user_ip_reservering' AND meta_value = '$user_ip_db'");
$check_time_new = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'datum_nieuwe_reservering'");
print_r($check_time_new);
if ( isset($_POST['submit_reservering']) ) {
if ( (empty($check_user_ip)) && $date > $check_time_new ) {
create_reservering();
echo "Your reservation has been send";
} else {
echo 'You have already reserved';
}
}
?><!-- end CODE FOR MAKING RESERVATION-->
Plugin file
//Using this function to send the current day + 1 day to the database
//with a hidden text field named `datum_nieuwe_reservering`(show below)
//and in the end check if current date >= `datum_nieuwe_reservering`
<?php
function getDateTime(){
date_default_timezone_set('Europe/Amsterdam');
$next_day = time() + (1 * 24 * 60 * 60);
$date_now = date('d-m-Y H:i:s');
$date_tomorrow = date('d-m-Y H:i:s', $next_day);
echo $date_tomorrow;
}
?>
Hidden field for datum_nieuwe_reservering
<input type='hidden' value='<?php echo getDateTime(); ?>' name='datum_nieuwe_reservering'>
NOTE: The submit form is working correctly with the if (empty($check_user_ip)) { }; code only. So I'm struggle to add the date in the if statement too.
I hope someone can help me in the right direction!
Thanks in advance.
Have you tried to use brackets in condition?
if ( isset($_POST['submit_reservering']) ) {
if ( (empty($check_user_ip)) && ($date > $check_time_new) ) {
create_reservering();
echo "Your reservation has been send";
} else {
echo 'You have already reserved';
}
}
if (isset($_POST['submit_reservering']) === true) {
$reserved = false;
if (empty($check_user_ip) === true) {
$test = end($check_time_new);
if(date > $test->meta_value) {
create_reservering();
echo "Your reservation has been send";
} else {
echo 'You have already reserved';
}
}
}

How to check past date should not be past from current date in PHP? in this second condition not working

My code is here,I am not able to perfect. when press submit button it check old date correct.. and shows "date must be in future" it correct. and whenever i put future date.. that time not show second alert instead it showing first.
if(isset($_POST['update']))
{
$pdata["card"] = $_POST['smcard'];
$pdata["expiry_date"] = date("Y-m-d" strtotime($_POST['smexdate']));
$dateone = $pdata["expiry_date"];
$nowdate = new DateTime();
if ($dateone < $nowdate)
{
echo "<script>";
echo "alert('date must be in future');";
echo "</script>";
}
elseif ($dateone > $nowdate)
{
echo "<script>";
echo "alert('Okay Good, Your date is in future');";
echo "</script>";
}
update_data('smartcard_data', $pdata, 'promotion_id='.$_REQUEST['promotion_id']);
exit;
}
Try below example to fine date passed or not
$dtA = new DateTime('05/14/2010 3:00PM');
$dtB = new DateTime('05/14/2010 4:00PM');
if ( $dtA > $dtB ) {
echo 'dtA > dtB';
}
else {
echo 'dtA <= dtB';
}

Very strange occurence mysql php blog

Ok so the problem I'm having is very strange. I have a blog website that displays a list of posts, and i made a system to select only 10 posts at a time. and yet on the second generated page 12 results are shown (the last 2 are duplicates)
//removed url because problem solved and i dont want to get sql injected
if you goto my project above and look at the second page of posts 12 entry's are shown with the last 2 being duplicates of the 3rd(and last) page...what is going on?! they should not be able to appear because the sql LIMIT function should restrict the displayed posts to 10.
here is the code for mainpage .php
<?php
session_start();
ob_start();
if ($_SERVER["REQUEST_METHOD"] == "POST"){//this works in my tests.
$low = 0;
$high = 10; //this loop/if method works in conjunction with the code at the bottom of this page
$e = 1;//starts at 1 because 1 itself is defined my default at the bottom of the page
while($_SESSION['i'] != $e){
$e++;
if (isset($_REQUEST["p$e"])){
$u = 1;
while($u != $e){
$u++;
$low = $low + 10;
$high = $high +10;
}
}
}}else{
$low = 0;
$high = 10;
}
?>
<!doctype html>
<!-- AUTHOR:JOSH FAIRBANKS -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<nav>
<ul>
<li><div id = "new">Create new post</div></li>
<li><div id = "veiw">Veiw posts</div></li>
</ul>
</nav>
<main>
<?php
$link = mysqli_connect( 'localhost', 'username', 'password' );
mysqli_select_db( $link, 'mydatabasename' );
$results = mysqli_query( $link, "SELECT LEFT(post, 575) as post FROM Users where verified = 1 ORDER BY `id` DESC LIMIT $low , $high" ); //this displayes all the posts that have been verified
while( $record = mysqli_fetch_assoc( $results ) ) {
$post = $record['post'];
$count = mysqli_affected_rows( $link );
//ORDER BY YEAR(Date) DESC, MONTH(Date) DESC, DAY(DATE) DESC
$post .= "</td></tr></table>";
print $post;
}
$vresults = mysqli_query( $link, "SELECT post FROM Users where verified = 1" );
while( $vrecord = mysqli_fetch_assoc( $vresults ) ) {
$vpost = $vrecord['post'];
$vcount = mysqli_affected_rows( $link );
$_SESSION['vcount'] = $vcount;
//
//these mirror variables arent seen and just are used to count the total amount of posts
//not just the ones on the page
}
mysqli_free_result( $results );
mysqli_close( $link );
?>
<form method = "post" action = "mainPage.php">
<table>
<tr><td>Page Number: </td><!--<td><input type = "submit" name = "p1" value = "1"></td>-->
<?php
$i = 0;
print "displaying low: $low high: $high";
for($j = 0; $j < $vcount; $j++) //modulus
{
if($j % 10 == 0)
{
$i++;
$_SESSION['i'] = $i;
print "<td><input type = 'submit' name ='"."p".$i. "' value = '$i'></td>";
}
}
?>
</tr>
</table>
</form>
</main>
</body>
</html>
I know this code is a bit of a mess :p but i swear it works except for this frustrating issue. any and all help appreciated.
I guess your problem at $high variable... Records per page always constant. But seems that you are increment in one place.
if ($_SERVER["REQUEST_METHOD"] == "POST"){//this works in my tests.
$low = 0;
$high = 10; //this loop/if method works in conjunction with the code at the bottom of this page
$e = 1;//starts at 1 because 1 itself is defined my default at the bottom of the page
while($_SESSION['i'] != $e){
$e++;
if (isset($_REQUEST["p$e"])){
$u = 1;
while($u != $e){
$u++;
$low = $low + 10;
$high = 10;
}
}
}}else{
$low = 0;
$high = 10;
}
?>

PHP controlling the output of rand

Is it possible to control the output of rand, for example if I just want rand to give me the output of the variable $roll1 with the value or number of 1 half the time out of the six possibilities when rand is ran or when the browser is refreshed, how does one accomplish that?
My code sucks but I am fighting to learn, I only get one every now and then, but it's not consistent, I want a 1 every time I refresh the page.
So If I refresh the page 6 times I should get a 1 out of the variable $roll1 three times, and the rest of the values for $roll1 should be random.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>loaded dice</title>
</head>
<body>
<h1>loaded dice</h1>
<h3>loaded dice</h3>
<?php
// loaded dice, should roll the number 1 half the time out of a total of 6.
// So if I refreshed my browser six times I should at least see three 1's for roll1.
$roll1 = rand(1, 6);
// Okay is it possible to divide rand by two or somehow set it up
// so that I get the the value 1 half the time?
// I am trying division here on the if clause in the hopes that I can just have
// 1 half the time, but it's not working,maybe some type of switch might work? :-(.
if ($roll1 == 3) {
$roll1 / 3;
}
if ($roll1 == 6) {
$roll1 / 6;
}
if ($roll1 == 1) {
$roll1 / 1;
}
// This parts works fine :-).
// Normal random roll, is okay.
$roll2 = rand(1, 6);
print <<<HERE
<p>Rolls normal roll:</p>
You rolled a $roll2.
<p>Rolls the number 1 half the time:</p>
<p>You rolled a $roll1.</p>
HERE;
// Notice how we used $roll1 and 2, alongside the HERE doc to echo out a given value.
?>
<p>
Please refresh this page in the browser to roll another die.
</p>
</body>
</html>
You could do something like this
if (rand(0,1))
{
$roll = rand(2,6);
}
else
{
$roll = 1;
}
You can't directly make rand() do that, but you can do something like this:
<?PHP
function roll(){
if(rand(0,1)) //this should evaluate true half the time.
return 1;
return rand(2,6); //the other half of the time we want this.
}
So if you want to guarantee that in the last 6 rolls their would always have been at least 3 ones, I think you would have to track the history of the rolls. Here is a way to do that:
<?php
if (array_key_exists('roll_history', $_GET)) {
$rollHistory = unserialize($_GET['roll_history']);
} else {
$rollHistory = array();
}
$oneCount = 0;
foreach($rollHistory as $roll) {
if ($roll == 1) {
$oneCount++;
}
}
if (6 - count($rollHistory) + $oneCount <= 3) {
$roll = 1;
} else {
if (rand(0,1)) {
$roll = rand(2,6);
} else {
$roll = 1;
}
}
$rollHistory[] = $roll;
if (count($rollHistory) > 5) {
array_shift($rollHistory);
}
echo '<p>Weighted Dice Role: ' . $roll . '</p>';
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="get" >';
echo '<input type="hidden" name="roll_history" value="' . htmlspecialchars(serialize($rollHistory)) . '" />';
echo '<input type="submit" value="Roll Again" name="roll_again" />';
echo '</form>';
Rather than call rand() twice, you can simply do a little extra math.
$roll = $x = rand(1,12)-6 ? $x : 1;
A slightly different solution. It isn't as elegant, but perhaps more conducive to loading the die more finely?
$i = rand(1, 9);
if($i<=3)
{
$num = 1;
}
else $num = $i-2;

Categories