HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

PHP Timestamps

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).


1. Creating Timestamps - 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);
?>

2. String to Time - 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);
?>

3. Time Calculations

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);
    }
?>
What is 1970? The Unix Epoch (Jan 1, 1970) was chosen as a standard point of origin for computer systems. Every timestamp you see is simply a count of seconds from that moment.
Pro Tip: strtotime() is very flexible. You can use phrases like "tomorrow", "next Monday", or even "+3 months" to handle complex scheduling logic with ease.

Commonly Used Keywords

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.