PHP Development Notes Series (iii)-Date and time

Source: Internet
Author: User
Tags echo date
PHP Development Note Series (iii)-Date and time

??? The first two completed "PHP Development Note Series (i)-pdo use" and "PHP Development Note Series (ii)-string use", today began to study the date and time in PHP and MySQL date and time processing, "PHP Development Note Series (three)-date and time."

?

????? datetime is a more commonly used function, in Java we can get the current time of the server through the new date (), through the SimpleDateFormat class, and specify formatstring, you can format the value of the Date object into the specified form. Similarly, in PHP, there are similar classes and functions, that is, the time function and the date function. Time () is similar to the new date (), and is able to get the server's current date, similar to the SimpleDateFormat class.

1. Use the time function to get the current date of the server and format it using the DATE function

file:time.phpurl:http://localhost:88/datetime/timephp
 
   ";       Gets the current default is the time zone    Echo date_default_timezone_get ().
"; echo Date ("Y-m-d h:i:s", $time). "
"; echo " "; Setting the current default is the time zone date_default_timezone_set ("America/new_york"); echo Date ("Y-m-d h:i:s", $time). "
";? >
?


2. Constructing datetime by string

file:strtotime.phpurl:http://localhost:88/datetime/strtotime.php
 
   ";        Use the date function to get the total number of days in the month and output    the echo ' Totals day of this month: '. Date ("T", $time 1). "
";? >
?

3. Date-time calculations 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). "
"; Use the date function to get the year echo ' current years: '. Date ("Y"); >

?
4. Working with date 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 calculations in SQL
??? Calculation of future dates using the Date_add function

SELECT Date_add (now (), INTERVAL 1); 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);

?

??? Using the Date_sub function to calculate past dates

SELECT Date_sub (now (), INTERVAL 1); 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, such as the three days before the query log information, SQL is as follows:
SELECT * FROM log WHERE create_time between SELECT date_sub (now (), INTERVAL 3 day) and select Date_add (now (), INTERVAL 1 D AY);

?

??? This 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.