The time() function in PHP returns the current Unix timestamp, which is the number of seconds since January 1, 1970, 00:00:00 GMT. It is widely used for time calculations, comparisons, and date handling.
date()
// Returns the current Unix timestamp
time();
This example shows how to retrieve the current Unix timestamp.
// Display the current Unix timestamp
<?php
echo time();
?>
The time() function outputs the total number of seconds elapsed since the Unix epoch.
Using timestamps for future or past date calculations.
// Calculate time for one day later
<?php
$time_now = time();
$one_day_later = $time_now + 86400;
echo "Current time: " . date("Y-m-d H:i:s", $time_now) . "<br>";
echo "One day later: " . date("Y-m-d H:i:s", $one_day_later);
?>
By adding seconds to the timestamp, we can easily calculate future or past dates and format them using date().