How to generate random date between two dates using php?

前端 未结 13 1900
甜味超标
甜味超标 2020-11-27 15:55

I am coding an application where i need to assign random date between two fixed timestamps

how i can achieve this using php i\'ve searched first but only found the a

相关标签:
13条回答
  • 2020-11-27 16:20

    An other solution where we can use date_format :

     /**
     * Method to generate random date between two dates
     * @param $sStartDate
     * @param $sEndDate
     * @param string $sFormat
     * @return bool|string
     */
    
    function randomDate($sStartDate, $sEndDate, $sFormat = 'Y-m-d H:i:s') {
        // Convert the supplied date to timestamp
        $fMin = strtotime($sStartDate);
        $fMax = strtotime($sEndDate);
        // Generate a random number from the start and end dates
        $fVal = mt_rand($fMin, $fMax);
        // Convert back to the specified date format
        return date($sFormat, $fVal);
    }
    

    Source : https://gist.github.com/samcrosoft/6550473

    You could use for example :

    $date_random = randomDate('2018-07-09 00:00:00','2018-08-27 00:00:00');
    
    0 讨论(0)
  • 2020-11-27 16:22

    You can just use a random number to determine a random date. Get a random number between 0 and number of days between the dates. Then just add that number to the first date.

    For example, to get a date a random numbers days between now and 30 days out.

    echo date('Y-m-d', strtotime( '+'.mt_rand(0,30).' days'));
    
    0 讨论(0)
  • 2020-11-27 16:24

    PHP has the rand() function:

    $int= rand(1262055681,1262055681);
    

    It also has mt_rand(), which is generally purported to have better randomness in the results:

    $int= mt_rand(1262055681,1262055681);
    

    To turn a timestamp into a string, you can use date(), ie:

    $string = date("Y-m-d H:i:s",$int);
    
    0 讨论(0)
  • 2020-11-27 16:27

    Here's another example:

    $datestart = strtotime('2009-12-10');//you can change it to your timestamp;
    $dateend = strtotime('2009-12-31');//you can change it to your timestamp;
    
    $daystep = 86400;
    
    $datebetween = abs(($dateend - $datestart) / $daystep);
    
    $randomday = rand(0, $datebetween);
    
    echo "\$randomday: $randomday\n";
    
    echo date("Y-m-d", $datestart + ($randomday * $daystep)) . "\n";
    
    0 讨论(0)
  • 2020-11-27 16:27

    Simplest of all, this small function works for me I wrote it in a helper class datetime as a static method

    /**
     * Return date between two dates
     *
     * @param String $startDate
     * @param String $endDate
     * @return String
     *
     * @author Kuldeep Dangi <kuldeepamy@gmail.com>
     */
    public static function getRandomDateTime($startDate, $endDate)
    {
        $randomTime = mt_rand(strtotime($startDate), strtotime($endDate));
        return date(self::DATETIME_FORMAT_MYSQL, $randomTime);
    
    }
    
    0 讨论(0)
  • 2020-11-27 16:28

    By using carbon and php rand between two dates

    $startDate = Carbon::now();
    $endDate   = Carbon::now()->subDays(7);
    
    $randomDate = Carbon::createFromTimestamp(rand($endDate->timestamp, $startDate->timestamp))->format('Y-m-d');
    

    OR

    $randomDate = Carbon::now()->subDays(rand(0, 7))->format('Y-m-d');
    
    0 讨论(0)
提交回复
热议问题