Date functions of PHP series Learning

Source: Internet
Author: User
Tags echo command epoch time unix epoch time
Introduction
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. For 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.
 
<! Doctype HTML> <HTML lang = "en"> Phpdate_default_timezone_set ('Asia/Shanghai');Echo"Today is ",Date('L');?> </Body>  

 

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 have read the PHP date function in the PHP manual, you will find many ways to format the date.
 
<? PHP echo "today is", date ('Y-m-d');?>

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.

 
<? PHPEcho"Today is ",Date(Date_cookie);?>

You will get the following content:

 
Today is Friday, 17-aug-12 11:34:38 CST

Note: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 ).
<? PHPEcho"The time is ",Date('G: I: Sa');?>

You will get

 
TheTimeIs 11:39:59 AM

 

Localization of 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 from your local. You need to specify the time zone on the server, then you use the following code:
 
<? PHP date_default_timezone_set ('Asia/Shanghai');?>

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.

 

You often need to get other time, instead of 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.
 
<? PHPEcho"Today is ",Date('Y-m-d ', 1309133434);?>

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.

 

There are many ways to create timestamps. We can use the mktime () function to obtain the timestamp we need.
 
<?PHP$ 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.

 

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:
<?PHP$ Mytime=Strtotime("PM June 26 2011");Echo"Today is ",Date('Y-m-d G: I: Sa ',$ Mytime);?>

Output:

 
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:

$ Nextfriday=Strtotime("Next Friday ");//Next Friday$ Nextmonth=Strtotime("+ 1 month ");//Calculate a month later from today$ Lastchristmas=Strtotime("-1 year Dec 25 ");//Last Christmas

 

The value returned by strtotime to obtain the date range is converted to a number. We can use these numbers for basic operations. We can use these numbers for many interesting tasks. For example, you need to teach a course every Tuesday for a period of 16 weeks. You can do the following.
<? PHP  $ Startdate = Strtotime ('Next Tuesday' );  $ Enddate = Strtotime ('+ 16 weeks ', $ Startdate  );  $ Currentdate = $ Startdate  ;  Echo '<Ol>' ;  While ( $ Currentdate < $ Enddate ): Echo "\ T <li> ", Date ('M d ', $ Currentdate  );  $ Currentdate = Strtotime ('+ 1 week ', $ Currentdate  );  Endwhile  ;  Echo '</OL>' ; ?>

You will get the following results:

Aug 21Aug28SEP04SEP11SEP18SEP25Oct02Oct09Oct16Oct23Oct30Nov06Nov13Nov20Nov27Dec04

 

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.

 

When using the calculator for the number of days to a certain date, we will try to calculate the number of days to a certain day. You can easily calculate the timestamp of the fourth Thursday of March.
$ 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.

 

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.

 

Recommendation

 

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.