Getting Started with PHP -022-key date function get localization timestamp function. MD

Source: Internet
Author: User
Tags echo date getting started with php

In the actual work we also need to often use the specified time to generate

For example, you need to find a registered user yesterday to this moment.

We need to do two things:

    • Gets the current time of the Unix timestamp. Use the time () function to directly handle
    • So how did you generate the time you specified yesterday? This is the time we need to use mktime()函数 . Abbreviation: Make time.

The best time to generate is a UNIX timestamp. Because it is January 1, 1970 0 o'clock to the present time. We make an interval judgment, the user registered yesterday to today by the time filter out.

Our Mktime () function can obtain a localized timestamp for a date and time. Its syntax format is as follows:

int mktime (int $hour [, int $minute [, int $second [, int $month [, int $day [. int$year [, int $.is_dstl.l}] [31]

The parameters of the function are: hours, minutes, seconds, months, days, years, daylight saving time. When using this function, it is important to note that the parameters listed are the same as the parameters of the function. For example, the following code implements the ability to construct a timestamp with mktime.

    1. <?php
    2. echo mktime (13,15,30,8,18,2014);
    3. ?>

The return result of the Mktime function is a UNIX timestamp that does not mean much to the user, and is often done with the date function to convert the time.

For example, the following code implements the operation of the time:

  1. <?php
  2. echo date ( "m-d-y h:m:s" "\ n"
  3. echo date ( "m-d-y h:m:s" mktime ( 10 15 Span class= "pun", 35 date Span class= "pun" > ( "M" date Span class= "pun" > ( "D" date Span class= "pun" > ( "Y" "\ n"
  4. echo date("m-d-Y h:m:s",mktime(10,15,35,date("m"),date("d")-30,date("Y")))."\n";
  5. ?>

When we use it, we often need to use another function: Strtotime ().

Its syntax format is as follows:

int Strtotime (string $time [, int $now = time ()])

It can parse the date-time description of the English text into a Unix timestamp.

Parameters:

    • Time to pass in a string
    • The optional parameter is whether to pass in the Unix timestamp, or the current UNIX timestamp if not passed.

Let's experiment and look at some of the examples provided in the manual:

  1. <?php
  2. //now为现在的当前时间
  3. echo strtotime("now")."<br />";
  4. //2000年9月10日
  5. echo strtotime("10 September 2000")."<br />";
  6. //当前时间加一天
  7. echo strtotime("+1 day")."<br />";
  8. //当前时间加一周
  9. echo strtotime("+1 week")."<br />";
  10. //当前时间加一周2天4小时2秒
  11. echo strtotime("+1 week 2 days 4 hours 2 seconds")."<br />";
  12. //下一个星期四
  13. echo strtotime("next Thursday")."<br />";
  14. //上一个星期一
  15. echo strtotime("last Monday")."<br />";
  16. ?>





Getting Started with PHP -022-key date function get localization timestamp function. MD

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.