24小时日期增量(PHP / Laravel)

时间:2018-08-13 05:32:14

标签: php html laravel bootstrap-4

我正在创建一个报告,该报告将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小时添加一个新行。我想这样做而不必在数据库中进一步存储任何内容。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

也许会给您一个想法。您将不得不将其合并到您的代码中。我只是举一个例子。让我知道这是否不是您的想法。

$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>';

答案 1 :(得分:0)

您可以在Laravel https://carbon.nesbot.com/docs/中使用Carbon \ Carbon

//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天开始的每一小时。

但是,如果您已经在数据库/模型中拥有日期/时间,则可以遍历这些日期/时间,或者获取结束时间的最后一条记录。