你好:)我一直在努力工作在這塊PHP腳本上,選擇符合某些條件的所有DateTime行,然後計算從那時起的時間差到當前時間。我的循環將無法使用PHP(日期時間轉換)
我的PHP腳本只是回聲「44年前」。當我在數據庫中有更多的記錄時,回聲是完全錯誤的,我在數據庫中沒有記錄舊的記錄。
我知道我可以在選擇循環內回顯合適的日期時間,選擇標準確實有效。
一些幫助表示讚賞。 :)
我的PHP代碼如下:
<?php
//AJAX REQUEST
$Following_ID = $_GET['Following_ID'];
//connection infi
$con=mysqli_connect("localhost","root","PASS","TABLE");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//SQL to select all fields from appointment table
$result = mysqli_query($con,"SELECT * FROM Followers
WHERE Following_ID='$Following_ID' AND Follow_Status='acce' AND Followers_Requested_Game NOT LIKE '%$Following_ID%' AND (Follower_Game='truth' OR Follower_Game='rate') ORDER BY Game_Time DESC");
//echo all in array
while($row = mysqli_fetch_array($result))
{
$time = $row['Game_Time'];
humanTiming ($time);
echo 'event happened '.humanTiming($time).' ago';
}
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time/$unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
}
//close connection
mysqli_close($con);
?>
我這裏有解決方案:
<?php
//AJAX REQUEST
$Following_ID = $_GET['Following_ID'];
//connection infi
$con=mysqli_connect("localhost","root","PASS","TABLE");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//SQL to select all fields from appointment table
$result = mysqli_query($con,"SELECT * FROM Followers
WHERE Following_ID='$Following_ID' AND Follow_Status='acce' AND Followers_Requested_Game NOT LIKE '%$Following_ID%' AND (Follower_Game='truth' OR Follower_Game='rate') ORDER BY Game_Time DESC");
//echo all in array
while($row = mysqli_fetch_array($result))
{
$times = $row['Game_Time'];
$time = strtotime($times);
humanTiming ($time);
echo 'event happened '.humanTiming($time).' ago';
}
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time/$unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
//close connection
mysqli_close($con);
?>
此代碼不可能作爲-writetein工作。你正在定義一個函數INSIDE while循環,這會導致它在第二次迭代時死於「無法重新聲明函數」的錯誤。 –
我認爲這也可能是一個問題,我可以用函數外的函數調用函數內的函數? – user2921824
是的。那可以正常工作。如果你正在重新整理一些東西,你可能需要在有人給你的服務器執行任務之前,修復你的漏洞廣泛的[SQL注入攻擊](http://bobby-tables.com)漏洞。 –