Found 1046 Articles for PHP

PHP program to remove non-alphanumeric characters from string

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

714 Views

To remove non-alphanumeric characters from string, the code is as follows −Example Live DemoOutputThe non-alphanumeric characters removed gives the string as ThisissampleonlyThe function ‘preg_replace’ is used to remove alphanumeric characters from the string. A regular expression is used to filter out the alphanumeric characters. The string is previously defined and the function ‘preg_replace’ is called on this and the reformed string is displayed on the console.Example Live DemoOutputThe non-alphanumeric characters removed gives the string as Thisisasample_onlyThe only difference here is that a different regular expression is used. It means the same as the previous regular expression, but written in a different fashion.Read More

PHP program to compute the execution time of a PHP script

AmitDiwan
Updated on 02-Jul-2020 06:51:46

601 Views

To compute the execution time of a PHP script, the code is as follows −Example Live DemoOutputThe execution time of the PHP script is : 1.69 secThe ‘microtime’ function can be used to check the time taken by a PHP script to execute completely. When the code begins execution, the time is recorded, and once the code is completed, another timestamp is generated and the difference between the end and star time is the time taken by the script to complete its execution.

PHP program to convert a given timestamp into time ago

AmitDiwan
Updated on 02-Jul-2020 06:50:46

737 Views

To convert a given timestamp into time ago, the code is as follows −Example Live DemoOutputThe timestamp to time ago conversion is 10 minutes agoA function named ‘to_time_ago’ is defined that checks the difference between the time passed as parameter to the function and the time function. If this difference is found to be less than 1, it returns that the time passed just a second ago. Otherwise, the year, month, day, hour, minute, and second is generated in an array. A ‘foreach’ loop is used to iterate over the array previously generated. The time difference is computed and printed on ... Read More

PHP program to calculate the total time given an array of times

AmitDiwan
Updated on 02-Jul-2020 06:48:50

1K+ Views

The ‘strtotime’ function can be used to convert the given string into a time format. Let us see an example −Example Live DemoOutputThe total time is :-441915:-12:-58An array that contains time format data is defined and the ‘strtotime´function is used to convert the string into a time format. The ‘foreach’ loop is used to iterate over the elements in the time format array and the elements are added.The hours is calculated by dividing the value computed by 3600. The minutes is calculated by dividing the value computed by product of hours calculated and 3600. The seconds is calculated by dividing the ... Read More

PHP program different ways to generate a random string

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

241 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

PHP program to generate a numeric one-time password

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

778 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.

PHP program to compare float values

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

104 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.

PHP program to sort dates given in the form of an array

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

91 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

PHP program to compare two dates

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

159 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.

PHP program to find number of days in every week between two given date ranges

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

503 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

Advertisements