Search code examples
phpdatedatetimetimestamp

How to generate random date between two dates using php?


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 answer for Java not php

for example :

$string = randomdate(1262055681,1262055681);

Solution

  • 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);