PHP Calculation time difference PHP Date conversion example

Source: Internet
Author: User

To achieve a calculation of the time difference in the code, the number of days, I remember in other programs to see the conversion into years and days, so began to write a bit, in fact, is very simple, is a few conversion, But in the monthly statistics is not very accurate, because the month, do not know how many days to calculate, so I replaced the month 30 days conversion, the error should not be small, this is impossible to solve.

Example, the code for calculating the station time: $jztime station time format: 2013-01-01

  1. !--? php $days = ABS (Strtotime ($jztim e)-Strtotime (Date ("y-m-d")))/86400;
  2. if ($days >365) {
  3. $site _y=intval ($days/365). " Year-";
  4. }
  5. if ($days >31) {
  6. $str =explode (".", $days/365);//obtain data for the remainder of the year
  7. $site _m = ("0.". $str [(Count ($STR)-1)] *365/30;
  8. $site _m = intval ($site _m). " Month-";
  9. } bbs.it-home.org
  10. if ($days >1) {
  11. $str =explode (".", $days/365);//continue to get the rest of the data
  12. $site _d = ("0."). $str [(Count ($STR)-1)]) *365-(intval ($site _m) *30);
  13. $site _d = intval ($site _d). " Days ";
  14. }
  15. $dayst = $site _y. $site _m. $site _d;
  16. echo $dayst;
  17. ?
copy code
  • 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.