[英]MySQL & PHP : Time() to Decimal Hour
我的 MySQL 數據庫中有兩個 Time() 保存:開始和結束。
我想在我的 PHP 代碼中獲得兩次間隔的十進制結果。
for exemple, if :
start = 15:00:00
end = 16:00:00
result = 1
and if
start = 15:30:00
end = 16:00:00
result = 0,5
是否可以使用 MySQL 獲得此結果? 如果不可能,我該如何將時間格式轉換為十進制間隔?
非常感謝您閱讀我的問題。
非常簡單的解決方案是取這兩個值,將它們轉換為時間,然后將其轉換為小時。
$start = "15:00:00";
$end = "16:00:00";
$result = (strtotime($end) - strtotime($start))/3600;
這給你$result = 1
對於值15:30:00
和16:00:00
$result
將為0.5
。
好的,
所以我認為這很容易。 只需按照以下步驟操作:
您需要將這些數字分開。 制作3個變量。 在這個例子中,我們將使用 $hours、$minutes、$seconds(您可以通過使用 REGEX 分隔主要時間來實現這些。我建議先閱讀有關如何做的內容)
現在你有了這些數字,例如像這樣:
$start = "15:30:00"; $start_hours = "15" $start_minutes = "30"; //或只是 0 $start_seconds = "00"; //或只是0
$end = "16:00:00"; $end_hours = "15" $end_minutes = "00"; //或只是 0 $end_seconds = "00"; //或只是0
所以......這是你需要如何分離這些數字的簡要說明。
現在關於計數...
$result_hours = $end_hours - $start_hours //你得到 1
所以小時很容易......對於幾分鍾,我們需要自己考慮時間。 因為時間值總是在 60 中,我們需要在 100 中的“百分比”。我們將解決這個問題:
$result_minutes = $end_minutes + $start_minutes; //you get 30.. which obviously is NOT what we want.. easily fixed by://
$right_minutes = 100 / 60 * $result_minutes; // gives you 50, which is great.
到目前為止,我們有 1,50(如果你付出足夠的努力,你可以簡單地將 50 除以 10 ......你會得到 5)
$result_seconds = $end_seconds + $start_seconds;
$right_seconds = 100 / 60 * $result_seconds;
現在我們需要意識到我們正在處理秒,所以數字將是分鍾而不是小時的百分比,我們可以使用以下方法解決這個問題:
$best_seconds = 100 / 60 * (0.$right_seconds) //now we have all right.
$right = $right_minutes + $best_seconds;
$result = $result_hours.",".$right;
…………………………………………………………………………………………………………………………………………
應該就是這樣。 希望它能幫助您找出最佳解決方案,這就是我認為它可能起作用的方式。
嘗試。 改變。 嘗試。 並做了。
祝你好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.