Home Backend Development PHP Tutorial PHP calculates the number of days difference between dates (the time difference between any time and the current time)

PHP calculates the number of days difference between dates (the time difference between any time and the current time)

Jul 25, 2016 am 08:51 AM

  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 $bjtimes<=$nowtimes){
  13. return ( floor(strftime("%j",mktime(0,0,0,$nowmon,$nowday,$nowyear)-mktime($da[3],$da[4],$da[5],$da[ 1],$da[2],$da[0]))));
  14. }else{
  15. $loop=$nowyear-$da[0];
  16. $totaldays=(floor(strftime("%j", mktime(0,0,0,$nowmon,$nowday,$nowyear)-mktime(0,0,0,1,1,$nowyear))));
  17. for($i=1;$i<=$ loop;$i++){
  18. for($j=12;$j>=1;$j--){
  19. if ($da[0]==$nowyear-$i and $da[1]==$ j){
  20. $days=MonDays($nowyear-$i,$j);
  21. return $totaldays+=$days-$da[2];
  22. break;
  23. }else{
  24. $days=MonDays($nowyear-$ i,$j);
  25. $totaldays+=$days;
  26. }//end else
  27. }//end for
  28. }//end for
  29. }//end else
  30. }//end function
  31. #Get the number of days in the month
  32. function MonDays($year,$month){
  33. switch ($month){
  34. case "1":
  35. case "3":
  36. case "5":
  37. case "7":
  38. case "8":
  39. case "10":
  40. case "12": $days=31;break;
  41. case "4":
  42. case "6":
  43. case "9":
  44. case "11": $days=30;break;
  45. case "2":
  46. if (checkdate($month,29,$year)){
  47. $days=29;
  48. }else{
  49. $days=28;
  50. }//end else
  51. break;
  52. }//end switch
  53. return $days;
  54. }//end function
  55. $datestr="2002-1-14 9:47:20";
  56. echo QueryDays($datestr);
  57. ?>
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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles