How can I generate a random time per day with php?

how to generate unique random number in php
php random number
generate 8 digit unique number in php
php random based on time
generate random number in php without repetition
php date
how to create auto generated number in php
random number generator in php mysql

I want to generate one random time within a 24 hour period using php. Here is what I have so far:

srand(mktime(0, 0, 0));
$time = rand( 0, time() );
echo date("H:i", $time);

It seemed to work at first but after a couple hours the time changed slightly. I got 16:41 first and then it jumped to 16:47 or something near that. Any help with this would be greatly appreciated.

How about simply do this,

echo rand(0,23).":".str_pad(rand(0,59), 2, "0", STR_PAD_LEFT);

more better with mt_rand,

echo mt_rand(0,23).":".str_pad(mt_rand(0,59), 2, "0", STR_PAD_LEFT);

DEMO.

Generate a random date with PHP., Generating a random date / timestamp with PHP. the timestamp “2147385600”, simply because this is the day before the Unix Time Stamp is expected to stop  To generate a random number which is different every day, I used the number of days after unix epoch as a seed: <?php srand ( floor ( time () / ( 60 * 60 * 24 )));

Your code will generate every time a new random time when your web page get loaded. You should create a cookie for one day. It will destroy automatically after one day. You should change your code like

if(!isset($_COOKIE["one_day_cookie"])){
 srand(mktime(0, 0, 0));
 $time = rand( 0, time() );
 setcookie("one_day_cookie", $time, time()+(3600*24*1));  /* expire in 1 day */

}else{
 $time = $_COOKIE["one_day_cookie"];
}

echo date("H:i", $time);

PHP random time() function [SOLVED], you can also use time to create a random time stamp, but what you posted e this feature would only be enabled on a certain day of the week,  The uniqid() function generates a unique ID based on the microtime (the current time in microseconds). Note: The generated ID from this function does not guarantee uniqueness of the return value! To generate an extremely difficult to predict ID, use the md5() function.

How about

$unix_start = strtotime("2017-10-10 12:25:16");
$unix_end = strtotime("2017-10-11 11:48:06");
$diff = $unix_end - $unix_start ;
$rndtime =  $unix_start + mt_rand(0,$diff);
echo date("Y-m-d H:i:s",$rndtime);

and another version for only hour and minute

$unix_start = strtotime("2017-10-10 12:25:16");
$unix_end = strtotime("2017-10-10 12:48:06");
$diff = $unix_end - $unix_start;
$rndtime =  $unix_start + mt_rand(0,$diff);
echo date("H:i",$rndtime);

Start time always must be low end time.

rand - Manual, Note: As of PHP 7.1.0, rand() uses the same random number generator as mt_rand(). //therefore we don't have to do the random checking each time we put  The rand() PHP function can also be used to generate a random number within a specific range, such as a number between 10 and 30. If no max limit is specified when using the rand() PHP function, the largest integer that can be returned is determined by the getrandmax() function, which varies by operating system.

Or should you need a more object-oriented approach, that's three lines:

$randDate = new DateTime();
$randDate->setTime(mt_rand(0, 23), mt_rand(0, 59));
echo $randDate->format('Y-m-d H:i');  // or whatever format you need

I would recommend to use mt_rand() over random_int(), because you don't necessarily need the cryptographic random ints in this case - mt_rand() is faster

srand - Manual, To generate a random number which is different every day, I used the number of days after unix epoch as a seed: <?php srand(floor(time() / (60*60*24))); Random Clock Time Generator. This form allows you to generate random clock times of the day (or night). The randomness comes from atmospheric noise, which for many purposes is better than the pseudo-random number algorithms typically used in computer programs.

PHP rand() Function, Tip: The mt_rand() function produces a better random value, and is 4 times faster than PHP 4.2.0: The random number generator is seeded automatically. You create a file called a digitrandom.php and then copy and paste this PHP Code into digitrandom.php file. Then after that you open it on the browser. You get the number of 12 digits Your value will change as often as you refresh this browser.

Update existing post times to random times?, <?php /** * Plugin Name: WPSE 259750 Random Dates Generate a random date between January 1st, 2015 and now And if you want to randomize only the time part, then this is one way to achieve that: take the date of post; get only day part of it; concert it to time; add random number of seconds to it. Note: As of PHP 7.1.0, rand() uses the same random number generator as mt_rand(). To preserve backwards compatibility rand() allows max to be smaller than min as opposed to returning FALSE as mt_rand().

How do I create a random post that will last for a day, Here is some code doing what you've asked and using the ideas others have highlighted: <?php if ( false === ( $totd_trans_post_id = get_transient(  dt = caldiff (t) dt = 1x4 calendarDuration 1mo 1mo 1mo 1mo. Determine the number of days between each date. dt = caldiff (t, 'days') dt = 1x4 calendarDuration 31d 31d 28d 31d. Add a number of calendar months to the date, January 31, 2014, to create a sequence of dates that fall on the last day of each month.

Comments
  • Do you call that whole set of code each time? If you do surely the rand function is generating a new time each time it goes through the code?
  • Thank you for your work Vineet. I believe the above will be per visiter correct so as soon as someone visits the webpage the 24 hours will begin, is that correct?