PHP Articles

Page 40 of 81

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

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 More

PHP program to generate a numeric one-time password

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

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 More

PHP program to compare float values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 198 Views

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 More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 238 Views

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 More

PHP program to compare two dates

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 381 Views

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 More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 692 Views

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 More

PHP program to find the total number of date between any two given dates

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 265 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 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 More

PHP program to print continuous character pattern triangle

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 541 Views

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

PHP program to print the number pattern

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

To print the number pattern in PHP, we use nested loops where each row contains the row number repeated row number of times. This creates a triangular pattern of numbers. Example Output 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 How It Works The function num_pattern() takes the limit as a parameter. The outer loop runs from 0 to the limit value, and the inner loop prints the current number ...

Read More

PHP program to print a pattern of pyramid

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

In PHP, you can print a pyramid pattern using nested loops. The pattern involves printing spaces for centering and asterisks to form the pyramid shape. Example Here's how to create a pyramid pattern using a function − Output * * * * * * * * * * * * * * * * * * * * * * * * * * * * How It ...

Read More
Showing 391–400 of 802 articles
« Prev 1 38 39 40 41 42 81 Next »
Advertisements