PHP中比较时间大小实例,php时间大小实例
PHP中比较时间大小实例,php时间大小实例
神马系PHP时间比较呢?
最简单的例子就是可以把两个时间用大于小于号进行两个时间的对比。有时候我们设计程序,需要控制用户操作某个功能时只能在某个时间段内可以操作,其余时间段不允许。
比如,我有一个发布消息的网站,我这个网站上发布消息,然后我在通过这个消息群发给上千用户。按照普通做法,应该就是一旦发布消息,就立即把消息群发给上千用户。这样一来,只要一有最新消息,用户就第一时间收到手机短信。可是,问题来了。比如我要是在深夜凌晨发布信息,那可就遭到所有用户XXOO了。
所以,这里可以用到时间比较,要是在白天时间就可以发布,晚上则阻止。我们假设白天时间段从早上七点(七点估计好多人都没睡醒)到下午六点,那么我们可以这么下。
复制代码 代码如下:
/**
* PHP时间比较
*/
// 定义开始时间,注意时间是字符串格式,所以必须用引号引起来否则报错别找我,另外里边的冒号必须是英文冒号
$start_time = '7:00';
// 定义结束时间,千万别问我下午六点为何写成十八点,我会建议你重读小学
$end_time = '18:00';
// 获取现在时间段,date()函数的使用我就不废话了,不明白的直接看以往文章或者google
$now_time = date('H:i');
// 判断
if( $start_time=$now_time ){
echo '我要发布信息啦!';
}else{
echo '大哥,现在才几点啊~~~人家都还没睡醒呢!!!';
}
这里需要提醒下,获取的现在的时间是服务器时间,所以请注意服务器时间的时区设置以及是否准时,或者可以来个计划任务让它每天校对就万事OK了
如果你想表示在$time1与$time3之间签到算出勤的话,可以这样写
if($time2>$time1 && $time2echo "出勤";
}
else{
echo "缺勤";
}
当然也可以写成
if(strtotime($time2)>strtotime($time1) && strtotime($time2)
}
else{
echo "缺勤";
}
时间可以那样比较,不过你的条件写错了,应该是&&吧?
if($time2>$time1 && $time2......

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
