Table of Contents
相关推荐:
Home Backend Development PHP Tutorial Detailed graphic explanation of time function date and commonly used time calculations in PHP

Detailed graphic explanation of time function date and commonly used time calculations in PHP

May 22, 2018 pm 03:42 PM
date php use

This article mainly introduces the relevant knowledge of the time function date and commonly used time calculations in PHP, which has a good reference value. Let’s take a look at it with the editor.

has been used in projects until today, yesterday, this week, this month, this quarter, this year, last week, last month, last quarter, etc. Timestamps, take advantage of this I have plenty of time recently, so I plan to summarize and study the relevant time knowledge points of PHP

1, read the PHP manual date function

commonly used time Function:

checkdate() verifies whether a time is correct

date_default_timezone_get() obtains the time zone used by the current script

date_default_timezone_set() sets the script The time zone used ini_set() can also be satisfied, or the configuration file can be modified

date_sunrise() date_sunset() returns the sunrise time and sunset time of the given date and location

date() format A date, there will be details below

Getdate() Gets the relevant information of the date and time

Gettimeofday() Gets the relevant information of the current time

idate() Changes the local time The date is formatted as an integer, but only accepts one character as parameter

Microtime() returns the current timestamp and number of seconds

mktime() gets the timestamp of a date

strtotime() parses the date seconds of English text into a timestamp

2, important functions are explained in detail

##date() formats one Date##    

string date( string $format [, int $timestamp] )

                                                                      remembered in int/ʻint $timestamp] /    or date , this has leading zeros, such as 01,02

D The day of the week, which is the abbreviation of the English day of the week, Mon to Sun

, this is the complete English format, Sunday to Saturday

 N  Use numbers to indicate the days of the week, 1 is Monday, 7 is Sunday

  S  The English suffix

# after the number of days in the month ##  w  The day of the week, represented by numbers, 0 is Sunday, 6 is Saturday

  z    The day of the year 0 to 365

                                                                                                                                                                                  …………**** lately be in numbers)

F Month, complete English word

m Month number format, with leading 0

M Three-letter abbreviated month

n Number The month represented, without leading 0

 t            L  Detect whether it is a leap year, leap year is 1, month is 0

  Y  4 digits The year represented by digits

 y The year represented by 2 digits

 a  The value of the morning or afternoon in lowercase letters

  A  The value of the morning or afternoon in uppercase letters

G 0

i The number of minutes with leading 0

s The number of seconds with leading 0

u The number of milliseconds, the date() function returns the format of 000000

E 22:21 00:00 Time in format

U ##The difference with date is that this function can only pass one parameter, date() can pass multiple parameters

  B  Internet time

  d  The day of the month

h 12-hour time

H 24-hour time

i Minutes

Returns 1 if daylight saving time is enabled, otherwise 0

L If it is a leap year, return 1, otherwise return 0 Number of seconds since 1970

 w  Day of the week

  W   Number of week in the year, week starts on Monday

  y  Year, 1 or 2 Number of digits Y Year 4 digit number

z Day of the year

#strtotime() function connection

Usage example

      strtotime ("now");

      strtotime ("10 September 2017");

      strtotime ("+1 day");

      strtotime ("+1 week");

      strtotime ("+1 week 2 days 4 hours 2 seconds");

      strtotime ("next Thursday");
      strtotime ("last Monday");
Copy after login

3, summary of commonly used times

$times = [];
function makeTime(){
  //获取今日开始时间戳和结束时间戳
  $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
  $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
  $times['today']['begin'] = $beginToday;
  $times['today']['end'] = $endToday;

  //获取昨日起始时间戳和结束时间戳
  $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
  $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
  $times['yesterday']['begin'] = $beginYesterday;
  $times['yesterday']['end'] = $endYesterday;

  //获取上周起始时间戳和结束时间戳
  $beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
  $endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
  $times['lastWeek']['begin'] = $beginLastweek;
  $times['lastWeek']['end'] = $endLastweek;

  //获取本月起始时间戳和结束时间戳
  $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
  $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
  $times['thisMonth']['begin'] = $beginThismonth;
  $times['thisMonth']['end'] = $endThismonth;

  //获取本周开始时间和结束时间,此例中开始时间为周一
  $defaultDate = date('Y-m-d');
  $first = 1;
  $w = date('w',strtotime($defaultDate));
  $beginWeek = strtotime("$defaultDate-" . ($w?$w-$first:6) . 'days');
  $endWeek = $beginWeek + 6*24*3600-1;
  $times['thisWeek']['begin'] = $beginWeek;
  $times['thisWeek']['end'] = $endWeek;

  //获取上月的起始时间戳和结束时间戳
  $beginLastmonth=mktime(0,0,0,date('m')-1,1,date('Y'));
  $endLastmonth=mktime(23,59,59,date('m')-1,date('t'),date('Y'));
  $times['LastMonth']['begin'] = $beginLastmonth;
  $times['LastMonth']['end'] = $endLastmonth;

  //获取今年的起始时间和结束时间
  $beginThisyear = mktime(0,0,0,1,1,date('Y'));
  $endThisyear = mktime(23,59,59,12,31,date('Y'));
  $times['thisYear']['begin'] = $beginThisyear;
  $times['thisYear']['end'] = $endThisyear;

  //获取上年的起始时间和结束时间
  $beginLastyear = mktime(0,0,0,1,1,date('Y')-1);
  $endLastyear = mktime(23,59,59,12,31,date('Y')-1);
  $times['lastYear']['begin'] = $beginLastyear;
  $times['lastYear']['end'] = $endLastyear;

  //获取本季度开始时间和结束时间
  $season = ceil((date('n'))/3);//当月是第几季度
  $beginThisSeason = mktime(0, 0, 0,$season*3-3+1,1,date('Y'));
  $endThisSeason = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
  $times['thisSeason']['begin'] = $beginThisSeason;
  $times['thisSeason']['end'] = $endThisSeason;

  //获取上季度的起始时间和结束时间
  $beginLastSeason = mktime(0, 0, 0,($season-1)*3-3+1,1,date('Y'));
  $endLastSeason = mktime(23,59,59,($season-1)*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
  $times['lastSeason']['begin'] = $beginLastSeason;
  $times['lastSeason']['end'] = $endLastSeason;

  return $times;
}
$times = makeTime();
Copy after login

目前是我之前用到的时间戳,后期还会积累汇总,避免重复造轮子。

相关推荐:

php date函数介绍与使用方法详解

关于PHP MySQL Update语句的相关内容

PHP验证码类ValidateCode

The above is the detailed content of Detailed graphic explanation of time function date and commonly used time calculations in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles