PHP development Notes Series (3)-date and time

Source: Internet
Author: User
Tags echo date
PHP development Notes Series (3)-date and time ??? The first two articles completed "PHP Development notes series (1)-PDO use" and "PHP Development notes series (2)-string use", today, I will take a look at the date and time processing in PHP and MySQL, "PHP development Notes Series (3)-date and time". ??? Date and time are commonly used functions. in JAVA, we use newDa PHP to develop the notes series (3)-date and time

??? The first two articles completed "PHP Development notes series (1)-PDO use" and "PHP Development notes series (2)-string use", today, I will take a look at the date and time processing in PHP and MySQL, "PHP development Notes Series (3)-date and time".

?

????? DateTime is a commonly used function. in JAVA, we can use new Date () to obtain the current time of the server, use the SimpleDateFormat class, and specify formatString, you can format the value of the date object in the specified format. Similarly, there are similar classes and functions in PHP, that is, the time function and the date function. Time () is similar to new Date () and can obtain the current time of the server. the date function is similar to the SimpleDateFormat class.

1. use the time function to get the current time of the server, and use the date function for formatting.

File: time. phpurl: http: // localhost: 88/datetime/timephp
 "; // Obtain the current default time zone echo date_default_timezone_get ()."
"; Echo date (" Y-m-d h: I: s ", $ time )."
"; Echo" "; // Set the current default value to date_default_timezone_set (" America/New_York "); echo date (" Y-m-d h: I: s ", $ time )."
";?>
?


2. construct a date and time using a string

File: strtotime. phpurl: http: // localhost: 88/datetime/strtotime. php
 "; // Use the date function to obtain the total number of days of the month and output echo 'total day of this month: '. date (" t ", $ time1 )."
";?>
?

3. date and time calculation based on the current time

File: date-compute.phpurl: http: // localhost: 88/datetime/date-compute.php.
 "; Echo 'last day: '. date (" Y-m-d h: I: s ", $ lastDay )."
"; Echo 'next month: '. date (" Y-m-d h: I: s ", $ nextMonth )."
"; Echo 'Last month: '. date (" Y-m-d h: I: s ", $ lastMonth )."
"; Echo 'next year: '. date (" Y-m-d h: I: s ", $ nextYear )."
"; Echo 'last year: '. date (" Y-m-d h: I: s ", $ lastYear )."
"; // Obtain the year echo 'current year: '. date (" Y ") through the date function;?>

?
4. process date and time in SQL

SELECT NOW()SELECT CURRENT_TIMESTAMP();SELECT DATE_FORMAT(NOW(), "%Y-%m-%d");SELECT DATE_FORMAT(NOW(), "%h:%i:%s");SELECT DATE_FORMAT(NOW(), "%W %w %p");

?

5. perform date calculation in SQL
??? Use the DATE_ADD function to calculate the future date

SELECT DATE_ADD(NOW(), INTERVAL 1 YEAR);SELECT DATE_ADD(NOW(), INTERVAL 1 MONTH);SELECT DATE_ADD(NOW(), INTERVAL 1 DAY);SELECT DATE_ADD(NOW(), INTERVAL 1 HOUR);SELECT DATE_ADD(NOW(), INTERVAL 1 MINUTE);SELECT DATE_ADD(NOW(), INTERVAL 1 SECOND);

?

??? Use the DATE_SUB function to calculate the past date

SELECT DATE_SUB(NOW(), INTERVAL 1 YEAR);SELECT DATE_SUB(NOW(), INTERVAL 1 MONTH);SELECT DATE_SUB(NOW(), INTERVAL 1 DAY);SELECT DATE_SUB(NOW(), INTERVAL 1 HOUR);SELECT DATE_SUB(NOW(), INTERVAL 1 MINUTE);SELECT DATE_SUB(NOW(), INTERVAL 1 SECOND);

?


??? The execution of the above functions can also be used in SQL statements. for example, to query the log information of the previous three days, the SQL statement is as follows:
SELECT * FROM log WHERE create_time between select DATE_SUB (NOW (), INTERVAL 3 DAY) and select DATE_ADD (NOW (), INTERVAL 1 DAY );

?

??? Address: http://ryan-d.iteye.com/blog/1543363

?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.