NameSilo

Generate random timestamp?

Spaceship Spaceship
Watch
Impact
38
how can I generate a random timestamp (between now and a few seconds from now / before now) with PHP?

PHP:
$this->db->Execute("UPDATE ".T_BANNERS." SET impressions=impressions+1, date_last_displayed=NOW() WHERE id=?",array($banner['id']));

That is my PHP that does the update. The problem is I am doing this multiple times quickly and the same date/time is getting inserted for each banner (we are updating banners here) which as a result is causing trouble randomizing banners to display.

What I want to do:

Create a random time (in seconds) from now and a few seconds in the future. Just enough time so the banners don't get the same date_last_displayed value in each row.


I have searched everywhere for an easy way to do this and most don't explain how to randomize time without treating it as a string.

Any help is greatly appreciated.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
hi,

instead of "randomizing" your timestamp as you put it, you could order your data by the timestamp and id

Order by timestamp, id....

that way you will get the banners sorted in the correct order
 
0
•••
You can convert your timestamp to an integer, and create a random number & add it on to the time. No need to work with strings. Look at the time() function
 
0
•••
geronimo said:
hi,

instead of "randomizing" your timestamp as you put it, you could order your data by the timestamp and id

Order by timestamp, id....

that way you will get the banners sorted in the correct order

Yes thats exactly what I did to fix this, order by date_last_modified, rand()

and that seems to work. I just wanted to see how this would be done like the last poster said, by adding an integer.
 
0
•••
fair enough, glad you fixed it.
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back