PHP date function learning notes-PHP source code

Source: Internet
Author: User
Tags echo command epoch time unix epoch time
Ec (2); for a simple date example & nbsp; I will use the echo command to output the content to our client (browser ). I will use the following code as the basic code. & Nbsp; copy the following code & lt ;! DOCTYPEhtml & gt; & lt; htmllang & quot; en & quot; & gt; & lt; head & gt; & nbsp; & script ec (2 ); script

A simple date example

I will use the echo command to output the content to our client (browser ). I will use the following code as the basic code.

The Code is as follows:

Getting started with dates in php5

Date_default_timezone_set ('Asia/Shanghai ');
Echo "Today is", date ('l ');

You will see the following content in your browser.

Today is Friday
This function outputs the text format of the day of the week. The date function requires at least one character parameter (this parameter tells us how to format the current date ).

Try different formats

If you read the php date function in the PHP manual, you will find many ways to format the date.

The Code is as follows:

Will get

Today is 2012-08-17

Some dates are widely used, so PHP provides constants for you. For example, you can use cookies to get the client date.

The Code is as follows:

You will get the following content:

Today is Friday, 17-Aug-12 11:34:38 CST
Note that do not use quotation marks when using constants.

When is it?

If you want to output the current time, you can use date (different formatting character parameters ).

The Code is as follows:

You will get

The time is 11:39:59 am

Localized Your Time Zone

If you find that the above Code does not give the correct time, it is likely that your server has set a different time zone than your local. You need to specify the time zone on the server, then you use the following code:

This will set China's Shanghai time zone. This is a php5 function (pay attention to the old version of php), there are many for you to choose the time zone. If you want to take effect permanently, you can modify your php. ini file.

Obtain other time

You often need other time, not the current time. When you use the date () function to create a time, the system uses the Unix system time. This time indicates the number of seconds from January 1, 1970 00:00:00 GMT (Unix epoch time) to the present.

To describe how to obtain the date of the specified time in detail, you can provide the number of seconds as the second parameter of date (0 function.

The Code is as follows:

The result is:

Today is 2011-06-27
This seems useless, but it means you can use the date () function for calculation. Before that, you need to create a timestamp.

Create Timestamp

There are many ways to create timestamps. We can use the mktime () function to obtain the timestamp we need.

The Code is as follows:
$ Mytime = mktime (9, 23, 33, 6, 26,201 1 );
Echo "Today is", date ('Y-m-d g: I: Sa', $ mytime );

The result is:

Today is 9:23:33 am
The mktime () function requires you to pass the hour, minute, second, month, day, and year in sequence. This is a good way to get the timestamp, but it is cool.

Get timestamp by character

You can use the strtotime () function to obtain the timestamp. php converts readable characters to Unix timestamps. PHP is quite flexible in converting characters into timestamps, so you can insert various values to get the timestamp you want.

This is a simple example:

The Code is as follows:

$ Mytime = strtotime ("pm June 26 2011 ");
Echo "Today is", date ('Y-m-d g: I: Sa', $ mytime );

Today is 7:50:00

PHP is quite clever in interpreting characters, but not perfect. Therefore, you must test the characters you enter before inserting them. Converting "english-like instructions" to the required timestamp is a very good method. You can do this as follows:

The Code is as follows:
$ Nextfriday = strtotime ("next Friday"); // next Friday
$ Nextmonth = strtotime ("+ 1 Month"); // calculate the time after one Month from today
$ Lastchristmas = strtotime ("-1 year dec 25"); // last Christmas

Get date range

The value returned by strtotime is converted to a number. We can use these numbers for basic operations. We can use these numbers to do a lot of interesting things. For example, you need to teach a course every Tuesday for a period of 16 weeks. You can do the following.

The Code is as follows:

$ Startdate = strtotime ('Next Tuesday ');
$ Enddate = strtotime ('+ 16 weeks', $ startdate );
$ Currentdate = $ startdate;

    While ($ currentdate <$ enddate ):
    Echo "t
  1. ", Date ('m d', $ currentdate );
    $ Currentdate = strtotime ('+ 1 week', $ currentdate );

You will get the following results:

Aug 21
Aug 28
Sep 04
Sep 11
Sep 18
Sep 25
Oct 02
Oct 09
Oct 16
Oct 23
Oct 30
Nov 06
Nov 13
Nov 20
Nov 27
Dec 04

Note This line: $ currentdate = strtotime ("+ 1 week", $ currentdate ). In this line, you will find that you need to specify a timestamp as the second parameter. strtotime will use this parameter to replace the default timestamp (today) and perform operations.

The number of days to a certain date

When using the calculator, we will try to calculate the number of days in a certain day. You can easily calculate the timestamp of the fourth Thursday of March.

The Code is as follows:
$ Someday = strtotime ("3 weeks Thursday November 1 ");
$ DaysUtilDate = ceil ($ someday-time ()/60/60/24 );
Echo "There are", $ daysUtilDate, "until Thanksgiving ";

First, we start to calculate the Thanksgiving Day date (the first Thursday after January 1, 3rd). Then we calculate the number of days between Thanksgiving Day and the current time through simple arithmetic. When we perform a comparison operation, we can use time () because it returns the epoch number of seconds from the current time.

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: 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.