Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save md-riaz/e897922c23455d6458abe591eee7dd4f to your computer and use it in GitHub Desktop.
Save md-riaz/e897922c23455d6458abe591eee7dd4f to your computer and use it in GitHub Desktop.
<?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