Calculate days from current date in php

If I have a given date in the format yyyy-mm-dd, how can I calculate the difference in days to the current date ?

I just want to check whether this date is more than one week (7 days) old compared to the current date.

Calculate days from current date in php

halfer

19.6k17 gold badges92 silver badges175 bronze badges

asked Nov 16, 2013 at 8:04

user2571510user2571510

10.8k38 gold badges86 silver badges134 bronze badges

1

1 Answer

date_default_timezone_set('Europe/Warsaw');
$from = strtotime('2013-11-01');
$today = time();
$difference = $today - $from;
echo floor($difference / 86400);  // (60 * 60 * 24)

or

date_default_timezone_set('Europe/Warsaw');
$datetime1 = new DateTime('2013-11-01');
$datetime2 = new DateTime('2013-11-15');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%a');

jklemmack

3,4582 gold badges30 silver badges53 bronze badges

answered Nov 16, 2013 at 8:11

0

In this article, we will see how to get the date difference in the number of days in PHP, along with will also understand the various ways to get the total count of difference in 2 dates & see their implementation through the examples. We have given two dates & our task is to find the number of days between these given dates. For this, we will be following the below 2 methods:

  • Using strtotime() Function
  • Using date_diff() Function

Consider the following example:

Input : date1 = "2-05-2017"
        date2 = "25-12-2017"
Output: Difference between two dates: 237 Days
Explanation: Calculating the total number of days between the start & end date.

Note: The dates can be taken in any format. In the above example, the date is taken in dd-mm-yyyy format.

Method 1: Using strtotime() Function

This is a built-in function in PHP that is used to convert an English textual date-time description to a UNIX timestamp. The function accepts a string parameter in English which represents the description of date-time. For e.g., “now” refers to the current date in the English date-time description. The function returns the time in seconds since the Unix Epoch

Example 1: In this example, we have taken two dates and calculated their differences.

PHP

  function dateDiffInDays($date1, $date2

  {

      $diff = strtotime($date2) - strtotime($date1);

      return abs(round($diff / 86400));

  }

  $date1 = "17-09-2018";

  $date2 = "31-09-2018";

  $dateDiff = dateDiffInDays($date1, $date2);

  printf("Difference between two dates: "

     . $dateDiff . " Days ");

?>

Output:

Difference between two dates: 14 Days

Method 2: Using date_diff() Function

The date_diff() function is an inbuilt function in PHP that is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure.

Example: This example describes calculating the number of days between the 2 dates in PHP.

PHP

  $datetime1 = date_create('17-09-2018');

  $datetime2 = date_create('25-09-2018');

  $interval = date_diff($datetime1, $datetime2);

  echo $interval->format('Difference between two dates: %R%a days');

?>

Output:

Difference between two dates: +8 days

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


How do you calculate days from today's date?

Calculate the number of days between today and another date.
So, the formula is: =TODAY() – B2..
To avoid having to do some mental math, you can take that formula and divide it by 30 to approximate months. ... .
And, of course, you can break it down even further by dividing it all by 365..

How can I get days between two dates in PHP?

The date_diff() function is an inbuilt function in PHP that is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure.

How can I get next 10 days in PHP?

echo date ( 'Y-m-d' , strtotime ( $Date . ' + 10 days' ));

How do you calculate days between two dates?

Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24) Print the final result using document.