Skip to content

Instantly share code, notes, and snippets.

@cp6
Created July 25, 2020 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cp6/4b12df0fff597726e133676f680379ef to your computer and use it in GitHub Desktop.
Save cp6/4b12df0fff597726e133676f680379ef to your computer and use it in GitHub Desktop.
PHP generate random time from timestamp
<?php
function randomTimeFormatted(string $max_time): string
{
$t = explode(':', $max_time);
return sprintf("%02d:%02d:%02d", rand(0, $t[0]), rand(0, $t[1]), rand(0, $t[2]));
}
echo randomTimeFormatted('02:38:55');
//02:38:55 will make random string from 0->2 hours, 0->38 mins, 0->55 seconds
//Could return 01:24:33 etc
//Wont return anything greater than 02:38:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment