w3resource

PHP: mt_rand() function

Description

The mt_rand() function is used to generate a random integer using the Mersenne Twister algorithm.

Version:

(PHP 4 and above)

Syntax:

mt_rand()
mt_rand( minv, maxv )

Parameters:

Name Description Required /
Optional
Type
minv Lowest value to be returned.
Default : 0
Optional Integer
maxv Highest value to be returned.
Default: mt_getrandmax())
Optional Integer

Note: If the function is called without the minv and maxv arguments mt_rand() returns a pseudo-random value between 0 and mt_getrandmax(). If you want a random number between 10 and 15 (inclusive), for example, use mt_rand(10, 15).

Return value:

A random number value between minv (or 0) and maxv.

Value Type: Integer.

Example:

<?php
echo(mt_rand() . "<br />");
echo(mt_rand(10,15))
?>

View the example in the browser

See also

PHP Function Reference

Previous: mt_getrandmax
Next: mt_srand



PHP: Tips of the Day

PHP: Convert one date format into another in PHP

The second parameter to date() needs to be a proper timestamp (seconds since January 1, 1970). You are passing a string, which date() can't recognize.

You can use strtotime() to convert a date string into a timestamp. However, even strtotime() doesn't recognize the y-m-d-h-i-s format.

PHP 5.3 and up

Use DateTime::createFromFormat. It allows you to specify an exact mask - using the date() syntax - to parse incoming string dates with.

PHP 5.2 and lower

You will have to parse the elements (year, month, day, hour, minute, second) manually using substr() and hand the results to mktime() that will build you a timestamp.

But that's a lot of work! I recommend using a different format that strftime() can understand. strftime() understands any date input short of the next time joe will slip on the ice. for example, this works:

$old_date = date('l, F d y h:i:s');         // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d H:i:s', $old_date_timestamp);

Ref : https://bit.ly/33QzKPG