Calculating Difference Between Two days And Display Particular Period of days Using PHP

By | September 23, 2014

In PHP some application depends on date , Calculating Diffence between two days and displaying a period of days or month or years.

We Are using to calculate difference between two days using DateTime class. This helpfull for many web application that depends on date time based.

Finding Difference between  two days:

1
2
3
4
5
6
7
$date1 = new DateTime('12-09-2014');
$date2 = new DateTime('23-09-2014');
$interval = $date1->diff($date2);
echo $interval->format('%a');

This Example will gives output 11 for the difference. diff() function used to find the difference from two days

Prininting period of days:

In that example period of days will shows class DateInterval();

And here used modify function which modify what we give inside function plus(+) or minus(-) days, months and year example $date->modify(“+2 day”) is add two days from its original date $date->modify(“-2 month”) reduce two months from original date.

We use the value P1D means  1 day interval we can change the day interval depend on our needs

Same as  Value P1M means 1 month interval and year for P1Y

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
$begin = new DateTime( '2012-08-01' );
$end = new DateTime('2012-08-01');
$end = $end->modify( '+100 day' );
echo "<b>Date</b><br>";
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){
echo $date->format("Y-m-d") . "   ";
}
$end = $end->modify( '+25 month' );
$interval = new DateInterval('P1M');
$daterange = new DatePeriod($begin, $interval ,$end);
echo "<br><b>Months</b>";
foreach($daterange as $date){
echo "<br><b>".$date->format("Y-m-d (D)") . "<b>";
}
$end = $end->modify( '+2 year' );
$interval = new DateInterval('P1Y');
$daterange = new DatePeriod($begin, $interval ,$end);
echo "<br><b>Years</b>";
foreach($daterange as $date){
echo "<br><strong style='color:red'>".$date->format("Y-m-d (D)") . "<strong>";
}

Here the modify function works add days or month or year

This code Out puts

Date
2012-08-01   2012-08-02   2012-08-03   2012-08-04   2012-08-05   2012-08-06   2012-08-07   2012-08-08   2012-08-09   2012-08-10   2012-08-11   2012-08-12   2012-08-13   2012-08-14   2012-08-15   2012-08-16   2012-08-17   2012-08-18   2012-08-19   2012-08-20
Months
2012-08-01 (Wed)
2012-09-01 (Sat)
2012-10-01 (Mon)
2012-11-01 (Thu)
2012-12-01 (Sat)
2013-01-01 (Tue)
2013-02-01 (Fri)
2013-03-01 (Fri)
2013-04-01 (Mon)
2013-05-01 (Wed)
2013-06-01 (Sat)
2013-07-01 (Mon)
2013-08-01 (Thu)
2013-09-01 (Sun)
2013-10-01 (Tue)
2013-11-01 (Fri)
2013-12-01 (Sun)
2014-01-01 (Wed)
2014-02-01 (Sat)
2014-03-01 (Sat)
2014-04-01 (Tue)
2014-05-01 (Thu)
2014-06-01 (Sun)
2014-07-01 (Tue)
2014-08-01 (Fri)
2014-09-01 (Mon)

Years
2012-08-01 (Wed)
2013-08-01 (Thu)
2014-08-01 (Fri)
2015-08-01 (Sat)
2016-08-01 (Mon)

One thought on “Calculating Difference Between Two days And Display Particular Period of days Using PHP

  1. Shahbaz Ahmed Bhatti

    very Goood and Nice post. Can u Please Start a Tutorial on Php Calender, how we can make our own calendar using Php
    Like
    Next October 1st Date Starting on Wednesday
    Using Php how i can do it

    it my 6 month old task, i coul dnot got in google that kind tutorial, so put it pending

    can u help me pease

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

anemotropism-uneminent