PHP mktime and Strtotime

Source: Internet
Author: User
Tags vars

This article to introduce you to the use of PHP with Strtotime or mktime specified date data (this week, last week, this month, last month, this quarter) instance, I hope that the students will help you.

Strtotime Definition and usage

The strtotime () function resolves the datetime description of any English text to a Unix timestamp.

Syntax:strtotime (Time,now), time specifies the timestamp to parse, now is used to calculate the timestamp of the return value, and if omitted, the current time is used.

The instance code is as follows:

  1. echo Date ("y-m-d",strtotime ("Now")), "";
  2. echo Date ("y-m-d",strtotime ("1 week Monday")), ""; //Closest to the current Monday
  3. echo Date ("y-m-d",strtotime ("1 week Sunday")), ""; //The most recent weekend from now
  4. echo Date ("y-m-d",strtotime ("+0 Week Monday")), ""; //The Monday that will come
  5. echo Date ("y-m-d",strtotime ("+0 Week Sunday")), ""; //The coming weekend
  6. echo Date ("n"); The first few months
  7. echo Date ("w"); Week of Week
  8. echo Date ("T"); Days of the Month
  9. Echo "Last week:";

Mktime function

The Mktime () function returns a Unix timestamp for a date, and the parameter always represents the GMT date, so IS_DST has no effect on the result, the parameter can be left-to-right and empty, and the empty argument is set to the corresponding current GMT value.

Syntax:mktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,IS_DST)

The instance code is as follows:

  1. echo Date ("y-m-d h:i:s",mktime (0, 0, 0,date ("M"),date ("D")-Date ("W") +1-7,   Date ("Y"))),"";
  2. echo Date ("y-m-d h:i:s",mktime (23,59,59,date ("M"),date ("D")-Date ("W  ") +7-7,date (" Y "))," ";
  3. echo "
  4. Week:
  5. ";
  6. echo Date ("y-m-d h:i:s",mktime (0, 0, 0,date ("M"),date ("D")-Date ("W") +1,   Date ("Y"))),"";
  7. echo Date ("y-m-d h:i:s",mktime (23,59,59,date ("M"),date ("D")-Date ("W") +7,  Date ("Y"))),"";
  8. echo "
  9. Last month:
  10. ";
  11. echo Date ("y-m-d h:i:s",mktime (0, 0, 0,date ("M") -1,1,date ("Y")),"";
  12. echo Date ("y-m-d h:i:s",mktime (23,59,59,date ("M"), 0,date ("Y")),"";
  13. echo "
  14. Month:
  15. ";
  16. echo Date ("y-m-d h:i:s",mktime (0, 0, 0,date ("M"), 1,date ("Y")),"";
  17. echo Date ("y-m-d h:i:s",mktime (23,59,59,date ("M"), Date ("T"),date("Y  "))),"";
  18. $getMonthDays = Date ("T",mktime (0, 0, 0,date ("n") + (date ("n")-1)%3,1,date ("Y")) ); //Not last month of the quarter days
  19. echo "
  20. This quarter:
  21. ";
  22. echo Date (' y-m-d h:i:s ', mktime (0, 0, 0,date (' n ')-(date (' n ')-1)%3,1,date (' Y '))  ,"";
  23. echo Date (' y-m-d h:i:s ', mktime (23,59,59,date (' n ') + (date (' n ')-1)%3,$  Getmonthdays,date (' Y ')),"";
  24. $jdtoday = Gregoriantojd (date (' n '), date ('j '), date (' y '));
  25. $offset = Jddayofweek ($jdtoday)-1;
  26. For ($i =0-$offset; $i <7-$offset; $i + +) {  
  27. $date = strtotime ($i.' days ');
  28. Echo ("". Date (' D ', $date).' /‘.  date (' n-j ', $date).
  29. }
  30. ?>

Parameter description

Hour is optional. Specified hours.

Minute is optional. Specify minutes.

Second is optional. Specify seconds.

Month is optional. Specifies the number of months to be represented.

Day is optional. Prescribed days.

Year is optional. Prescribed year.

On some systems, the legal value is between 1901-2038, but there is no such limit in PHP 5.

IS_DST is optional. If the time is in daylight saving time (DST), set to 1, otherwise set to 0 and if unknown, set to-1.

Since 5.1.0, the IS_DST parameter has been deprecated, so the new Time zone processing feature should be used.

PHP mktime and Strtotime

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.