PHP 比较日期
在本文中,我们将介绍PHP中日期之间的比较。
在PHP中比较日期,当日期格式相似时不是一项繁琐的任务,但当比较不同格式的日期时,就变得困难了。然后我们将使用DateTime类、date_diff方法和strtotime函数来比较PHP中的日期。
strtotime函数在PHP中是什么意思
在PHP中,strtotime()函数将文本形式的日期时间描述转换为UNIX时间戳。此函数接受一个字符串参数,表示日期时间的描述。我们可以使用date()函数将文本形式的日期时间返回为日期格式。
语法
strtotime (DateTime,time_now)
PHP中的date_diff()函数是什么意思
date_diff()函数用于返回两个DateTime对象之间的差异。
语法:
date_diff(datetime1, datetime2, absolute)
让我们以多种示例来比较 PHP 中的日期。
示例1
<! Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title> PHP compare dates Example </title>
<style>
body
{
margin-top: 20px;
text-align: center;
padding: 20px 20px 20px 20px;
background-color: lightgrey;
}
h2 {
font-style: italic;
font-family: "Playfair Display","Bookman",serif;
color: black;
letter-spacing: -0.005em;
word-spacing: 1px;
font-size: 1.75em;
font-weight: bold;
}
h3 {
font-style: italic;
font-family: "Playfair Display","Bookman",serif;
color: red;
letter-spacing: -0.005em;
word-spacing: 1px;
font-size: 1em;
font-weight: bold;
}
h4 {
font-style: italic;
font-family: "Playfair Display","Bookman",serif;
color: green;
letter-spacing: -0.005em;
word-spacing: 1px;
font-size: 1em;
font-weight: bold;
}
</style>
<body>
<h2> <u> PHP compare dates Example </u> </h2>
</body>
</html>
<?php
date1 = "2008-11-24";date2 = "2007-03-26";
echo " <h3> First date: date1 </h3>";
echo "<br>";
echo " <h3> Second date:date2 </h3>";
echo "<br>";
echo " <h4> Use greater than comparison operator to compare dates </h4>";
if (date1>date2)
echo " <h3> date1 is latest thandate2 </h3>";
else
echo " <h3> date1 is older thandate2 </h3>";
?>
解释:
在上面的示例中,我们创建了一个对给定日期进行相同格式比较的对比。在此示例中,我们使用了一个简单的比较运算符来比较php中的日期。
输出:
以下是此示例的输出:
示例2
<! Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title> PHP compare dates Example </title>
<style>
body
{
margin-top: 20px;
text-align: center;
padding: 20px 20px 20px 20px;
background-color: lightgrey;
}
h2 {
font-style: italic;
font-family: "Playfair Display","Bookman",serif;
color: black;
letter-spacing: -0.005em;
word-spacing: 1px;
font-size: 1.75em;
font-weight: bold;
}
h3 {
font-style: italic;
font-family: "Playfair Display","Bookman",serif;
color: red;
letter-spacing: -0.005em;
word-spacing: 1px;
font-size: 1em;
font-weight: bold;
}
h4 {
font-style: italic;
font-family: "Playfair Display","Bookman",serif;
color: green;
letter-spacing: -0.005em;
word-spacing: 1px;
font-size: 1em;
font-weight: bold;
}
</style>
<body>
<h2> <u> PHP compare dates Example </u> </h2>
</body>
</html>
<?php
date1 = "2008-11-24";date2 = "2007-03-26";
echo " <h3> First date: date1 </h3>";
echo "<br>";
echo " <h3> Second date:date2 </h3>";
echo "<br>";
echo " <h4> Compare dates
declare in different format
</h4>";
Timestamp1 = strtotime(date1);
Timestamp2 = strtotime(date2);
if (Timestamp1>Timestamp2)
echo " <h3> date1 is latest thandate2 </h3>";
else
echo " <h3> date1 is older thandate2 </h3>";
?>
解释:
在上面的示例中,我们比较了给定日期的两种不同格式。在这种情况下,我们使用strtotime()函数将给定的日期转换为相应的时间戳格式,然后比较时间戳值以获取所需的结果。
输出:
以下是此示例的输出:
示例3
<! Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title> PHP compare dates Example </title>
<style>
body
{
margin-top: 20px;
text-align: center;
padding: 20px 20px 20px 20px;
background-color: lightgrey;
}
h2 {
font-style: italic;
font-family: "Playfair Display","Bookman",serif;
color: black;
letter-spacing: -0.005em;
word-spacing: 1px;
font-size: 1.75em;
font-weight: bold;
}
h3 {
font-style: italic;
font-family: "Playfair Display","Bookman",serif;
color: red;
letter-spacing: -0.005em;
word-spacing: 1px;
font-size: 1em;
font-weight: bold;
}
h4 {
font-style: italic;
font-family: "Playfair Display","Bookman",serif;
color: green;
letter-spacing: -0.005em;
word-spacing: 1px;
font-size: 1em;
font-weight: bold;
}
</style>
<body>
<h2> <u> PHP compare dates Example </u> </h2>
</body>
</html>
<?php
date1 = new DateTime("19-02-24");date2 = new DateTime("2020-03-24");
if (date1>date2) {
echo '<h3> datetime1 greater than datetime2 </h3>';
}
if (date1<date2) {
echo '<h3> datetime1 lesser than datetime2 </h3>';
}
if (date1 ==date2) {
echo '<h3> datetime2 is equal than datetime1 </h3>';
}
?>
解释:
在上面的示例中,我们对给定的日期进行了比较。在这个示例中,我们使用了DateTime类来在php中比较两个日期。
输出:
以下是这个示例的输出:
示例4
<! Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title> PHP compare dates Example </title>
<style>
body
{
margin-top: 20px;
text-align: center;
padding: 20px 20px 20px 20px;
background-color: lightgrey;
}
h2 {
font-style: italic;
font-family: "Playfair Display","Bookman",serif;
color: black;
letter-spacing: -0.005em;
word-spacing: 1px;
font-size: 1.75em;
font-weight: bold;
}
h3 {
font-style: italic;
font-family: "Playfair Display","Bookman",serif;
color: red;
letter-spacing: -0.005em;
word-spacing: 1px;
font-size: 1em;
font-weight: bold;
}
h4 {
font-style: italic;
font-family: "Playfair Display","Bookman",serif;
color: green;
letter-spacing: -0.005em;
word-spacing: 1px;
font-size: 1em;
font-weight: bold;
}
</style>
<body>
<h2> <u> PHP compare dates Example </u> </h2>
</body>
</html>
<?php
date1 = "2021-03-01";date2 = "2021-01-07";
d1 = date_create("2020-01-01");d2 = date_create("2020-01-05");
diff = date_diff(d1,d2);
print "<pre>";
print_r(diff);
print "</pre>";
if(diff->invert=="0"){
echo ' <h3> Date '.date2.' is greater then </h3>'.$date1;
}
?>
解释:
在上面的示例中,我们对给定的日期进行了比较。我们使用date_diff()方法进行比较。
输出:
以下是这个示例的输出结果: