Categories

# Calculating Dates

Learn how to calculate the difference between two date and time strings. Also, how to find the leap year from a list of years.

1. Calculating difference between two dates with DateTime class
2. Calculatng manually the difference between two dates
3. Finding leap year

## Calculating difference between two dates with DateTime class

Sometimes you need to calculate the difference between two dates, you can use the `diff()` method of `DateTime` class to perform the task:

Example: Object-oriented way to find difference between two dates

```<?php
\$a = new DateTime('9th Jun 2017 3:59:08AM');
\$b = new DateTime('11th August 2022 07:42:09PM');
\$o = \$a->diff(\$b);

echo \$o->y.' years, '.\$o->m.' months, '.\$o->d.
'days, '.\$o->h.' hours, ' .\$o->i.' minutes, and'.
\$o->s .' seconds. Total days: '. \$o->days;```

The DateTime::diff() method returns DateInterval object that can be formatted with DateInterval::format() method.

Example: Procedural way to calculate the difference between two dates

```<?php
\$a = date_create('9th Jun 2017 3:59:08AM');
\$b = date_create('11th August 2022 07:42:09PM');
\$o = date_diff(\$a, \$b);

echo \$o->y.' years, '.\$o->m.' months, '.\$o->d.
'days, '.\$o->h.' hours, ' .\$o->i.' minutes, and'.
\$o->s .' seconds. Total days: '. \$o->days;```

Both examples print the following output on web browsers:

``` # 5 years, 2 months, 2days, 15 hours,
43 minutes, and1 seconds. Total days: 1889```

## Manually calculating the difference between two dates

The epoche value (Unix timestamp) can be used to easily calculate the difference between two dates. The trick is to convert the dates into timestamps, then the difference between these two timestamps is calculated. The result is the time difference in seconds.

```<?php
\$prevDate = strtotime('Dec, 1 1998 12:00PM');
\$nextDate = mktime(12, 0, 0, 1, 1, 2022);;
\$diference = \$nextDate - \$prevDate;```

This value can then be used to find out how many minutes, hours, and days this corresponds to:

• 1 minute = 60 seconds
• 1 hour = 60 minutes = 60 x 60 = 3600 seconds
• 1 day = 24 hours = 24 x 60 x 60 = 86400 seconds

Example: Calculating the Difference Between Two Dates

In this example, we used the `strtotime()` function, you can use a different function for generating the timestamp. We just round down each result and subtract it from the result to split up the difference into days, hours, and minutes.

```<?php
\$prevDate = '9th Aug 2022 06:41:08AM';
\$nextDate = '11th August 2022 07:42:09PM';

# Difference between two days in seconds
\$difference = strtotime(\$nextDate) - strtotime(\$prevDate);

# Convert seconds into days
\$days =  floor(\$difference / 84600);

# Subtract days (in seconds) from the \$difference
\$difference -= 84600 * floor(\$difference / 84600);

# Convert remaining seconds into hours
\$hours = floor(\$difference / 3600);

# Subtract hours
\$difference -= 3600 * floor(\$difference / 3600);

# Convert remaining seconds into minutes
\$minutes = floor(\$difference / 60);

# Subtract minutes, the remaining difference will be seconds
\$difference -= 60 * floor(\$difference / 60);

echo "Difference between \$prevDate and \$nextDate is:<br>".
"days: \$days, hours: \$hours, seconds: \$difference";
/* Difference between 9th Aug 2022 06:41:08AM
and 11th August 2022 07:42:09PM is:
days: 2, hours: 14, seconds: 1 */```

## Finding leap year

If a year is divisible by 4 and is either not divisible by 100 or is divisible by 400, it is a leap year and February has 29 days (this is because the Earth needs approximately 365.25 days to revolve around the sun). If you want to determine whether a given year is a leap year, this function comes in handy:

```<?php
function isLeapYear(\$year) {
return
(\$year % 4 == 0 &&
(\$year % 100 != 0 ||
\$year % 400 == 0));
}

\$years = range (2020, 2040);
foreach (\$years as \$year){
if (isLeapYear(\$year))
echo \$year . ' ';
}
# Prints: 2020 2024 2028 2032 2036 2040```

The Date and Time Tutorials: