Introduction to date functions of PHP series learning

Source: Internet
Author: User
Tags epoch time unix epoch time
PHP has a wealth of library resources, which makes it easy for developers to process some businesses. Since we have the most contact with dates in the project, we will start with date functions today.
PHP is a very surprising language. It is powerful enough (the core language of the largest blog (wordpress) and extensive enough (running on facebook, the largest social networking website). It is simple enough (as the preferred language for beginners ). It runs well on low-cost machines. In addition, php has many very good server suites (such as WAMP and MAMP), which can be conveniently installed on your machine. PHP has a wealth of library resources, which makes it easy for developers to process some businesses. Since we have the most contact with dates in projects, we will start with date functions today.
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.
The code is as follows:
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:
The code is as follows:
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:
The code is as follows:

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:
The code is as follows:
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.
Special liking
If you start learning php, learning date is the best way to learn this language, because date has a lot of interesting things to deal. Explore date and time-related functions in the manual. please share your gains with us.

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.