search
HomeBackend DevelopmentPHP TutorialPHP determines whether a certain day is before or after the specified date_PHP Tutorial

There is such a requirement. The specified date is December 12th. I want to execute event A before December 12th and event B after that. How to judge whether today is before December 12th? Or after?

The procedure is as follows:

<?php
$year = 2010;
$month = 12;  // 月
$day = 12;  // 日
$timeoffset = 8; // 与格林尼治时间 GMT 的时差
list($thisyear, $thismonth, $thisday) = explode('-', date('Y-n-j', time() + $timeoffset * 3600));
if(($thismonth > $month) || ($thismonth == $month && $thisday > $day) || $thisyear > $year) {
	echo '已经过了';
} elseif ($thismonth == $month && $thisday == $day && $thisyear == $year) {
	echo '就是今天了';
} else {
	echo '还没到';
}
?>

Here is the annotated code:

//你还需要知道的另一个变量是年份,不过依你的意思应该是当年。
$timestamp = time();
$dateYear = date('Y', $timestamp);//当前年,依当前需要可以不需要比较,但有时也许会用到,比如你的记录不是当年要处理的
$dateMonth = date('n', $timestamp);//当前月份数字,没有前导零
$dateDay = date('j', $timestamp);//月份中的第几天,没有前导零
//剩下的就是比较了,比如你要比较的日期是 2007-03-01
$eventDate = '2007-03-01';
$eventDateArr = explode('-', $eventDate);
$eventYear = intval($eventDateArr[0]);
$eventMonth = intval($eventDateArr[1]);
$eventDay = intval($eventDateArr[2]);
//上面是年月日的拆分,根据实际情况可以调整,如果你直接可以得到就不需要这样取了。
//之后剩下的就是比较了,先比较年
if($dateYear == $eventYear) {
  //同年
  if($dateMonth == $eventMonth) {
    //同月
    if($dateDay == $eventDay) {
      //同一天,就是当前日期就是事件触发的日期
    } elseif($dateDay > $eventDay) {
      //事件已经过期了
    } else {
      //事件触发日期还没到
    }
  } else {
    //之前或之后,再需要详细判断可以在这里比较哪个月份大
  }
} else {
  //之前或之后,再需要详细判断可以在这里比较哪个年份大
}
//其实这种比较是最简单的逻辑判断,如果你在记录时间触发日期时记录的是 unix 时间戳,这里可以直接进行时间戳比较不过需要处理一下,因为时间戳是精确到秒的
//或者把你记录的日期及其后一天直接转化成 unix 时间戳,时分秒都用 0 ,和当前时间戳比较,细节判断就自己去分析吧,可用函数去查查手册里面 strtotime() ,时间相关函数参数很多,不过用过几次就熟悉了。

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752485.htmlTechArticleThere is such a requirement. The specified date is December 12th. I want to execute event A before December 12th. , to execute event B later, how to determine whether today is before or after December 12? The procedure is as follows...
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
如何根据当前时间戳创建文件/文件夹并为其命名如何根据当前时间戳创建文件/文件夹并为其命名Apr 27, 2023 pm 11:07 PM

如果您正在寻找根据系统时间戳自动创建文件和文件夹并为其命名的方法,那么您来对地方了。有一种超级简单的方法可以用来完成这项任务。然后,创建的文件夹或文件可用于各种目的,例如存储文件备份、根据日期对文件进行排序等。在本文中,我们将通过一些非常简单的步骤解释如何在Windows11/10中自动创建文件和文件夹,并根据系统的时间戳对其进行命名。使用的方法是批处理脚本,非常简单。希望你喜欢阅读这篇文章。第1节:如何根据系统当前时间戳自动创建文件夹并命名第1步:首先,导航到要在其中创建文件夹的父文件夹,

Golang时间处理:如何在Golang中将时间戳转换为字符串Golang时间处理:如何在Golang中将时间戳转换为字符串Feb 24, 2024 pm 10:42 PM

Golang时间转换:如何将时间戳转换为字符串在Golang中,时间操作是非常常见的操作之一。有时候我们需要将时间戳转换为字符串,以便于展示或者存储。本文将介绍如何使用Golang将时间戳转换为字符串,并提供具体的代码示例。1.时间戳和字符串的转换在Golang中,时间戳通常是以整型数字的形式表示的,表示的是从1970年1月1日至当前时间的秒数。而字符串则

如何从Excel中的日期中删除时间如何从Excel中的日期中删除时间May 17, 2023 am 11:22 AM

使用数字格式更改Excel中的日期格式在Excel中从日期中删除时间的最简单方法是更改​​数字格式。这不会从时间戳中删除时间——它只是阻止它在您的单元格中显示。如果您在计算中使用这些单元格,时间和日期仍然包括在内。要使用数字格式更改Excel中的日期格式:打开您的Excel电子表格。选择包含您的时间戳的单元格。在主菜单中,选择数字格式框末尾的向下箭头。选择一种日期格式。更改格式后,时间将停止出现在您的单元格中。如果单击其中一个单元格,则时间格式在编辑栏中仍然可见。使用单元格格式更

Java文档解读:System类的currentTimeMillis()方法用法解析Java文档解读:System类的currentTimeMillis()方法用法解析Nov 03, 2023 am 09:30 AM

Java文档解读:System类的currentTimeMillis()方法用法解析,需要具体代码示例在Java编程中,System类是一个非常重要的类,其封装了与系统相关的一些属性和操作。其中,currentTimeMillis方法是System类中非常常用的一个方法,本文将对该方法做详细解读并提供代码示例。一.currentTimeMillis方法概述

时间戳获取的最佳实践:Golang编程中的强大工具时间戳获取的最佳实践:Golang编程中的强大工具Dec 29, 2023 am 08:28 AM

Golang编程利器:时间戳获取的最佳实践引言:在软件开发中,时间戳是一个非常常见的概念。它是一个标识特定事件发生的数字值,通常表示自从某个参考时间点开始的毫秒数或纳秒数。在Golang中,处理时间戳的操作非常简单且高效。本文将介绍Golang中获取时间戳的最佳实践,并提供具体的代码示例。正文:获取当前时间戳在Golang中,获取当前时间戳非常简单。我们可以

PHP中的时间戳处理:如何使用strftime函数格式化时间戳为本地化的日期时间PHP中的时间戳处理:如何使用strftime函数格式化时间戳为本地化的日期时间Jul 30, 2023 am 09:15 AM

PHP中的时间戳处理:如何使用strftime函数格式化时间戳为本地化的日期时间在开发PHP应用程序时,我们经常需要处理日期和时间。PHP提供了强大的日期和时间处理函数,其中strftime函数允许我们将时间戳格式化为本地化的日期时间。strftime函数具有以下语法:strftime(string$format[,int$timestamp=ti

js和php怎么将时间戳转换为日期js和php怎么将时间戳转换为日期Mar 21, 2023 pm 05:16 PM

JavaScript和PHP都支持时间戳的处理。JavaScript使用了Date对象来处理日期和时间,而PHP中使用date()函数来格式化时间戳。本文将向大家介绍如何在JavaScript和PHP中进行时间戳与日期之间的转换。

php中的时间戳怎么转换成时间php中的时间戳怎么转换成时间Jan 07, 2023 pm 03:17 PM

在php中,可以使用date()函数来将时间戳转换为日期时间,该函数可以将时间戳格式化为可读性更好的日期和时间;语法格式“date(format,时间戳)”,参数format用于指定格式化字符,设置要转换成的日期格式,例如“Y-m-d H:i:s”,就是将时间戳转为“年-月-日 时:分:秒”格式。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools