PHP Notes: Introduction to the use of date functions _php tutorial

Source: Internet
Author: User
Tags epoch time php language time zones unix epoch time
Introduced

PHP is a very amazing language. It's strong enough (The biggest blog (WordPress) core language), it's broad enough (running on the biggest social networking site on Facebook), it's simple enough (as a beginner's preferred entry language). Works well on low-cost machines. And the PHP language has a lot of very good server suites (such as Wamp and Mamp), easy to install on your machine. PHP has a very rich library resource that makes it easy for developers to handle some business. Since we have the most contact with dates in the project, today we start with the date function.

to give 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 base code.
Copy CodeThe code is as follows:




<title>Getting started with dates in PHP5</title>


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 a different format

If you look at 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 commonly used, so PHP provides some constants for you to use. For example, you can use cookies to get to the client date.

You will get the content as below

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


What time is it now?

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

You will get

The time is 11:39:59am

Localize your time zone

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



This will set the time zone in Shanghai, China. This is the function of php5 (note the old version of PHP), there are a lot of time zones for you to choose. If you want to take effect permanently, you can modify your php.ini file.

Get other Time

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

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

The result is:

Today is 2011-06-27

This doesn't seem to be useful, but it means you can use the date () function to do the calculations. Before this, you need to simply create a timestamp.

Create timestamp

There are many ways to create timestamps. We can use the Mktime () function to get the timestamp we need.
Copy the Code code as follows:
$mytime =mktime (9, 23, 33, 6, 26, 2011);
echo "Today is", Date (' y-m-d g:i:sa ', $mytime);
?>

The resulting results are:

Today is 2011-06-26 9:23:33am mktime ()

The function requires you to sequentially pass the hour, minute, second, month, day, year. This is a good way to get a timestamp, but there's a cooler way.

Get timestamp by character

You can use the Strtotime () function to get timestamps, and PHP converts the readable characters into Unix timestamps. PHP is quite flexible in converting characters to timestamps, so you can insert a variety of values to get the timestamp you want.

This is a simple example:
Copy the Code code as follows:
$mytime =strtotime ("7:50pm June 26 2011");
echo "Today is", Date (' y-m-d g:i:sa ', $mytime);
?>

Output:

Today is 2011-06-26 7:50:00pm

PHP is quite dexterous in interpreting characters, but not perfect, so be sure to test the characters you enter before you insert them. It's a great way to use "english-like instructions" to convert to the required timestamp. You can do this as follows:
Copy the Code code as follows:
$nextfriday =strtotime ("Next Friday"); Next Friday
$nextmonth =strtotime ("+1 Month"); Calculate one months from today.
$lastchristmas =strtotime ("-1 year Dec 25"); Last Christmas

Get date Range

The value returned by the Strtotime is converted to a number, and we can use these numbers to do basic arithmetic, and we could do a lot of very interesting things with these numbers. For example, you need to teach a lesson every Tuesday for 16 weeks, and you want to teach time. You can do the following things.
Copy the Code code 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:
Copy CodeThe code is as follows:
21
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

Take a look at 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 instead of the default timestamp (today) and perform the operation.

Number of days to a certain date

When using a calculator, we try to calculate the number of days to a certain day. You can easily calculate the timestamp of the fourth Thursday in November.
Copy the Code code as follows:
$someday = Strtotime ("3 weeks Thursday November 1");
$daysUtilDate = Ceil (($someday-time ())/60/60/24);
echo "There is", $daysUtilDate, "until Thanksgiving";

First, we began to calculate the Thanksgiving holiday (3rd Thursday after the first Thursday after November 1), and then we calculated the number of days between Thanksgiving and the current time through simple arithmetic. When we perform a comparison operation, we can use time (), because it returns the number of ERAs in the current period.

http://www.bkjia.com/PHPjc/326804.html www.bkjia.com true http://www.bkjia.com/PHPjc/326804.html techarticle the introduction of PHP is a very surprising language. It's strong enough (The biggest blog (WordPress) core language), it's broad enough (running on the biggest social networking site on Facebook), it's full ...

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