Hello Guys! :)

I am just wondering how I would be able to echo a random hour timestamp.

Any help would be appreciated!



6 Years
Discussion Span
Last Post by edwinhermann
Featured Replies

What do you mean hour timestamp? between 0 and 23? between 2 actual dates as an integer?
Give an example and the allowed range.


Just a random hour have nothing to do with time function.

echo rand(1, 12);

OR do you want random date?


Yes a random hour but I need to have it as a timestamp and not just a number



Give an example of the output you want.

Your answer does not make sense. You want a random hour (hours wrt time 0 to 12 or 00 to 23) do you need the accompanying minutes? Do you need the date part?

What is the timestamp to which you're referring?

To me a timestamp is this: 1259647200 (based on unix epoch)
A datetime is this: 2011-06-15 15:47

Edited by diafol: n/a


ravisoni6262 that is not correct!

on PHP manual:
Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

you can also use time to create a random time stamp, but what you posted e incorrect, because it will output the current time stamp|


Right let me basically make this clear:

I need to get a random hour that I will insert to my database I could do this by just getting the hour and inserting that or I could use a timestamp, now what this random time of day will do for me is when a user logs in during this time (like a happy hour feature) then they will receive some sort of reward, this feature would only be enabled on a certain day of the week, therefore what I am trying to do is something like


today is thursday & the time is RANDOM HOUR then give the user a gold star
Just log them in without giving them anything or doing anything...


Here I'm assuming you will be executing the code as the user logs in. So you can do this:

$random_hour = randbetween(0,23);
$current_hour = date("G",time());
if ($current_hour == $random_hour) {
  // something great happens

If you're trying to implement it on a weekly cycle (i.e. choosing a random hour on a random day) then use this code when the user logs in:

$random_hour = randbetween(0,23);
$random_day = randbetween(1,7);
$current_hour = date("G",time());
$current_day = date("N",time());
if ( ($current_hour == $random_hour) && ($current_day == $random_day) ) {
  // something great happens

Edited by edwinhermann: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.