A timestamp is a numeric representation of a specific point in time. In PHP, this is usually a Unix Timestamp — the number of seconds that have passed since January 1, 1970 (the Unix Epoch).
mktime()The mktime() function returns the Unix timestamp for a date. This is useful
for creating a timestamp for a specific past or future date.
Syntax: mktime(hour, minute, second, month, day, year)
<?php
$d = mktime(11, 14, 54, 8, 12, 2014);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
strtotime()The strtotime() function is incredibly powerful. it converts a
human-readable date string into a Unix timestamp.
<?php
$d = strtotime("10:30pm April 15 2024");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
$next_sat = strtotime("next Saturday");
echo "Next Saturday is " . date("Y-m-d", $next_sat);
?>
Because timestamps are just integers (seconds), you can do simple math to calculate future or past dates.
<?php
$startdate = strtotime("Saturday");
$enddate = strtotime("+6 weeks", $startdate);
while ($startdate < $enddate) {
echo date("M d", $startdate) . "<br>";
$startdate = strtotime("+1 week", $startdate);
}
?>
strtotime() is very flexible. You can use phrases
like "tomorrow", "next Monday", or even "+3 months" to handle complex scheduling logic
with ease.
| Keyword | Description |
|---|---|
now |
Current timestamp (Same as time()). |
tomorrow |
Timestamp for midnight tomorrow. |
yesterday |
Timestamp for midnight yesterday. |
+1 day |
Adds 24 hours to the current time. |
next month |
Calculates the same day next month. |