Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
PHP program to remove non-alphanumeric characters from string
To remove non-alphanumeric characters from a string in PHP, you can use the preg_replace() function with regular expressions. This function searches for a pattern and replaces it with specified content. Method 1: Using [^a-z0-9] Pattern The most common approach uses a negated character class to match anything that's not alphanumeric − The non-alphanumeric characters removed gives the string as Thisissampleonly The pattern /[^a-z0-9]/i means: [^a-z0-9] − Match any character that is NOT a lowercase letter or digit i flag − Makes the pattern case-insensitive Method 2: ...
Read MorePHP program to convert a given timestamp into time ago
In PHP, converting a timestamp to a human-readable "time ago" format is useful for displaying relative time in applications like social media feeds or comment systems. This can be achieved using a custom function that calculates the time difference and formats it appropriately. Example The following example demonstrates how to convert a timestamp into a "time ago" format ? The timestamp to time ago conversion is 10 minutes ago 1 hour ago 1 day ago How It Works The to_time_ago() function calculates the difference between the current time and ...
Read MorePHP program to calculate the total time given an array of times
In PHP, you can calculate the total time from an array of time strings by converting each time to seconds, summing them up, and converting back to HH:MM:SS format. The key is to parse time strings correctly and handle the arithmetic properly. Example Here's how to sum an array of time strings ? Total time: 24:38:54 How It Works The solution breaks down each time string using explode(':') to separate hours, minutes, and seconds. Each component is converted to seconds and added to the running total. Finally, the total ...
Read MorePHP program to generate a numeric one-time password
To generate a numeric one-time password in PHP, you can use the rand() function to randomly select digits from a string of numbers. This method ensures each digit is chosen randomly for better security ? Example Output The one time password generated is: 52471609 How It Works The generate_otp() function takes the desired length as a parameter. It defines a string containing digits 0-9 in a shuffled order. The function then iterates through the specified length, randomly selecting one digit at a time using rand() and substr(). Each selected ...
Read MorePHP program to compare float values
Comparing float values in PHP requires special attention due to floating-point precision issues. Direct equality comparison (==) can fail with floats, so we use epsilon-based comparison to check if values are "close enough". Basic Float Comparison The most reliable method uses abs() function with a small tolerance value (epsilon) ? The values are not same Why Direct Comparison Fails This example demonstrates why == comparison can be unreliable with floats ? Not equal using == Equal using epsilon method Comparison Function ...
Read MorePHP program to sort dates given in the form of an array
To sort dates given in the form of an array in PHP, you can use the usort() function with a custom comparison function. This approach leverages strtotime() to convert date strings into timestamps for accurate comparison. Example Output The dates in sorted order: Array ( [0] => 2090-12-06 [1] => 2020-09-23 [2] => 2009-11-30 [3] => 2002-09-11 ) How It Works The function compare_dates takes two date parameters and uses strtotime() to convert ...
Read MorePHP program to compare two dates
To compare two dates in PHP, you can use the DateTime class which provides built-in comparison operators. This allows you to directly compare date objects using standard comparison operators like >, 2020-11-22 is later than 2011-11-22 Using diff() Method You can also use the diff() method to get detailed information about the difference between dates ? First date is 5 days later Comparing Equal Dates To check if two dates are exactly equal ? Both dates are equal ...
Read MorePHP program to find number of days in every week between two given date ranges
To find the number of days in every week between two given date ranges in PHP, we can use the DateTime class to iterate through dates and count occurrences of each weekday. Example The following example counts how many times each day of the week occurs between two dates ? The number of days between the given range is: Array ( [Monday] => 5 [Tuesday] => 5 [Wednesday] => 5 [Thursday] => 5 [Friday] => 4 ...
Read MorePHP program to find the total number of date between any two given dates
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 containing the difference between the dates. Syntax date_diff(datetime1, datetime2, absolute) Parameters datetime1 − The first date object datetime2 − The second date object absolute (optional) − If set to TRUE, the interval will always be positive Example The following example demonstrates how to find the total number of days between two given dates ? The day difference is: +60 days Using ...
Read MorePHP program to print continuous character pattern triangle
To print continuous character pattern triangle in PHP, we use nested loops to generate a triangular pattern where each row contains consecutive alphabets. The pattern starts with 'A' and continues sequentially across all rows. Example Here's how to create a continuous character pattern triangle ? Output A B C D E F G H I J K L M N O P Q R S T U How It Works The function uses two nested loops to create the triangular pattern. The ...
Read More