How To Calculate Days Between Two Dates In Php Code Example

Tutorials Algorithms Analysis of Algorithms Data Structures Languages Interview Corner GATE ISRO CS UGC NET CS CS Subjects Web Technologies Student Jobs

Bạn đang xem: How to calculate days between two dates in php code example

*

*

*

*

Given two dates, start_date và end_date. The task is to find the difference between two dates.

Examples:

Method 2: To use date-time mathematical formula lớn find the difference between two dates. It returns the years, months, days, hours, minutes, seconds between two specified dates.

Program:


// Declare và define two dates
// Formulate the Difference between two dates
$diff = abs($date2 - $date1);
// To get the year divide the resultant date inlớn
// total seconds in a year (365*60*60*24)
$years = floor($diff / (365*60*60*24));
// To get the month, subtract it with years &
// divide the resultant date into lớn
// total seconds in a month (30*60*60*24)
$months = floor(($diff - $years * 365*60*60*24)
/ (30*60*60*24));
// To get the day, subtract it with years và
// months và divide the resultant date into
// total seconds in a days (60*60*24)
$days = floor(($diff - $years * 365*60*60*24 -
$months*30*60*60*24)/ (60*60*24));
// To get the hour, subtract it with years,
// months và seconds và divide the resultant
// date into total seconds in a hours (60*60)
$hours = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24)
/ (60*60));
// To get the minutes, subtract it with years,
// months, seconds và hours và divide the
// resultant date into lớn total seconds i.e. 60
$minutes = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24
- $hours*60*60)/ 60);
// To get the minutes, subtract it with years,
// months, seconds, hours and minutes
$seconds = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24
- $hours*60*60 - $minutes*60));
// Print the result
printf("%d years, %d months, %d days, %d hours, "
. "%d minutes, %d seconds", $years, $months,
$days, $hours, $minutes, $seconds);
?>
Output:

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Method 3: This method is used to get the total number of days between two specified dates.


// Declare two dates
// Get the difference and divide into lớn
// number of days
echo "Difference between two dates: "
?>
Output:

Difference between two dates: 103
My Personal Notes arrow_drop_up
Save sầu
favorite_border Like
Next last_page
PHP | date_diff() Function
Recommended Articles
Page :
Return all dates between two dates in an array in PHP
21, Nov 18
How to store all dates in an array present in between given two dates in JavaScript ?
31, Jul 19
How khổng lồ calculate minutes between two dates in JavaScript ?
19, Jun 19

Xem thêm: Con Đường Chông Gai Để Trở Thành "Bạn Tình" Của Một Oiran Là Gì ?

How lớn calculate the number of days between two dates in javascript?
18, Jul 19
Program to find the number of days between two dates in PHP
17, Sep 18
PHPhường. | Number of week days between two dates
đôi mươi, Oct 18
Comparing two dates in PHP
11, Feb 19
How khổng lồ check if one date is between two dates in JavaScript ?
30, Dec 19
Calculating total number of Hours, Days, Minutes and Seconds between two dates in Golang
01, May 20
Select Pandas dataframe rows between two dates
23, Feb 21
Get the relative timestamp difference between dates in JavaScript
28, Jan 20
Sort an array of dates in PHP
22, Feb 18
Compare two dates using JavaScript
15, Aquảng bá 19
Java Program lớn Calculate Difference Between Two Time Periods
10, Dec 20
Compare Dates in Java
10, Oct 18
How to lớn select Min/Max dates in an array using JavaScript ?
31, Jul 19
How to get highcharts dates in the x-axis ?
10, Jun 20
Adding Dates & Times in Python
16, Feb 21
Sorting a CSV object by dates in Python
23, Feb 21
Java Program lớn Calculate the Difference Between the Sum of the Odd Level & the Even Level Nodes of a Binary Tree
20, Dec 20
How to calculate total time of an array in PHP.. ?
11, Oct 19
How to lớn run two PHPhường versions for two projects on same apađậy VPS ?
14, Oct 20
How to get a substring between two strings in PHP?
30, Sep 19
How lớn calculate percent minus px in SASS ?
27, Mar 20
Article Contributed By :
*

bilal-hungund
bilal-hungund
Vote for difficulty
Easy Normal Medium Hard Expert
Report Issue
What’s New
Data Structures and Algorithms – Self Paced Course
View Details
Ad-Free Experience – obatambeienwasirherbal.com Premium
View Details
Most popular in PHP
Most visited in PHPhường Programs

Writing code in comment? Please use ide.obatambeienwasirherbal.com, generate link và share the link here.


Load Comments
5th Floor, A-118, Sector-136, Noida, Uttar Pradesh - 201305
feedback
obatambeienwasirherbal.com
Company Learn Practice Contribute
obatambeienwasirherbal.com , Some rights reserved
We use cookies khổng lồ ensure you have the best browsing experience on our trang web. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It !