PHP notes: Introduction to date functions _ php instances

Source: Internet
Author: User
Tags epoch time unix epoch time
This article will introduce the use of date functions in PHP notes. For more information, see 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.

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 Today is Friday 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.



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.

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

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 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 2011-06-26 9:23:33 am mktime ()

The 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 );
?>


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:

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;
Echo'

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


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.

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.