Date and time functions in php (1/6)

Source: Internet
Author: User
Tags add time manual echo date first string php code php tutorial

Get the current date and time-how many methods do we have?
2. Change the date display mode-date and time display form
3. Convert the current date to the unix timestamp value
4. Change date
A. Add time
B. Minus the time
C. Find the interval between two dates
5. Add the dateadd function for the php Tutorial.
6. Add the datediff function for php

** Get the current date and time.

In unix, the representation of time is the number of seconds that have elapsed since, January 1, January 1, 1970. This is called a unix timestamp (unix epoch ).
If we have such a piece of code:

The code is as follows: Copy code
?
Echo time ();
?


Returns 958905820
The time is, January 1, May 21, 2000.
You may say this is pretty good. If this is not helpful to me, or it is only helpful. In php, all date-processing functions must use the timestamp value returned by time. At the same time, because php uses the same timestamp value in both unix and windows systems, this allows you to port between different systems without modifying the code. Another benefit is that the time () function returns an integer. You can save it as an integer or text field to the database tutorial without using a special date/time field.
You have basically understood the unix timestamp value. Now Let's demonstrate its actual usage.

Change the date display mode-date and time display form

Php provides two methods to convert unix timestamp values into useful data. The first 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 unix timestamp value.
The formatted string uses some simple special formatting characters to display the date and time of the format you want to see. Suppose you want the date to display "18h01 Sunday 21 may" in this format ".
We need to use a special formatted character for each part of the string. You can find it in the date and time function library in the php Manual. There are a large number of special formatting characters, which are similar to the day of the week, the English name of the month, the year expressed in two or four digits, whether it is morning (am) or afternoon (pm) and others. For this example, we need the following special characters:
'H'-hour in 24-hour format
'I'-minutes
'L'-full name of the day of the week
'D '-the day of the month
'F'-English full name of the month
Therefore, the 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 21 may
The result looks strange. Let's look at the php manual again. The original 'h' represents the 12-hour. This once again proves the truth: "computers only do what you tell them, not what you want them to do ". We have two options. The first is to use the escape character "" before h:
Echo date ("hhi l d f", time ());

Home 1 2 3 4 5 6 Last page

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.