【发布时间】:2021-04-27 22:47:27
【问题描述】:
我正在尝试添加迄今为止的几周,两者都将从数据库中获取,我成功地获取了所需的两个,但我不知道如何进行计算。到目前为止,我尝试使用“strtotime”,但它给了我一些奇怪的结果
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT startdate // <- DATE WE WANT TO ADD WEEKS TO
FROM database
ORDER BY id
DESC limit 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$recoredDate = $row["startdate"];
$sql2 = "SELECT ready // <- INT NUMBER REPRESENT NUMBER OF WEEKS
FROM crop
ORDER BY id
DESC limit 1";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
// output data of each row
while($row = $result2->fetch_assoc()) {
$weeks = $row["ready"];
}
} else {
echo "No Result";
}
?>
<p>
<div align=center>
<?php echo "$recoredDate";?> - Date from DB
<p>
<?php echo "$weeks";?> Weeks to add
<p>
<?php echo "$recoredDate",strtotime ("+ $weeks week");?> Wierd result
</div>
</p>
<?php
}
}
?>
我该如何以正确的方式做到这一点?期望的结果应该是打印 $recoredDate + $weeks。
【问题讨论】: