【发布时间】:2022-01-25 21:06:23
【问题描述】:
我正在尝试编写一个代码,该代码应该为一个帐户存储一定天数。然后,该帐户的用户应该能够使用 html 选择特定的时间范围。然后应该从总金额中减去这些天数,并将新值放回文本文件中。
到目前为止,我已经尝试通过将 txt 文件读入数组并从那里工作来解决很多问题,但这就是我遇到死胡同的地方,因为我无法找到解决问题的方法.
我现在的工作方式是这样的:
if(isset($_POST['submit_btn'])) //If the submit button is pressed, it starts working through the steps
{
$start1 = date_create($_POST["start"]); //I convert the HTML dates to php dates so i can work with them in a second
$end1 = date_create($_POST["end"]);
$start2 = date_format($start1, "Y-m-d");
$end2 = date_format($end1, "Y-m-d");
$end = $end2;
$start = $start2;
$d_diff1 = date_diff(date_create($end), date_create($start)); //I create the date difference and convert them back into a value I can work with
$d_diff = $d_diff1->format('%d days');
$up = fopen('text\urlaub.txt', 'a+');
if($d_diff < 0) //The next few lines are technically irrelevant for the problem
{
echo "Ihr Startdatum muss vor dem Enddatum liegen.";
}
elseif($d_diff == 0)
{
echo "Sie können heute keinen Urlaub mehr legen.";
}
elseif($d_diff >= 35)
{
echo "Ihr Antrag wurde gestellt. Aufgrund der Länge dea Beantragten Urlaubs muss dieser erst manuell bestätigt werden. Sie werden innerhalb von 2 Werktagen benachrichtigt.<br>";
}
elseif($d_diff >= 1 && $d_diff <= 35) //Here is where the problem starts
{
echo "Ihr Antrag wurde eingereicht und von System freigegeben. Sollte es Probleme mit ihrem Antrag geben wird sich in den nächsten 2 Tagen ein Mitarbeiter an sie wenden. <br>";
$urlaubkey = str_word_count(file_get_contents('text\urlaub.txt'),1,'üöä1234567890-.:;_<>|@€!§%&/=?'); //I read the txt file into the array
}
$key = array_search($name, $urlaubkey); //I check for the username in the array. If I find it, the array key for the username is saved as a variable
echo $key; //Next step would be to increase the value of this by 1 to get the correct key. Then i'd have to overwrite the txt file.
这基本上就是我现在所处的位置。唯一缺少的其他事情是我将数组键加一并运行简单的减法,但这不是真正的问题
我的txt文件保存值的方式是这样的:
körner,26
werner,26
albert,30
wernher,26
Walther,34
它遵循'用户名','剩余天数'的方案
任何帮助将不胜感激
【问题讨论】:
-
是否需要遵循文件中数据的给定格式?或者是否也可以使用另一种数据存储格式,例如 JSON?
-
如果我可以选择保留为 txt。
标签: php