<!DOCTYPE html>
<html>
<head>
	<title>Cron 解析器</title>
	<meta charset='UTF-8'>
	<style>
		body {
			font-family: Arial, sans-serif;
			margin: 0;
			padding: 0;
		}
<pre><code>	h1 {
		text-align: center;
		margin-top: 50px;
	}

	form {
		margin: 50px auto;
		width: 80%;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	label {
		margin: 20px 0;
		font-size: 20px;
		display: flex;
		align-items: center;
	}

	input[type='text'], input[type='checkbox'] {
		margin-left: 20px;
		padding: 10px;
		font-size: 18px;
		border-radius: 5px;
		border: none;
		box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3);
	}

	input[type='submit'] {
		margin-top: 50px;
		padding: 10px 20px;
		font-size: 20px;
		background-color: #4CAF50;
		color: white;
		border: none;
		border-radius: 5px;
		cursor: pointer;
		box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3);
	}

	table {
		margin: 50px auto;
		border-collapse: collapse;
	}

	th, td {
		padding: 10px;
		border: 1px solid black;
	}
&lt;/style&gt;
</code></pre>
</head>
<body>
	<h1>Cron 解析器</h1>
	<form method='post'>
		<label>
			Cron 表达式:<input type='text' name='cron' maxlength='6' minlength='6' required>
		</label>
		<label>
			<input type='checkbox' name='option[]' value='yearly'>每年运行
		</label>
		<label>
			<input type='checkbox' name='option[]' value='monthly'>每月运行
		</label>
		<label>
			<input type='checkbox' name='option[]' value='weekly'>每周运行
		</label>
		<label>
			<input type='checkbox' name='option[]' value='daily'>每天运行
		</label>
		<label>
			<input type='checkbox' name='option[]' value='hourly'>每小时运行
		</label>
		<input type='submit' value='解析'>
	</form>
<pre><code>&lt;?php
	function getNextTenDates($cron) {
		$dates = array();
		$currentDate = date('Y-m-d H:i:s');

		for ($i = 0; $i &lt; 10; $i++) {
			$nextDate = shell_exec(&quot;echo $(/usr/bin/php -r 'echo strtotime(&quot;$currentDate&quot;);') | /usr/bin/php -r 'echo date(&quot;Y-m-d H:i:s&quot;, strtotime(&quot;$cron&quot;));' &quot;);
			$dates[] = $nextDate;
			$currentDate = $nextDate;
		}

		return $dates;
	}

	function getOptionCron($option) {
		switch ($option) {
			case 'yearly':
				return '0 0 1 1 *';
			case 'monthly':
				return '0 0 1 * *';
			case 'weekly':
				return '0 0 * * 0';
			case 'daily':
				return '0 0 * * *';
			case 'hourly':
				return '0 * * * *';
		}
	}

	if ($_SERVER['REQUEST_METHOD'] === 'POST') {
		$cron = $_POST['cron'];
		$options = $_POST['option'];

		$nextTenDates = getNextTenDates($cron);

		echo '&lt;table&gt;';
		echo '&lt;tr&gt;&lt;th&gt;序号&lt;/th&gt;&lt;th&gt;日期时间&lt;/th&gt;&lt;/tr&gt;';
		for ($i = 0; $i &lt; count($nextTenDates); $i++) {
			echo '&lt;tr&gt;&lt;td&gt;' . ($i + 1) . '&lt;/td&gt;&lt;td&gt;' . $nextTenDates[$i] . '&lt;/td&gt;&lt;/tr&gt;';
		}
		echo '&lt;/table&gt;';

		if (!empty($options)) {
			echo '&lt;h2&gt;选中的运行选项对应的Cron表达式&lt;/h2&gt;';
			echo '&lt;table&gt;';
			echo '&lt;tr&gt;&lt;th&gt;运行选项&lt;/th&gt;&lt;th&gt;Cron表达式&lt;/th&gt;&lt;/tr&gt;';
			foreach ($options as $option) {
				$optionCron = getOptionCron($option);
				echo '&lt;tr&gt;&lt;td&gt;' . $option . '&lt;/td&gt;&lt;td&gt;' . $optionCron . '&lt;/td&gt;&lt;/tr&gt;';
			}
			echo '&lt;/table&gt;';
		}
	}
?&gt;
</code></pre>
</body>
</html>
Cron 表达式解析器 - 生成运行时间和转换表达式

原文地址: https://www.cveoy.top/t/topic/mQPA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录