[英]24 Hour Date Increments (PHP/Laravel)
我正在創建一個報告,將MySQL數據庫中的數據總計加載到表行中。 每行包含24小時時段(晚上9點到晚上9點)的總計。 我在數據庫中的時間戳設置如下:“ 2018-8-13 22:00:00”。
我正在使用foreach循環加載表,但是我很困惑如何基於24小時時間框架添加新行。
這是到目前為止我正在使用的代碼:
<?php
use App\Http\Controllers\DashController;
use App\Http\Controllers\KYCController;
use App\Http\Controllers\ICOController;
use App\Http\Controllers\JSONController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
$count = 0;
$totalMAF = 0;
$fiatValue = 0;
$percentageRef = 0;
$percentageFiat = 0;
$percentageBTC = 0;
$totalCount = 0;
$totalTotalMAF = 0;
$totalFiatValue = 0;
$totalPercentageRef = 0;
$totalPercentageFiat = 0;
$totalPercentageBTC = 0;
$genesisDate = "2018-8-13 21:00:00";
$currentBTC = JSONController::getURLRequest("https://api.coinmarketcap.com/v2/ticker/1/?convert=USD")->data->quotes->USD->price;
$Fiat2BTC = 0;
$targetAffID = "MAF0c41e768546c28c66a8e2cca89294853";
$affiliates = DB::table('users')->where('referred_by', $targetAffID)->get();
$startDate = 0; // 24 hour start date/time
$stopDate = 0; // 24 hour stop date/time
$dateRange = $startDate . " /<br /> " . $stopDate;
foreach ($affiliates as $affilate) {
$affID = $affilate->id;
$Txs = DB::table('maf_ico')->where('user_id', $affID)->get();
foreach ($Txs as $data) {
if(empty($data)) {
//do nothing
}else{
$count++;
//echo(dd($data));
$totalMAF += abs($data->owedMAF);
$percentageRef = ( 15 / 100 ) * $totalMAF;
$percentageFiat = ( 5 / 100 ) * ($totalMAF * .35);
$percentageBTC = round(( 5 / 100) * (($totalMAF * .35) / $currentBTC), 8);
$fiatValue = "$" . number_format(round(($totalMAF * .35), 2), 2);
}
}
}
?>
<!doctype html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">Date Range</th>
<th scope="col">Confirmed Purchases</th>
<th scope="col">Total MAF Sold</th>
<th scope="col">Fiat Value of Sales</th>
<th scope="col">15% Referral Bonus</th>
<th scope="col">5% of Fiat/BTC</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ $dateRange }}</td>
<td>{{ $count }}</td>
<td>{{ $totalMAF }} MAF</td>
<td>{{ $fiatValue }} USD</td>
<td>{{ $percentageRef }} MAF</td>
<td>{{ "$" . number_format(round(($percentageFiat), 2), 2) . " USD" }}<br />{{ $percentageBTC . " BTC" }}</td>
</tr>
</tbody>
</table>
<hr style="border: thin solid #2d2d2d;" />
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">Todays Date</th>
<th scope="col">Total Confirmed Purchases</th>
<th scope="col">Total MAF Sold</th>
<th scope="col">Total Fiat Value of Sales</th>
<th scope="col">Total 15% Referral Bonus</th>
<th scope="col">Total 5% of Fiat/BTC</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ date('m-d-Y') }}</td>
<td>{{ $count }}</td>
<td>{{ $totalMAF }} MAF</td>
<td>{{ $fiatValue }} USD</td>
<td>{{ $percentageRef }} MAF</td>
<td>{{ "$" . number_format(round(($percentageFiat), 2), 2) . " USD" }}<br />{{ $percentageBTC . " BTC" }}</td>
</tr>
</tbody>
</table>
<hr style="border: thin solid #2d2d2d;" />
<div align="center"><a href="#" class="btn btn-primary btn-lg" role="button" aria-disabled="true">Download Report (CSV)</a></div>
</body>
</html>
我需要能夠從創世時間起每隔24小時添加一個新行。 我想這樣做而不必在數據庫中進一步存儲任何內容。 任何幫助將不勝感激。
也許會給你一個主意。 您將不得不將其合並到您的代碼中。 我只是舉一個例子。 讓我知道這是否不是您的想法。
$date = date('Y-m-d') . ' 21:00:00'; //Just initiates a date so we can go through the hours.
//Does not matter what date it is.
echo
'<table>';
for($i = 0; $i < 24; $i++){
echo
'<tr>' .
'<td>' . date('hA', strtotime($date . ' +' . $i . ' hours')) . '</td>' . //Adds $i hours to $date. Then displays the hour once per row.
'<td>' . $array[$i]['total'] . '</td>' . //Or however you are getting your total.
'</tr>';
}
echo
'</table>';
您可以在Laravel中使用Carbon \\ Carbon https://carbon.nesbot.com/docs/
//Start a carbon object with the genesisDate
$date = \Carbon\Carbon::createFormFormat("Y-m-d H:i:s",$genesisDate);
//For this example lets say the end date is 7 days from the genesisDate
//We use copy to avoid altering the original start date
$endDate = $date->copy()->addDays(7);
while($date <= $endDate){
//Do whatever you need to do with the date
$date->addHour(); //This goes last to increment the date by a full 24 hours
}
您可以在幾分鍾,幾秒鍾或其他任何需要的時間內執行此操作,以上示例是從發生日期/時間開始以及向前7天開始的每一小時。
但是,如果您已經在數據庫/模型中擁有日期/時間,則可以遍歷這些日期/時間,或者獲取最后時間的最后一條記錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.