Date-time Functions in PHP (1/6) _php Tutorial

Source: Internet
Author: User
Tags echo date first string
Get the current date and time-how many ways do we have? 2. Change the way the date is displayed-date and time display Form 3, convert the current date to a Unix timestamp value of 4, change the date a. Increase time B. Subtract time C. Find the interval between two dates 5, add DateAdd function 6 for PHP, add DateDiff function for PHP

Get the current date and time-how many ways do we have?
2, change the way the date is displayed-the date and time of the display form
3. Convert the current date to a Unix timestamp value
4. Change the date
A. Adding time
B. Subtract time
C. Finding the interval between two dates
5. Add DateAdd function for PHP tutorial
6. Add DateDiff function to PHP

* * Get the current date and time

In Unix, time is expressed as the number of seconds elapsed since 0 o'clock January 1, 1970, which is known as the Unix Timestamp (Unix epoch).
If we have the code for this segment:

?
echo Time ();
?


will return a value of 958905820
And this time is May 21, 2000 12:43.
You might say this is pretty good. When it doesn't help me, or only a little help. In PHP, a date-processed function must use the timestamp value returned by Time (). Also, because PHP uses the same timestamp values in both UNIX and Windows systems, this allows you to migrate between different systems without having to modify the code. Another benefit is that the time () function returns an integer that you can save as an integer or text field into the database tutorial without having to use a special date/Time field.
Now that you have a basic understanding of Unix timestamp values, let's show you how it's actually used.

Change the way date is displayed-date and time display form

PHP provides two ways to convert the timestamp value of UNIX into useful data. The first one is the date () function. This function has two parameters-the first string is used to set the format you want to return, and the second is the timestamp value for UNIX.
The formatted string displays the date and time of the format you want to see by some simple special formatting characters. Suppose you want the date to display "18h01 Sunday" in such a format.
We need to use a special format character for each part of the string, which you can find in the PHP manual in the date and Time function library. There are a number of special formatting characters that they represent, such as the day of the week, the English name of the month, the year in 2 or 4 digits, whether it is morning (AM) or PM (PM), and so on. The special characters we need for this example are:
' H '-hour of 24 hours
' I '-minute
' L '-the English full name of the day of the week
' d '-the first day of the month
' F '-the full English name of the month
So our formatted string is "Hhi l D F" and the PHP code is:
?
echo Date ("Hhi l D F", Time ());
?
When we execute this code, we find that the result we get is:
180609 Sunday
The result looks a little strange. Let's check the PHP manual again, the original ' H ' represents the 12-hour hour. This proves again the truth: "The computer does what you tell it to do, not what you want it to do." We have two options. The first is to use the escape character "" in front of H:
echo Date ("Hhi l D F", Time ());

1 2 3 4 5 6

http://www.bkjia.com/PHPjc/445428.html www.bkjia.com true http://www.bkjia.com/PHPjc/445428.html techarticle get the current date and time-how many ways do we have? 2, change the way the date is displayed-the date and time of the Display Form 3, the conversion is now the date of the Unix timestamp value 4, change ...

  • Related Article

    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.