Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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); 
like image 809
EzzDev Avatar asked Dec 29 '09 03:12

EzzDev


People also ask

What is Mt_rand function in PHP?

The mt_rand() function is a drop-in replacement for the older rand(). It uses a random number generator with known characteristics using the » Mersenne Twister, which will produce random numbers four times faster than what the average libc rand() provides.

How does laravel generate random numbers?

You can use the rand() function to generate a random number. You can also create some characters and prefix the rand() with it.


1 Answers

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); 
like image 117
zombat Avatar answered Sep 22 '22 09:09

zombat