<?php
/**
* Replaces time values for validation checks.
*
* @param string $time_value The time value string (e.g., '10:30:00').
* @param string $replacement_value The replacement time value string (e.g., '00:00:00').
* @param string $format The format of the time value (e.g., 'H:i:s').
* @return string The replaced time value string, or the original value if the input is invalid.
*/
function replaceTimeForValidation(string $time_value, string $replacement_value, string $format = 'H:i:s'): string
{
// Validate input parameters
if (empty($time_value) || empty($replacement_value) || empty($format)) {
return $time_value; // Return original if any input is empty.
}
// Attempt to create a DateTime object. Handle potential errors gracefully.
try {
$original_datetime = new DateTime($time_value);
} catch (Exception $e) {
return $time_value; // Return original if parsing fails.
}
// Create a new DateTime object with the replacement value.
$new_datetime = new DateTime($replacement_value);
// Format the new DateTime object back to the original format.
return $new_datetime->format($format);
}
//Example Usage
// $time = "14:45:12";
// $replaced_time = replaceTimeForValidation($time, "00:00:00");
// echo $replaced_time . "\n";
?>
Add your comment