Home  >  Article  >  Backend Development  >  How to determine whether it is the time period of the day in php

How to determine whether it is the time period of the day in php

藏色散人
藏色散人Original
2021-12-16 09:34:392764browse

php method to determine whether it is the time period of the day: 1. Set a date; 2. Intercept the date part and discard the hours, minutes and seconds; 3. Get today's date; 4. Use IF as a string to determine whether It’s not just a time period of the day.

How to determine whether it is the time period of the day in php

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

How does php determine whether it is the time period of the day?

PHP determines whether the time is the time of the day

The first type:

<?php 
 
/** 
 
 * PHP判断一个日期是不是今天 
 
 * 琼台博客 
 
 */ 
 
echo &#39;<meta charset="utf-8" />&#39;; 
 
// 拟设一个日期 
 
$a = &#39;2012-06-28 10:10:10&#39;; 
 
// 截取日期部分,摒弃时分秒 
 
$b = substr($a,0,10); 
 
// 获取今天的日期,格式为 YYYY-MM-DD 
 
$c = date(&#39;Y-m-d&#39;); 
 
// 使用IF当作字符串判断是否相等 
 
if($b==$c){ 
 
    echo &#39;是今天&#39;; 
 
}else{ 
 
    echo &#39;不是今天&#39;; 
 
} 
 
?>

The second type:

<?php 
 
/** 
 
 * PHP判断一个日期是不是今天 
 
 * 琼台博客 
 
 */ 
 
echo &#39;<meta charset="utf-8" />&#39;; 
 
// 拟设一个日期 
 
$a = &#39;2012-06-28 10:10:10&#39;; 
 
// 转换为时间戳 
 
$a_ux = strtotime($a); 
 
// 转换为 YYYY-MM-DD 格式 
 
$a_date = date(&#39;Y-m-d&#39;,$a_ux); 
 
// 获取今天的 YYYY-MM-DD 格式 
 
$b_date = date(&#39;Y-m-d&#39;); 
 
// 使用IF当作字符串判断是否相等 
 
if($a_date==$b_date){ 
 
    echo &#39;是今天&#39;; 
 
}else{ 
 
    echo &#39;不是今天&#39;; 
 
} 
 
?>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine whether it is the time period of the day in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn