Home  >  Article  >  Backend Development  >  Php calculate time difference php date conversion example

Php calculate time difference php date conversion example

WBOY
WBOYOriginal
2016-07-25 09:13:16989browse

I implemented a code to calculate the time difference and counted the number of days. I remembered that I had seen the conversion into months and days in other programs, so I started writing it. It was actually very simple, just a few conversions, but it was not in monthly statistics. It's very accurate, because I don't know how many days it is calculated when I convert it into a month, so I converted it into a month with 30 days. The error shouldn't be big, but this cannot be solved.

Example, code to calculate website building time: $jztime website creation time format: 2013-01-01

  1. $days = abs(strtotime($jztime) - 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);//Get the remaining data of the year
  7. $site_m = ( "0.".$str[(count($str)-1)])*365/30;
  8. $site_m = intval($site_m)."月-";
  9. } bbs.it-home.org
  10. if ($days>1){
  11. $str=explode(".",$days/365);//Continue to obtain the remaining data
  12. $site_d = ("0.".$str[(count($str)- 1)])*365-(intval($site_m)*30);
  13. $site_d = intval($site_d)."Day";
  14. }
  15. $dayst= $site_Y.$site_m.$site_d;
  16. echo $dayst ;
  17. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn