Summary of date and time operations in PHP_PHP tutorial
//Encode of GB2312
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
/*Focus on understanding the strtotime() function
1. strftime is easier to use than time(). It can directly convert the commonly used '2010-02-03' into a timestamp.
2. date() can display the time before 1970. Instead of using negative numbers as parameter 2
3. Date calculation can be transferred using timestamp. To calculate the number of days difference between two dates, you can get the difference timestamp and divide it by "24 hours * 60 minutes * 60" seconds, but it is more concise to use strtotime()
4. Learn how to create a calendar using PEAR. Omit it here.
Knowledge point: There is date(Y-m-d,-800) on the Internet to calculate the time before 1970, but the WINDOW system does not support negative values, so it will always return 1970-1-1 midnight.
*/
#PHP5 must set the default zone first.
date_default_timezone_set(ETC/GMT-8);
$nowdate=2010-02-23;
$lassdate = 2010-02-22;
The echo strftime() function outputs .strftime(%Y-%m-%d %H:%M:%S,time()).
;
The echo date() function outputs .date(Y-m-d H:i:s,time()).
;
//Check date: boolean checkdate(int month,int day,int year)
$d=2010-2-31;
echo $d.Yes.(checkdate(2,31,2010)? Valid date!: Invalid date!).
;
//Determine the number of days in the month
echo There are .date(t,time()).days this month
; //28 days
//Determine the number of days in any given month
$d=2008-02-01; //Leap year, or $d=2008-02; You don’t need to enter the day
$d=strtotime($d);
echo There are .date(t,$d).days in February 2008
; //29 days
$d=getdate();
echo
;<br> print_r($d);<br> echo;
/*Array(
[seconds] => 42
[minutes] => 16
[hours] => 13
[mday] => 23
[wday] => 2
[mon] => 2
[year] => 2010
[yday] => 53
[weekday] => Tuesday
[month] => February
[0] => 1266902202
)
*/
//echo date("Y-m-d H:i:s",-8000);
//setlocale(LC_ALL,zh_CN.gb2312); //The setlocale function has no effect on the following.
#Test strftime, mktime functions.
echo strftime(Today is: %Y-%m-%d %H:%M:%S).
;
echo strtotime(now).
; // Equivalent to time(), but the usage range of strtotime is more flexible, see below.
echo test to restore yesterday's time:.date(Y-m-d,strtotime($lassdate)).
; //You can convert the string date into a timestamp and then use date to convert it back to the original format.
$x=strtotime($lassdate);
$y=mktime(0,0,0,2,22,2010);
Yesterday's timestamp obtained by echo strtotime() is: .$x., yesterday's timestamp obtained by mktime() is: .$y.(($x==$y)?, the two are equal:, the two are not Same).
; //Equal.
#Show dates before 1970
$time_int=strtotime(1929-2-10);
echo date("Y-m-d ",$time_int).
; //The same function as the date() function in MYSQL is date_format(1996-02-05 11:07:45,%Y-%m -%d) or for_format()
/*Time operation:
*Please use method three. Other methods are for reference only. *
*/
#1. Today is the 23rd. Get the time of the day before yesterday, that is, minus two days.
$predate=2;
$pretime=$predate*24*60*60; //2-day timestamp.
echo date(The day before yesterday was: Y-m-d, time()-$pretime).
; //The day before yesterday was: 2010-02-21
#2, The number of days difference between two dates.
$olddate = 2010-02-11; //If you want to use the mktime function, you need to use explode to disassemble the date.
$oldtime = strtotime($olddate);
$passtime = time()-$oldtime; //Elapsed timestamp.
echo You've been online.floor($passtime/(24*60*60)) for days.
; //12 days.
#3. This time last year. Leap years should be taken into account when using: 365 days in an ordinary year and 366 days in a leap year.
#Method 1: Get the timestamp minus the number of days in the year.
$yDate=1;
$yDate_Y=date(Y,time())-1; //Year-1, that is, last year
$yDateYMD="$yDate_Y-01-01";
$yYMD=strtotime($yDateYMD); //The timestamp of January 1 last year.
$d=date(L,$yYMD)?366:365; //Is it a leap year
$yYearTime=$d*24*60*60;
$yYear=date(Y-m-d,time()-$yYearTime);
echo "Today last year: $yYear
"; //2009-02-23
#Method 2: Use the year that directly intercepts the current date and subtract one, but it is not rigorous and does not take leap years into account.
#Calculate 60 years ago today. Ignore any leap years that pass.
$yDate_Y=$yDate_Y-59;
$md=explode(-,date(Y-m-d));
$yYMD="$yDate_Y-{$md[1]}-{$md[2]}";
echo "60 years ago today: $yYMD
"; //1950-02-23
#Method 3: Use strtotime() and GNU date syntax---------Recommended!
//3 days later; //The current time is 2010-02-23
$d=strtotime(3 days);
echo 3 days later.date(Y-m-d,$d)."
";
//3 days ago:
$d=strtotime(-3 days);
echo 3 days ago.date(Y-m-d,$d)."
"; //2010-02-20
//One month ago:
$d=strtotime(-1 months);
echo one month ago.date(Y-m-d,$d)."
"; //2010-01-23
//2 months later:
$d=strtotime(2 months);
echo two months later.date(Y-m-d,$d)."
"; //2010-04-23
//1 year ago:
$d=strtotime(-1 years);
echo 1 year ago.date(Y-m-d,$d)."
"; //2009-02-23
//2 hours ago:
$d=strtotime(-2 hours);
echo Current: .date(Y-m-d H:i:s,time())., 2 hours ago.date(Y-m-d H:i:s,$d)."
"; //Currently: 2010 -02-23 13:38:49, 2 hours ago 2010-02-23 11:38:49
#DateTime constructor: object DateTime([string $time [,dateTimeZone $timezone])
$date = new DateTime(2010-02-23 12:26:36);
echo $date->format(Y-m-d H:i:s)."
"; //Same as date() function. 2010-02-23 12:26:36
//Reset time:
//1. Reset date: boolean setDate(int year,int month,int day)
//2. Reset time: boolean setDate(int hour,int minute[,int second])
$date->setDate(2010,2,28);
echo $date->format(Y-m-d H:i:s)."
"; //2010-02-28 12:26:36
//Date calculation, equivalent to strtotime()
above
$date->modify("+7 hours");
echo $date->format(Y-m-d H:i:s)."
"; //2010-02-28 19:26:36
$date->modify("3 days");
echo $date->format(Y-m-d H:i:s)."
"; //2010-03-03 19:26:36 //Start from the 28th that was changed above
/*PHP5 does not support the money_format function in WIN?
setlocale(LC_MONETARY,zh_CN);
echo money_format("%i",786.56);//?Fatal error: Call to undefined function money_format()
*/
?>

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

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

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











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,

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.

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

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.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.
