Please wait

Unix Timestamps

Unix timestamps in PHP are integers that represent the number of seconds that have elapsed since the Unix Epoch, which is set at 00:00:00 UTC on January 1, 1970. This is a standardized point in time used across many operating systems and programming languages.

Unix timestamps provide a consistent way to represent time across different systems, time zones, and locales. Working with Unix timestamps makes it easy to perform arithmetic operations on dates and times. You can add or subtract seconds to calculate new dates easily. If you need to compare two dates, it's often easier to work with them as Unix timestamps.

Retrieving a Unix Timestamp

We already learned how to grab a Unix timestamp when working with files. We can use the time() function. Here's a simple example:

$timestamp = time();
echo $timestamp; // Outputs the current Unix timestamp

This function always returns the current timestamp.

Specific Timestamps

Sometimes, we may want to grab a timestamp with a specific date or time. The strtotime() function in PHP is a powerful tool used to parse a human-readable date and time string into a Unix timestamp. It takes a string representation of a date and time, along with an optional base timestamp, and returns the corresponding Unix timestamp.

The basic syntax of strtotime() is as follows:

strtotime(string $datetime, ?int $baseTimestamp = null): int|false
  • $datetime: The input string representing the date and time. This can be in many different formats, including relative formats like "next Monday" or "3 days ago."
  • $baseTimestamp: An optional Unix timestamp that serves as the base time for relative calculations. If not provided, the current timestamp is used.

If the function is unable to parse the provided string, it will return false. It is timezone-aware, meaning that it will consider the current timezone setting in PHP.

Here's a basic example:

$timestamp = strtotime('2023-08-04 12:00:00');

In this example, we're passing along the date in the format specified here: https://www.php.net/manual/en/datetime.formats.php

Relative Times

When using the strtotime() function, you can pass in a relative time format when you don't have an exact date in mind. Relative time formats are textual representations of time expressions that describe a specific time relative to another time (usually the current time).

Relative time formats can describe things like next Monday, 3 days ago, first day of next month, etc. They allow you to write time expressions in a human-readable form, which PHP can then convert into a Unix timestamp for further manipulation.

Using relative time expressions makes the code more intuitive and closer to natural language, making it easier to read and maintain. They provide a way to calculate dates without having to manually add or subtract days, weeks, or other units of time.

Symbols and Expressions Used with Relative Time Formats

  • + and -: Used to add or subtract time, e.g., +1 day, -3 weeks.
  • next, previous, this, and last: Used to refer to the next or last occurrence of a specific day, e.g., next Monday, last day of next month.
  • first and last: Used to refer to the first or last occurrence within a specific context, e.g., first day of January, last day of -1 month.
  • yesterday, today, tomorrow: Special keywords to refer to specific relative days.
  • Units of Time: Words like second, minute, hour, day, week, month, year to define the period of time to add or subtract.

Examples

  • strtotime('now + 1 week'): Timestamp for one week from now.
  • strtotime('last day of next month'): Timestamp for the last day of the next month.
  • strtotime('first day of +2 months'): Timestamp for the first day of two months from now.

Relative time formats offer a powerful and intuitive way to handle date and time calculations in PHP. By using these expressions, you can easily perform complex date manipulations without having to deal with individual components like days, months, or years manually.

mktime() Function

The mktime() function in PHP is used to get the Unix timestamp for a specific date and time. Essentially, it takes individual components like hours, minutes, seconds, months, days, and years and converts them into the corresponding Unix timestamp.

Here's the basic syntax of the mktime() function:

 mktime(
  int $hour,
  ?int $minute = null,
  ?int $second = null,
  ?int $month = null,
  ?int $day = null,
  ?int $year = null
): int|false

Parameters

  • $hour: The number of hours.
  • $minute: The number of minutes.
  • $second: The number of seconds.
  • $month: The number of the month.
  • $day: The number of the day.
  • $year: The year.

All these parameters are optional, and if omitted, the current date's values are used.

Let's say you want to find the Unix timestamp for 3:45:00 on August 4, 2023. You would use the mktime() function like this:

$timestamp = mktime(3, 45, 0, 8, 4, 2023);
echo $timestamp; // Outputs the Unix timestamp for the specified date and time

The mktime() is a handy function in PHP that helps you create a Unix timestamp from individual components of a date and time. It's especially useful when you have these components separately and want to convert them into a standardized timestamp form.

Timestamps with date() Function

You can use a custom timestamp with the date() function in PHP by passing the timestamp as the second argument. If you want to work with a date and time other than "now," a custom timestamp lets you specify exactly what that date and time is.

Let's say you have a Unix timestamp representing a specific date and time, and you want to format it in a human-readable way:

$timestamp = mktime(3, 45, 0, 8, 4, 2023);
$formattedDate = date('Y-m-d H:i:s', $timestamp);
echo $formattedDate; // Outputs "2023-08-04 03:45:00"

Key Takeaways

  • Unix timestamps represent the number of seconds since January 1, 1970, at 00:00:00 UTC.
  • You can obtain the current Unix timestamp using the time() function.
  • Use the mktime() function to create a Unix timestamp from specific date and time components.
  • The strtotime() function allows converting human-readable date and time strings into Unix timestamps.
  • The date() function enables formatting Unix timestamps into human-readable dates and times by using various format characters.
  • Unix timestamps facilitate easy arithmetic operations, such as adding or subtracting seconds, to manipulate dates and times.
  • Comparing Unix timestamps is straightforward, as they are simple integers, enabling easy sorting and comparison of dates.
  • Unix timestamps are efficient for storing in databases, allowing consistent date and time storage across different systems.

Comments

Please read this before commenting