time-php distance A time countdown, there is error, how to solve???

Source: Internet
Author: User
Tags diff

  
   ';$end=strtotime('2018-8-16 00:00:00');$d=$end-$now;$y=floor($d/3600/24/30/12);$m=($d/3600/24/30)%12;$dt=($d/3600/24)%30;echo "距世界杯还有{$y}年{$m}个月{$dt}天";

How can you write if you have an error in writing this month?
Does this error have an effect?

Reply content:


  
   ';$end=strtotime('2018-8-16 00:00:00');$d=$end-$now;$y=floor($d/3600/24/30/12);$m=($d/3600/24/30)%12;$dt=($d/3600/24)%30;echo "距世界杯还有{$y}年{$m}个月{$dt}天";

How can you write if you have an error in writing this month?
Does this error have an effect?

DateTime can be used to calculate a diff of two dates very quickly and easily.


  
   diff($datetime2);list($y, $m, $d) = explode('-', $interval->format('%Y-%m-%d'));echo "距世界杯还有{$y}年{$m}个月{$d}天";

The key is the logic is unclear, February 1 to March 1 Whether it is one months or 28 (29) days?

There's a crooked way to get the worth. Date parsing, and 1970-1-1 comparison


  
   

https://3v4l.org/1QEbC

Why use time ()
Why not go to the wee hours of the day?

  $y =floor ($ D/3600/24/30/12); This year is a bit far-fetched, some months more than 30 days.  

If your machine time is not very bad, you should not have much error.

  $dt = ($d/3600/24)%30; There's something wrong with that.  

For a time, you can use the third-party library of Carbon for quick calculations.

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