Skip to content

Difference between 2 dates in seconds

In PHP, you can use the built-in function strtotime() to convert a date string to a Unix timestamp, which is the number of seconds since the Unix epoch (January 1, 1970). Once you have the timestamps for both dates, you can subtract one from the other to get the number of seconds between the two dates.

Here is an example of how you can use strtotime() to find the number of seconds between two dates:

Example of using strtotime() to find the number of seconds between two dates in PHP

php
<?php

$date1 = "2022-01-01";
$date2 = "2022-01-31";

$timestamp1 = strtotime($date1);
$timestamp2 = strtotime($date2);

$diff = $timestamp2 - $timestamp1;

echo $diff;

This will output the number of seconds between January 1st, 2022 and January 31st, 2022, which is 2592000 seconds (30 days).

Note: The code above assumes the input date format is YYYY-MM-DD. If your dates use a different format, strtotime() may not parse them correctly. For strict parsing, use DateTime::createFromFormat('d/m/Y', $date) (adjusting the format string to match your actual input) and then call getTimestamp() on the returned object.

Dual-run preview — compare with live Symfony routes.