How to Check How Many Days Are Between Two Dates in PHP?

3 minutes read

Working with dates in PHP involves various operations such as formatting dates, comparing dates, calculating date differences, and working with time zones. Here are some commonly used functions and techniques for performing date-related tasks in PHP:

  1. Current Date and Time: date(format, timestamp) function returns the current date and time formatted according to the specified format. time() function returns the current Unix timestamp (number of seconds since January 1, 1970).
  2. Formatting Dates: date(format, timestamp) can be used to format a specific date or timestamp. Common format characters include d (day), m (month), Y (4-digit year), H (hour), i (minutes), s (seconds), etc.
  3. Converting Strings to Dates: strtotime(time_string) function converts a string representation of a date and time into a Unix timestamp. Example: strtotime("2022-01-20") will return the timestamp for January 20, 2022.
  4. Comparing Dates: Dates can be compared using comparison operators (<, <=, >, >=) or numeric comparison of timestamps.
  5. Date Formatting Examples: date("Y-m-d") returns the current date in the "YYYY-MM-DD" format. date("F j, Y") returns the current date in the "Month day, Year" format (e.g., "January 1, 2022").
  6. Date Arithmetic: strtotime("+1 day", timestamp) adds a specified time period to a given timestamp. Example: strtotime("+1 day", time()) will return the timestamp for tomorrow.
  7. Time Zones: date_default_timezone_set("timezone") sets the default time zone used by date and time functions. DateTime class provides more flexibility in working with time zones, including conversions, date arithmetic, etc.
  8. Date Differences: date_diff(date1, date2) function calculates the difference between two dates and returns a DateInterval object.

These are just some of the most commonly used functions and techniques. PHP offers many more date and time functions to handle various scenarios. It is recommended to refer to the official PHP documentation ( for detailed information and examples.

How to check how many days are between two dates in PHP?

You can calculate the number of days between two dates in PHP using the DateTime class. Here's an example:


$date1 = new DateTime('2021-01-01');
$date2 = new DateTime('2021-02-10');

$interval = $date1->diff($date2);
$days = $interval->days;

echo "Number of days between the two dates: " . $days;

This code creates two DateTime objects for the given dates. Then, the diff() method is used on the first date object to calculate the difference between the two dates. The days property of the resulting DateInterval object gives you the number of days between the dates.

Note: Make sure to specify the dates in the format 'YYYY-MM-DD' in the DateTime constructor.

How to format date in PHP?

In PHP, you can format a date using the date() function. The date() function converts a timestamp or current date/time to a specified format.

The syntax of the date() function is as follows:


date(format, timestamp)

The format parameter specifies the desired format in which the date should be formatted. The timestamp parameter (optional) is a Unix timestamp representing the date and time.

Here are some commonly used format characters for formatting dates:

  • d - Represents the day of the month (01 to 31)
  • m - Represents the month (01 to 12)
  • Y - Represents the year (in four digits, e.g. 2022)
  • y - Represents the year (in two digits, e.g. 22)
  • H - Represents the hour in 24-hour format (00 to 23)
  • h - Represents the hour in 12-hour format (01 to 12)
  • i - Represents the minutes (00 to 59)
  • s - Represents the seconds (00 to 59)
  • a - Represents "am" or "pm"

Here's an example to format the current date in the format "d/m/Y":

$date = date('d/m/Y');
echo $date;
// Output: 06/10/2022

You can also use the date() function with a specified timestamp instead of the current date:

$timestamp = strtotime('2022-10-06');
$date = date('d/m/Y', $timestamp);
echo $date;
// Output: 06/10/2022

By using different format characters, you can format the date in various ways according to your requirements.

Facebook Twitter LinkedIn Telegram


No comments

Related Posts:

Learning PHP will depend on the focus and the time that you put into it. Different people learn it for different periods depending on how fast you can grasp the content and apply it. For fast learners, you can learn PHP in two weeks if you are coding every day...
PHP (Hypertext Preprocessor) is a popular server-side scripting language used for web development. PHP 5 and PHP 7 are two major versions of PHP, with PHP 7 being the newer and more advanced version.
The php.ini file is a configuration file used by PHP to control various settings. It is typically located in the PHP installation directory or in the root folder of the web server. The purpose of the php.ini file is to customize the behavior of PHP to suit you...