dongmie3987067 2018-12-10 17:09
浏览 42

如何显示/隐藏横幅,基于它的PHP中的from_date和end_date?

I'm trying to work on an index page, and to implement minor modifications to the said page. Currently I do have a banner management section within the admin panel where I'm able to upload an image along with a title. Thus, my index page will show all entered banners as a slide show.

However, I've been trying to make it abit more organized. For that I edited the database and added fields such as banner_id, banner name, Fromdate, EndDate and bannerimage. However instead of showing the entire banner content, I want to show banner image based on the date and time which I added into the database.

is there any way to sort this filtering method? I'm trying to learn and improvise on my own, but then again due to the fact that I'm still a beginner and I do know the logical application I'm failing to implement it code wise.

----This is the current code that I have to handle the slide sow---

$image_dir = "$_SERVER[DOCUMENT_ROOT]/images/img"; // directory on server
$image_relative_path = 'images/img'; // path to images relative to script
$file_types = array('jpg', 'jpeg', 'gif', 'png');
$image_time = '10000'; // seconds each image will display (4000 = 4 seconds)
$image_rotation ='';

if ($handle = opendir($image_dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $ext_bits = explode(".", $file); // finds file extensions
            foreach ($ext_bits as $key => $value) {
                if (in_array($value, $file_types)) { 
                    $image_rotation .= '<div data-p="170.00"><img data-u="image" src="'.$image_relative_path.'/' . $file . '"/></div>';
                }
            }
        }
    }
    closedir($handle);
}

?>

<div class="container">
<div id="jssor_1" style="position:relative;margin:0 auto;top:0px;left:0px;width:1300px;height:500px;overflow:hidden;visibility:hidden;">
    <!-- Loading Screen -->
    <div data-u="loading" class="jssorl-009-spin" style="position:absolute;top:0px;left:0px;width:100%;height:100%;text-align:center;background-color:rgba(0,0,0,0.7);">
        <img style="margin-top:-19px;position:relative;top:50%;width:38px;height:38px;" src="images/spin.svg" />
    </div>
    <div data-u="slides" style="cursor:default;position:relative;top:0px;left:0px;width:1300px;height:500px;overflow:hidden;">
        <?php echo $image_rotation; ?>
    </div>

----The above code works fine, but it doesn't give me the privilege to show/hide banner based on the dates entered into the database.---

So, Currently I am uploading each banner as banner name, banner image, Date & time to start showing banner, end date and end time for banner. (However, I'm using a datepicker that accepts the date and time within a single package.

So, let's say I upload 10 banner, I don't want it to be shown on the index page right away, I want it to be displayed within the dates as per the database

  • 写回答

1条回答 默认 最新

  • dongxiane0395 2018-12-11 08:42
    关注

    As this is tagged with PHP I'll provide a PHP answer.

    With PHP you can compare DateTime by using a variety of comparison operators.

    For your case, it's best suited to use <= (less than or equal to) and >= (greater than or equal to).

    Your logic will essentially be:

    if date is greater than or equal to DateA and is less than or equal to DateB - show banner.

    <?php
        $today = date('Y-m-d H:i:s');
    
        $dateA = date('Y-m-d H:i:s', strtotime('13 Nov 2018'));
        $dateB = date('Y-m-d H:i:s', strtotime('21 Dec 2018'));
    
        echo $today. '<br />';
        echo $dateA. '<br />';
        echo $dateB. '<br />';
    
        if ($today >= $dateA && $today <= $dateB) {
            echo 'show banner';
        }
    

    Here we make use of PHPs in-built functions date and strtotime to declare today, DateA (the start of the range) and DateB (the end of the range). I used a range I know would display show banner just for examples sake.

    Note: This isn't a copy-paste solution, this just shows you an answer to your problem for you to edit/tweak to suit your code :)

    评论

报告相同问题?

悬赏问题

  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题