How do I generate a random time interval and add it to a mysql datetime using php?

前端 未结 3 2091
说谎
说谎 2021-01-03 09:11

I have many rows in mysql table with datetime\'s in the format of:

2008-12-08 04:16:51 etc

I\'d like to generate a random time interval of

3条回答
  •  攒了一身酷
    2021-01-03 09:47

    Would you be able to use a random unix timestamp and convert into the MySQL timestamp format? I would assume you could so something like:

    $randomTime = time() + rand( 30, 86400 * 3 );   // since there are 86400 seconds in a day,
                                                            // this should generate a random time
                                                            // 3-30 days from now
    $randomTimeString = date( "Y-m-d H:i:s", $randomTime ); // format the date (php.net/date)
    $st = $mysqli->query( "...", $randomTimeString );       // insert it into the database
    ...
    

    This probably isn't the most efficient solution, but it should work.

提交回复
热议问题