Different Ways to Generate a Random String in PHP

AmitDiwan
Updated on 02-Jul-2020 06:47:41

386 Views

Using bin2hex functionExample Live DemoOutputThe randomly generated string is : f1db16115fa93b98493d388bA number is defined and the bin2hex function is called on this number. Inside the bin2hex function the ‘random_bytes’ function is called on this number. The generated random string is printed on the console.Using hashing functionExample Live DemoOutputThe randomly generated string using hashing function sha1 is :9a4a73c35ac034832332977f3d5accd8eace5260A number is defined by calling the ‘rand’ function. The sha1 hashing function is called on this randomly generated number. The generated random string is printed on the console.Using built-in function uniqidExample Live DemoOutputThe randomly generated string using uniqid function is : 5ed4b884cef34A number is defined by ... Read More

Generate Numeric One-Time Password in PHP

AmitDiwan
Updated on 02-Jul-2020 06:47:16

1K+ Views

To generate a numeric one-time password in PHP, the code is as follows −Example Live DemoOutputThe one time password generated is :52471609A function named ‘generate_otp’ is defined that takes the length as a parameter. This is the length of the password that needs to be generated. A number that contains 0 to 9 numbers is defined and the length is iterated over and a random number that contains these 0 to 9 numbers randomly is generated. The length is defined and the function is called on this length. This generates a numeric password and displays it on the console.

Round Seconds to Nearest Half Minute in MySQL

AmitDiwan
Updated on 02-Jul-2020 06:45:14

355 Views

To round seconds to nearest half minute, use CEILING(). Let us first create a table −mysql> create table DemoTable (secondValue int); Query OK, 0 rows affected (0.64 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values(27); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(118); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+-------------+ | secondValue | +-------------+ | 27          | | 56       ... Read More

Compare Float Values in PHP

AmitDiwan
Updated on 02-Jul-2020 06:45:05

158 Views

To compare float values in PHP, the code is as follows −Example Live DemoOutputThe values are not sameThree values are defined that are floating point numbers. The absolute values of these numbers are compared and relevant message is displayed on the console.

Select Three Random Records with Fixed Character Count in MySQL

AmitDiwan
Updated on 02-Jul-2020 06:43:49

121 Views

For this, you can use CHAR_LENGTH(). Use RAND() for random records. Let us first create a table −mysql> create table DemoTable (Subject text); Query OK, 0 rows affected (0.61 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values('C'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('MySQL'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('MongoDB'); Query OK, 1 row affected (0.59 sec) mysql> insert into DemoTable values('RubyOnRails'); Query OK, 1 row affected (0.25 sec) mysql> insert ... Read More

Sort Dates in PHP from an Array

AmitDiwan
Updated on 02-Jul-2020 06:43:22

171 Views

To sort dates given in the form of an array in PHP, the code is as follows −Example Live DemoOutputThe dates in sorted order is Array (    [0] => 2090-12-06    [1] => 2020-09-23    [2] => 2002-09-11    [3] => 2009-30-11 )A function named ‘compare_dates’ takes two time formats as parameters. If the first time format is greater than the second one, it returns -1. Otherwise, if the first time format is lesser than the second time, it returns 1 and if both the conditions are not true, the function returns 0. An array is defined that contains various ... Read More

Compare Two Dates in PHP

AmitDiwan
Updated on 02-Jul-2020 06:41:52

245 Views

To compare two dates in PHP, the code is as follows −Example Live DemoOutput2020-11-22 is later than 2011-11-22Two dates of ‘DateTime’ format are generated and checked to see which one is sooner or later. If the first date is later, relevant message is printed on the console. Otherwise, a message indicating that the first date is sooner than the second date is printed on the console.

Find Number of Days in Every Week Between Two Given Date Ranges in PHP

AmitDiwan
Updated on 02-Jul-2020 06:40:19

642 Views

To find number of days in every week between two given date ranges in PHP, the code is as follows −Example Live DemoOutputThe number of days between the given range isArray (    [Monday] => 5    [Tuesday] => 5    [Wednesday] => 5    [Thursday] => 5    [Friday] => 4    [Saturday] => 4    [Sunday] => 4 )Two dates of ‘DateTime’ type is defined and an array of days of a week is defined, where initially the count of all days of a week are 0. The dates are converted to a time format and timestamp variable is ... Read More

Find Total Number of Days Between Two Given Dates in PHP

AmitDiwan
Updated on 02-Jul-2020 06:38:28

221 Views

The ‘date_diff’ function can be used to get the difference between two dates. It is a built-in function that returns a DateInterval object if a specific number of days has been found, and returns False if the days is not found.Example Live DemoOutputThe day difference is: +60 daysTwo dates are defined using the ‘date_create’ function and the difference/ the number of days that are present between these two dates can be computed using the ‘date_diff’ function. It is assigned to a variable and printed on the console.

Print Continuous Character Pattern Triangle in PHP

AmitDiwan
Updated on 02-Jul-2020 06:37:21

448 Views

To print continuous character pattern triangle in PHP, the code is as follows −Example Live DemoOutputA B C D E F G H I J K L M N O P Q R S T UThis is similar to generating a star or a number pattern, the only difference being, continuous characters of English alphabets are generated instead of stars or numbers. The function ‘continuous_alphabets’ is defined that takes the limit as a parameter. The limit value is iterated over and the character is printed and relevant line breaks are also generated in between. The function is called by passing this ... Read More

Advertisements