Home  >  Article  >  Backend Development  >  Compare date size and time size in PHP

Compare date size and time size in PHP

Faith、
Faith、Original
2021-12-16 14:35:19140browse

Compare two dates or times using the greater than or less sign. In some scenarios, it is necessary to control the user to operate or view a certain function only on a certain date or within a certain time period, and not allowed in other time periods.

The company's e-commerce project has such needs:

Preferential price activity function:

1. Preferential price activity function setting

<?php
$start_date = &#39;2021-09-20&#39;;
$end_date = &#39;2021-09-21&#39;;
$now_date = date(&#39;Y-m-d&#39;);
if( $start_date<=$now_date && $end_date>=$now_date ){
     echo &#39;当前日期在区间内&#39;;//在此实现自己的逻辑
}else{
     echo &#39;当前日期不在区间内&#39;;//在此实现自己的逻辑
}

die;

$start_time = &#39;7:00&#39;;
$end_time  = &#39;18:00&#39;;
$now_time  = date(&#39;H:i&#39;);
if( $start_time<=$now_time && $end_time>=$now_time ){
     echo &#39;当前时间在区间内&#39;;//在此实现自己的逻辑
}else{
     echo &#39;当前时间不在区间内&#39;;//在此实现自己的逻辑
}

The above is the detailed content of Compare date size and time size 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