How can time() and date() functions be utilized for date calculations in PHP?

The time() function in PHP returns the current Unix timestamp, which represents the current date and time in seconds since the Unix Epoch (January 1, 1970). The date() function can be used to format this timestamp into a human-readable date format. By performing arithmetic operations on timestamps obtained using time(), you can easily calculate date differences or perform date calculations in PHP.

// Calculate the difference between two dates in days
$date1 = strtotime('2022-01-01');
$date2 = time();
$diff = ($date2 - $date1) / (60 * 60 * 24);
echo "The difference between the two dates is: " . $diff . " days.";

// Add 1 week to the current date
$nextWeek = time() + (7 * 24 * 60 * 60);
echo "One week from now will be: " . date('Y-m-d', $nextWeek);