douluo3256 2011-05-24 02:01
浏览 22
已采纳

PHP比赛,试图在两个给定日期之间选择获胜者日期/时间

In an attempt to build a flash-based contest game that ties into PHP/MySQL for tracking behind the scenes, I aim to eventually end up with a set date range (ie., between YYYY-MM-DD and YYYY-MM-DD) and pick a random datetime (or two, or three) within the given range, and designate those as winning times. I'm not really sure the best way to go about this, any ideas?

I'm rather new with time + rand() and not sure the best way to go about this. Any advice or helpful snippets that seem to make sense for this application would be grand :D

  • 写回答

1条回答 默认 最新

  • doujing5726 2011-05-24 02:10
    关注
    $start = '2011-05-12';
    $end   = '2011-05-30';
    
    $start = strtotime($start);
    $end   = strtotime($end);
    
    $winningTimes = array();
    for ($i = 1; $i <= 3; $i++) {
        $winningTimes[] = mt_rand($start, $end);
    }
    

    This gives you three timestamps within the given range. Timestamps are merely seconds since 1970, so they're simple sequential numbers. Just pick a random number within the start-end range using mt_rand. Convert timestamps back to human readable dates using, for example,
    date('Y-m-d', $winningTimes[0]).


    To make sure you get unique days, do something like this:

    $start = strtotime('2011-05-12');
    $end   = strtotime('2011-05-30');
    $days  = array();
    
    // populate array with all possible days
    for ($i = $start; $i <= $end; $i = strtotime('+1 day', $i)) {
        $days[] = date('Y-m-d', $i);
    }
    
    shuffle($days); // randomize
    $days = array_slice($days, 0, 3); // take first 3
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?