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.

Watch a course Learn object oriented PHP

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


$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 2678400 seconds (31 days).

Note: the code above assumes that the input date format is YYYY-MM-DD. If the input format is different, you can use the DateTime::createFromFormat('Y-m-d', $date) to format the input date accordingly.