网络营销电子商务研究中心

网络营销电子商务研究中心 (https://www.0058.net/index.php)
-   PHP (https://www.0058.net/forumdisplay.php?f=75)
-   -   how to generate random time, pls? (https://www.0058.net/showthread.php?t=4537)

topvip 2013-05-09 12:01 PM

how to generate random time, pls?
 
Q:
say i need random time between 01-01-1970 to 12-31-2012,
how to do, pls?

A:
$timestamp1 = strtotime('01-01-1970');
$timestamp2 = strtotime('31-12-2012');
echo "timestamp 1 (01-01-1970) : ".$timestamp1."<br />timestamp 2 (31-12-2012) : ".$timestamp2;

$rand_timestamp = rand(-3600, 1356908400);
$rand_date = date('d-m-Y', $rand_timestamp);
echo "<br />random date between 01-01-1970 and 31-12-2012 : ".$rand_date;

$now_timestamp = time();
$rand_timestamp_till_now = rand(-3600, $now_timestamp);
$rand_date_till_now = date('d-m-Y', $rand_timestamp_till_now);
echo "<br />random date between 01-01-1970 and NOW : ".$rand_date_till_now;

-----------------------

A:

<?php

$timestamp = strtotime('2012-12-31');

// unix timestamp for 1970-01-01 is => 0

$result = rand(0, $timestamp);

print date('Y/m/d h:i:s', $result);

?>


All times are GMT +8. The time now is 10:38 PM.

Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.