Hello Guys! :)

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

Any help would be appreciated!

Yours,

Public-Image

Recommended Answers

All 13 Replies

Member Avatar for diafol

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

Member Avatar for diafol

Again:

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

public-image,
go to the gmdate() page php manual :)
http://php.net/manual/en/function.gmdate.php

look up the following expression on that page:
"random timestamp between two dates"

best regards

<?php
//will gives random unix timestamp
echo time();
?>

ravisoni6262 that is not correct!

on PHP manual:
time()
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|

Why isn't it random? It changes all the time! :D

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

FOR EXAMPLE:

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

Member Avatar for diafol

mktime() will give you the integer . Check against this to give (or not give) reward.

A note to take, there is no such a thing as true random in real world.
That said, there is pseudo random. That said, check rand

Member Avatar for diafol

rand() works, but I read mt_rand() is better. Any views?

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
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.