修改现有脚本以接受其他参数

时间:2016-12-06 20:59:21

标签: php

感谢您的时间。

我正在尝试修改现有的PhP脚本以接受第23行的用户输入字符串。现在它只接受放入数组的数字。

 <?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";
 ?>

我喜欢的是行

 $jdNumber = gregoriantojd(12, 12, 2016);

接受而不是具体的数字,USER INPUT。我以为你可以使用$ userinput,但是它会产生预期3个字符串的错误,得到一个。

同样,这不是我的强项,但我被投入混合作为项目的后端。我不希望为我编写代码,只是在正确的方向上轻推。谢谢。

2 个答案:

答案 0 :(得分:0)

有很多方法可以获得用户输入,但我真的很喜欢jQuery-UI's datepicker。如果设置了,那么您可以使用the DateTime classformat作为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";
}

答案 1 :(得分:0)

有两种方法。它们都要求您将$ userinput变量转换为数组。

if:$userinput = "12 6 2016";

你可以这样做:

$input = explode($userinput, ' ');
$jdNumber = gregoriantojd($input[0], $input[1], $input[2]);

另一种方法是将数组作为输入列表传递:

$jdNumber = call_user_func_array('gregoriantojd', $input);

未经测试,但应该可以使用。