search
HomeBackend DevelopmentPHP TutorialPHP calculates the number of days difference between dates (the time difference between any time and the current time)

  1. function count_days($a,$b){
  2. $a_dt=getdate($a);
  3. $b_dt=getdate($b);
  4. $a_new=mktime(12,0, 0,$a_dt['mon'],$a_dt['mday'],$a_dt['year']);
  5. $b_new=mktime(12,0,0,$b_dt['mon'],$b_dt[ 'mday'],$b_dt['year']);
  6. return round(abs($a_new-$b_new)/86400);
  7. }
  8. //How many days are there between today and October 11, 2008
  9. $date1= strtotime(time());
  10. $date1=strtotime('10/11/2008');
  11. $result=count_days($date1,$date2);
  12. echo $result;
  13. ?>
Copy code

Method 2:

  1. //How many days are there between today and September 9, 2008
  2. $Date_1=date("Y-m-d");
  3. $Date_2="2008-10-11";
  4. $d1 =strtotime($Date_1);
  5. $d2=strtotime($Date_2);
  6. $Days=round(($d2-$d1)/3600/24);
  7. echo "The difference between today and October 11, 2008". $Days."Days";
  8. ?>
Copy code

PHP Get the time difference between any time and the current time

Code:

  1. #Function: Get the time difference between any time and the current time
  2. function QueryDays($datestr){
  3. #Format time
  4. $da=preg_split("/(-| |:)/i ",$datestr);
  5. $nowyear=date("Y");
  6. $nowmon=date("n");
  7. $nowday=date("d");
  8. $nowtimes=mktime(0,0,0 ,$nowmon,$nowday,$nowyear);
  9. $pdtimes= mktime(0,0,0,$nowmon,$nowday,$nowyear-1);
  10. $bjtimes= mktime(0,0,0,$da[ 1],$da[2],$da[0]);
  11. #Judge whether the time given is within one year
  12. if ($bjtimes>=$pdtimes and $bjtimesreturn ( floor(strftime("%j",mktime(0,0,0,$nowmon,$nowday,$nowyear)-mktime($da[3],$da[4],$da[5],$da[ 1],$da[2],$da[0]))));
  13. }else{
  14. $loop=$nowyear-$da[0];
  15. $totaldays=(floor(strftime("%j", mktime(0,0,0,$nowmon,$nowday,$nowyear)-mktime(0,0,0,1,1,$nowyear))));
  16. for($i=1;$ifor($j=12;$j>=1;$j--){
  17. if ($da[0]==$nowyear-$i and $da[1]==$ j){
  18. $days=MonDays($nowyear-$i,$j);
  19. return $totaldays+=$days-$da[2];
  20. break;
  21. }else{
  22. $days=MonDays($nowyear-$ i,$j);
  23. $totaldays+=$days;
  24. }//end else
  25. }//end for
  26. }//end for
  27. }//end else
  28. }//end function
  29. #Get the number of days in the month
  30. function MonDays($year,$month){
  31. switch ($month){
  32. case "1":
  33. case "3":
  34. case "5":
  35. case "7":
  36. case "8":
  37. case "10":
  38. case "12": $days=31;break;
  39. case "4":
  40. case "6":
  41. case "9":
  42. case "11": $days=30;break;
  43. case "2":
  44. if (checkdate($month,29,$year)){
  45. $days=29;
  46. }else{
  47. $days=28;
  48. }//end else
  49. break;
  50. }//end switch
  51. return $days;
  52. }//end function
  53. $datestr="2002-1-14 9:47:20";
  54. echo QueryDays($datestr);
  55. ?>
Copy code

PHP Calculate the year, month, hour, minute and second difference between two times

How to calculate the difference between two times in years, months, hours, minutes and seconds in PHP

First convert the two times into timestamps, then subtract them to get the seconds difference between the two times, and finally do some arithmetic to get the year, month, day, hour, minute and second difference between the two times.

Code:

  1. $time1 = "2008-6-15 11:49:59"; //The first time
  2. $time2 = "2007-5-5 12:53:28"; //The second time
  3. $t1 = strtotime($time1);
  4. $t2 = strtotime($time2);
  5. $t12 = abs($t1-$t2);
  6. $start = 0;
  7. $string = " The difference between the two times: ";
  8. $y = floor($t12/(3600*24*360));
  9. if($start || $y )
  10. {
  11. $start = 1;
  12. $t12 -= $y *3600*24*360;
  13. $string .= $y."year";
  14. }
  15. $m = floor($t12/(3600*24*31));
  16. if($start || $m)
  17. {
  18. $start = 1;
  19. $t12 -= $m*3600*24*31;
  20. $string .= $m."month";
  21. }
  22. $d = floor($t12/(3600*24)) ;
  23. if($start || $d)
  24. {
  25. $start = 1;
  26. $t12 -= $d*3600*24;
  27. $string .= $d."天";
  28. }
  29. $h = floor ($t12/(3600));
  30. if($start || $h)
  31. {
  32. $start = 1;
  33. $t12 -= $h*3600;
  34. $string .= $h."hour";
  35. }
  36. $s = floor($t12/(60));
  37. if($start || $s)
  38. {
  39. $start = 1;
  40. $t12 -= $s*60;
  41. $string .= $s ."minutes";
  42. }
  43. $string .= "{$t12} seconds";
  44. echo $string;
  45. ?>
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.