Thank you for your time.
I am trying to modify an existing PhP script to accept a user input string on line 23. Right now it only accepts numbers put into the array.
<?php
function isJewishLeapYear($year) {
if ($year % 19 == 0 || $year % 19 == 3 || $year % 19 == 6 ||
$year % 19 == 8 || $year % 19 == 11 || $year % 19 == 14 ||
$year % 19 == 17)
return true;
else
return false;
}
function getJewishMonthName($jewishMonth, $jewishYear) {
$jewishMonthNamesLeap = array("Tishri", "Heshvan", "Kislev", "Tevet",
"Shevat", "Adar I", "Adar II", "Nisan",
"Iyar", "Sivan", "Tammuz", "Av", "Elul");
$jewishMonthNamesNonLeap = array("Tishri", "Heshvan", "Kislev", "Tevet",
"Shevat", "", "Adar", "Nisan",
"Iyar", "Sivan", "Tammuz", "Av", "Elul");
if (isJewishLeapYear($jewishYear))
return $jewishMonthNamesLeap[$jewishMonth-1];
else
return $jewishMonthNamesNonLeap[$jewishMonth-1];
}
$jdNumber = gregoriantojd(12, 12, 2016);
$jewishDate = jdtojewish($jdNumber);
list($jewishMonth, $jewishDay, $jewishYear) = explode('/', $jewishDate);
$jewishMonthName = getJewishMonthName($jewishMonth, $jewishYear);
echo "<p>The Jewish date of death is $jewishDay $jewishMonthName $jewishYear</p>\n";
?>
What I would LIKE is the line
$jdNumber = gregoriantojd(12, 12, 2016);
to accept instead of specific numbers, USER INPUT. I was thinking that you could use the $userinput, but that threw an error of expected 3 strings, got one.
Again this is not my forte, but I am thrown into the mix as back-end for a project. I do not expect code written for me, just nudges in the right direction. Thank you.
There are lots of ways to get user input, but I really like jQuery-UI's datepicker. And if it is set, then you can parse the date with the DateTime class and format its output as inputs to gregoriantojd
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#datepicker" ).datepicker();
} );
</script>
</head>
<body>
<form>
<p>Date: <input type="text" name="datepicker" id="datepicker"></p>
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>
<?php
function isJewishLeapYear($year) {
if ($year % 19 == 0 || $year % 19 == 3 || $year % 19 == 6 ||
$year % 19 == 8 || $year % 19 == 11 || $year % 19 == 14 ||
$year % 19 == 17)
return true;
else
return false;
}
function getJewishMonthName($jewishMonth, $jewishYear) {
$jewishMonthNamesLeap = array("Tishri", "Heshvan", "Kislev", "Tevet",
"Shevat", "Adar I", "Adar II", "Nisan",
"Iyar", "Sivan", "Tammuz", "Av", "Elul");
$jewishMonthNamesNonLeap = array("Tishri", "Heshvan", "Kislev", "Tevet",
"Shevat", "", "Adar", "Nisan",
"Iyar", "Sivan", "Tammuz", "Av", "Elul");
if (isJewishLeapYear($jewishYear))
return $jewishMonthNamesLeap[$jewishMonth-1];
else
return $jewishMonthNamesNonLeap[$jewishMonth-1];
}
if(
isset($_GET['datepicker']) &&
$datetime = DateTime::createFromFormat('m/d/Y', $_GET['datepicker'])
){
$jdNumber = gregoriantojd($datetime->format('m'), $datetime->format('d'), $datetime->format('Y'));
$jewishDate = jdtojewish($jdNumber);
list($jewishMonth, $jewishDay, $jewishYear) = explode('/', $jewishDate);
$jewishMonthName = getJewishMonthName($jewishMonth, $jewishYear);
echo "<p>The Jewish date of death is $jewishDay $jewishMonthName $jewishYear</p>\n";
}
There are two ways. They both require that you convert your $userinput variable into an array.
if: $userinput = "12 6 2016";
You can do:
$input = explode($userinput, ' ');
$jdNumber = gregoriantojd($input[0], $input[1], $input[2]);
The other way is to pass the array as a list of inputs:
$jdNumber = call_user_func_array('gregoriantojd', $input);
Not tested, but should work.
Related
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.
I just started learning PHP # school and for the third assignment we have to create a TicTacToe game. I have followed a turorial video on youtube and have made a game that is playable at the moment. But it doesnt know whos turn it is. IE: you can keep pressing the submit button and the computer will keep filling in O's without the player to have to fill in a X.
Please can someone explain how i can make the script know who's turn it is?
I want to know the logic behind it. So only a code wont help me at all, please explain how you check if the player has filled in a X before you switch turn for instance.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Boter, Kaas & Eieren</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
$winner = 'niemand';
$box = array('','','','','','','','','');
if (isset($_POST["submit"])){ //When the player hits submit, we retrieve the data
$box[0] = $_POST['box0'];
$box[1] = $_POST['box1'];
$box[2] = $_POST['box2'];
$box[3] = $_POST['box3'];
$box[4] = $_POST['box4'];
$box[5] = $_POST['box5'];
$box[6] = $_POST['box6'];
$box[7] = $_POST['box7'];
$box[8] = $_POST['box8'];
//print_r($box); //kijken in welke array, wat is ingevuld
//check if the player has won
if (($box[0]=='x' && $box[1]=='x' && $box[2]=='x') ||
($box[3]=='x' && $box[4]=='x' && $box[5]=='x') ||
($box[6]=='x' && $box[7]=='x' && $box[8]=='x') ||
($box[0]=='x' && $box[4]=='x' && $box[8]=='x') ||
($box[2]=='x' && $box[4]=='x' && $box[6]=='x') ||
($box[0]=='x' && $box[3]=='x' && $box[6]=='x') ||
($box[1]=='x' && $box[4]=='x' && $box[7]=='x') ||
($box[2]=='x' && $box[5]=='x' && $box[8]=='x')){
$winner = 'x';
echo "Speler wint";
}
//check if X has played and switch turn to O
$blank = 0; //assume there is no empty box
//check for an empty box
for ($i=0; $i<=8; $i++){
if ($box[$i]==''){
$blank=1;
}
}
//if there is an empty box and no winner yet its O's turn
if ($blank == 1 && $winner == 'niemand'){
$i = rand(0,8);
while ($box[$i]!=''){ //keep looking for an empty box if $i isnt empty
$i = rand(0,8);
}
$box[$i] = "o";
//check if O has won
if (($box[0]=='o' && $box[1]=='o' && $box[2]=='o') ||
($box[3]=='o' && $box[4]=='o' && $box[5]=='o') ||
($box[6]=='o' && $box[7]=='o' && $box[8]=='o') ||
($box[0]=='o' && $box[4]=='o' && $box[8]=='o') ||
($box[2]=='o' && $box[4]=='o' && $box[6]=='o') ||
($box[0]=='o' && $box[3]=='o' && $box[6]=='o') ||
($box[1]=='o' && $box[4]=='o' && $box[7]=='o') ||
($box[2]=='o' && $box[5]=='o' && $box[8]=='o')){
$winner = "o";
echo "KI wint";
}
}
//check if it is a draw
if ($blank == 0 && $winner == 'niemand'){
echo "Gelijkspel!";
}
}
?>
<div id="beurt">
<p>
<form action="destroy.php" method="get">
<input type="submit" id="destroy" onClick="windows.location.href'index.php'" value="Begin opnieuw!">
</form>
</p>
</div>
<form id="game" name="tictactoe" method="post">
<?php
//create the grid to play
for ($i=0; $i<=8; $i++){
echo "<input class=\"box\" type=\"text\" name=\"box$i\" value=\"$box[$i]\">";
if ($i==2||$i==5||$i==8){ //put in a break if $i is 2,5 or 8
echo "<br>";
}
}
if ($winner == 'niemand'){
echo "<br><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Spelen!\"><br></form>";
}
?>
</body>
</html>
Please help me out.
You can add a new variable $_POST['player'] which you can change from 0 to 1 or from 1 to 0 and that way know by the value which player should play now.
I'm not going to write you the code since I gave you a hint how it should be done and you have to learn by yourself :)
I would add a small txt file to save how was the last one to play.
For instance you could save many diffrent values into a single .txt file like this.
<?php
function savesettings($nextturn){
if(is_file("file.txt")){
$mysettings = unserialize(file_get_contents("file.txt"));
}else{
$mysettings = array();}
$mysettings["nextturn"]=$nextturn;
file_put_contents("file.txt",serialize($mysettings));
}
function loadsettings(){
if(is_file("file.txt")){
$mysettings = unserialize(file_get_contents("file.txt"));
}else{
$mysettings = array();
}
return $mysettings["nextturn"];
}
?>
You can use the following code like this:
//Save name of next player
savesettings("David");
// Load the player setting
$player = loadsettings();
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>
I'm aware there are many similar questions on this forum, but it's the 2nd day that I'm going through the answers and nothing seems to work.
It's my first week of PHP learning, so please try to answer in a simple way :) So:
I'm creating a conjugator and so far it's going well, but only if I don't use special characters. As Polish verbs are all about special characters, I'm stuck.
This code works (the conjugation is visible on the screen after pressing "submit"):
Page 1:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Conjugator</title>
</head>
<body>
<form action="new.php" accept-charset="UTF-8" method="post" >
Conjugate: <input type="text" name="verb"><br>
<input type="submit">
</form>
</body>
</html>
Page 2:
<?php
header('Content-Type: text/html; charset="UTF-8"');
$verb = $_POST['verb'];
$last2 = substr ($verb, -2);
$last3 = substr($verb, -3);
$last4 = substr($verb, -4);
$root2 = str_replace($last2, "", $verb);
$root3 = str_replace($last3, "", $verb);
$root4 = str_replace($last4, "", $verb);
$nic = array("nie", "nisz", "ni", "nimy", "nicie", "nia" );
$gnic = array("gnije", "gnijesz", "gnije", "gnijemy", "gnijecie", "gnija");
$ac = array("am", "asz", "a", "amy", "acie", "aja");
if ($last3 == "nic" && $last4 != "gnic") {
foreach ($nic as $one) {
echo "<li>$root3$one</li>";
}
}
elseif ($last4 == "gnic") {
foreach ($gnic as $one) {
echo "<li>$root4$one</li>";
}
}
elseif ($last2 == "ac") {
foreach ($ac as $one) {
echo "<li>$root2$one</li>";
}
}
?>
But if I write for example:
$nic = array("nię", "nisz", "ni", "nimy", "nicie", "nią" );
and then:
if ($last3 == "nić" && $last4 != "gnic")
no result shows up.
For checking, try with verbs like "pienić" or "lśnić" (they don't work) or write them without the special characters ("pienic", "lsnic") - the first code will work.
Help will be deeply appreciated!!
You probably can't use substr() with UTF-8. It thinks in terms of bytes, not characters. Take a look at mb_substr().
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;