Home > Article > Backend Development > Compare date size and time size in PHP
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 = '2021-09-20'; $end_date = '2021-09-21'; $now_date = date('Y-m-d'); if( $start_date<=$now_date && $end_date>=$now_date ){ echo '当前日期在区间内';//在此实现自己的逻辑 }else{ echo '当前日期不在区间内';//在此实现自己的逻辑 } die; $start_time = '7:00'; $end_time = '18:00'; $now_time = date('H:i'); if( $start_time<=$now_time && $end_time>=$now_time ){ echo '当前时间在区间内';//在此实现自己的逻辑 }else{ echo '当前时间不在区间内';//在此实现自己的逻辑 }
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!