首页 > 解决方案 > PHP | 随机顺序包括文件

问题描述

我在代码中包含文件。

逻辑是这些文件包含用户必须查看的信息(通信),这 20 个文件的目标是它们应该以随机顺序显示给用户。

现在代码计算用户注册日期并根据已经过去的天数显示用户必须看到的文件

<?php
// Declare and define two dates
$date1 = strtotime($data_registrazione);
$date2 = strtotime(date('Y-m-d'));

// Formulate the Difference between two dates
$diff = abs($date2 - $date1);


// To get the year divide the resultant date into
// total seconds in a year (365*60*60*24)
$years = floor($diff / (365*60*60*24));


// To get the month, subtract it with years and
// divide the resultant date into
// total seconds in a month (30*60*60*24)
$months = floor(($diff - $years * 365*60*60*24)
    / (30*60*60*24));


// To get the day, subtract it with years and
// months and divide the resultant date into
// total seconds in a days (60*60*24)
$days = floor(($diff - $years * 365*60*60*24 -
        $months*30*60*60*24)/ (60*60*24));

?>
<?php if ($days == 0) :?>
    <?php include 'referrer_pro/giorno_zero.php'; ?>
<?php elseif ($days == 1) :?>
    <?php include 'referrer_pro/giorno_uno.php'; ?>
<?php elseif($days == 2) : ?>
    <?php include 'referrer_pro/giorno_due.php'; ?>
<?php elseif($days == 3) : ?>
    <?php include 'referrer_pro/giorno_tre.php'; ?>
<?php elseif($days == 4) : ?>
    <?php include 'referrer_pro/giorno_quattro.php'; ?>
<?php elseif($days == 5) : ?>
    <?php include 'referrer_pro/giorno_cinque.php'; ?>
<?php elseif($days == 6) : ?>
    <?php include 'referrer_pro/giorno_sei.php'; ?>
<?php elseif($days == 7) : ?>
    <?php include 'referrer_pro/giorno_sette.php'; ?>
<?php elseif($days == 8) : ?>
    <?php include 'referrer_pro/giorno_otto.php'; ?>
<?php elseif($days == 9) : ?>
    <?php include 'referrer_pro/giorno_nove.php'; ?>
<?php elseif($days == 10) : ?>
    <?php include 'referrer_pro/giorno_dieci.php'; ?>
<?php elseif($days == 11) : ?>
    <?php include 'referrer_pro/giorno_undici.php'; ?>
<?php elseif($days == 12) : ?>
    <?php include 'referrer_pro/giorno_dodici.php'; ?>
<?php elseif($days == 13) : ?>
    <?php include 'referrer_pro/giorno_tredici.php'; ?>
<?php elseif($days == 14) : ?>
    <?php include 'referrer_pro/giorno_quattoridic.php'; ?>
<?php elseif($days == 15) : ?>
    <?php include 'referrer_pro/giorno_quindici.php'; ?>
<?php elseif($days == 16) : ?>
    <?php include 'referrer_pro/giorno_sedici.php'; ?>
<?php elseif($days == 17) : ?>
    <?php include 'referrer_pro/giorno_diciassette.php'; ?>
<?php elseif($days == 18) : ?>
    <?php include 'referrer_pro/giorno_diciotto.php'; ?>
<?php elseif($days == 19) : ?>
    <?php include 'referrer_pro/giorno_diciannove.php'; ?>
<?php elseif($days == 20) : ?>
    <?php include 'referrer_pro/giorno_venti.php'; ?>
<?php endif; ?>

我怎样才能改变结构不再显示基于过去几天的不同文件。

但它是否以随机顺序显示文件,而不管过去的日子如何?

标签: php

解决方案


要计算您所说的大致随机数,但这将在注册日期后的任意天数内有效,您需要做的是获取天数差的模 20。

diff calc 也可以简化为

# change the format to match how your date is being presented to this code
$regDate = (new DateTime())->createFromFormat('Y-m-d', '2000-11-16');
$diff = $regDate->diff(new DateTime());
$days = $diff->days % 20;
echo "Random_ish number less than or equal to 20 = $days";

所以你可以$days像现在一样使用。


推荐阅读