Created
June 4, 2023 10:25
-
-
Save md-riaz/e897922c23455d6458abe591eee7dd4f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function convertCronJobScheduleToHumanReadable($schedule) { | |
// Split the schedule into its components. | |
$components = explode(' ', $schedule); | |
// Get the minute, hour, day of month, month, day of week, and year components. | |
$minute = $components[1] ?? '*'; | |
$hour = $components[2] ?? '*'; | |
$dayOfMonth = $components[3] ?? '*'; | |
$month = $components[4] ?? '*'; | |
$dayOfWeek = $components[5] ?? '*'; | |
$year = $components[6] ?? '*'; | |
// Create a human-readable string for the schedule. | |
$humanReadableSchedule = ''; | |
// Add the minute component. | |
if ($minute !== '*') { | |
$humanReadableSchedule .= "Every $minute minute, "; | |
} | |
// Add the hour component. | |
if ($hour !== '*') { | |
$humanReadableSchedule .= "at $hour hour, "; | |
} | |
// Add the day of month component. | |
if ($dayOfMonth !== '*') { | |
$humanReadableSchedule .= "on the $dayOfMonth day of the month, "; | |
} | |
// Add the month component. | |
if ($month !== '*') { | |
$humanReadableSchedule .= "in the $month month, "; | |
} | |
// Add the day of week component. | |
if ($dayOfWeek !== '*') { | |
$humanReadableSchedule .= "on the $dayOfWeek day of the week, "; | |
} | |
// Add the year component. | |
if ($year !== '*') { | |
$humanReadableSchedule .= "in the year $year."; | |
} else { | |
$humanReadableSchedule .= "every day."; | |
} | |
// Return the human-readable schedule. | |
return $humanReadableSchedule; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment