How to use Strtotime function in PHP

Source: Internet
Author: User
Tags echo date
How to use Strtotime function in PHP




  1. There is a function called Strtotime in PHP. Strtotime implementation: Gets the timestamp of a date, or gets the timestamp of a time. Strtotime to parse the datetime description of any English text into a UNIX timestamp [convert system time to UNIX timestamp]
  2. One, gets the UNIX timestamp for the specified date
  3. Strtotime ("2009-1-22") examples are as follows:
  4. 1.echo strtotime ("2009-1-22")
  5. Results: 1232553600
  6. Description: Return January 22, 2009 0:0 0 seconds timestamp
  7. Second, get the English text date time
  8. Examples are as follows:
  9. Easy to compare, use date to convert time stamp to system time with specified timestamp
  10. (1) Print the timestamp for tomorrow at this time Strtotime ("+1 Day")
  11. Current time:
  12. 1.echo date ("Y-m-d h:i:s", Time ())
  13. Result: 2009-01-22 09:40:25
  14. Specify time:
  15. 1.echo date ("Y-m-d h:i:s", Strtotime ("+1 Day")
  16. Result: 2009-01-23 09:40:25
  17. (2) Print the timestamp at this time yesterday Strtotime ("-1 day")
  18. Current time:
  19. 1.echo date ("Y-m-d h:i:s", Time ())
  20. Result: 2009-01-22 09:40:25
  21. Specify time:
  22. 1.echo date ("Y-m-d h:i:s", Strtotime ("1 day"))
  23. Result: 2009-01-21 09:40:25
  24. (3) Print timestamp strtotime ("+1 Week") at this time next week
  25. Current time:
  26. 1.echo date ("Y-m-d h:i:s", Time ())
  27. Result: 2009-01-22 09:40:25
  28. Specify time:
  29. 1.echo date ("Y-m-d h:i:s", Strtotime ("+1 Week"))
  30. Result: 2009-01-29 09:40:25
  31. (4) Print timestamp strtotime ("1 week") at this time last week
  32. Current time:
  33. 1.echo date ("Y-m-d h:i:s", Time ())
  34. Result: 2009-01-22 09:40:25
  35. Specify time:
  36. 1.echo date ("Y-m-d h:i:s", Strtotime ("1 week"))
  37. Result: 2009-01-15 09:40:25
  38. (5) Print the time stamp specified next week strtotime ("next Thursday")
  39. Current time:
  40. 1.echo date ("Y-m-d h:i:s", Time ())
  41. Result: 2009-01-22 09:40:25
  42. Specify time:
  43. 1.echo date ("Y-m-d h:i:s", Strtotime ("next Thursday"))
  44. Result: 2009-01-29 00:00:00
  45. (6) Print the timestamp strtotime ("last Thursday") specified on the previous day of the week
  46. Current time:
  47. 1.echo date ("Y-m-d h:i:s", Time ())
  48. Result: 2009-01-22 09:40:25
  49. Specify time:
  50. 1.echo date ("Y-m-d h:i:s", Strtotime ("last Thursday"))
  51. Result: 2009-01-15 00:00:00
  52. The above example shows that Strtotime can parse the datetime description of any English text into a Unix timestamp, and we get the specified timestamp by combining mktime () or date () format date time to achieve the required date time.
  53. Hopefully, after the introduction of this article, you can already master the Strtotime function usage.
Copy Code
  • 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.