Menu
Coddy logo textTech

Unix Timestamps with 'time()'

Part of the Logic & Flow section of Coddy's PHP journey — lesson 47 of 68.

A Unix timestamp is a way of representing a specific moment in time as a single number. It counts the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC, which is known as the Unix Epoch. This standardized system makes it easy for computers to store, compare, and calculate with dates and times.

PHP provides the time() function to get the current Unix timestamp. When you call this function, it returns the number of seconds from the Unix Epoch to the current moment. This timestamp is particularly useful because it's independent of time zones and provides a consistent way to work with dates across different systems.

<?php
$currentTimestamp = time();
echo $currentTimestamp;  // Outputs something like: 1710515425
?>

The beauty of Unix timestamps becomes apparent when you combine them with the date() function you learned earlier. You can pass a timestamp as the second parameter to date() to format any specific moment in time, not just the current moment. This creates a powerful combination for working with dates in your applications.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

You will receive one input: a Unix timestamp as a number. The input will be a timestamp representing a specific moment in time (example: 1710515425).

Read the timestamp input, use the date() function to format this timestamp into a readable date and time, and print the result in the format Y-m-d H:i:s (year-month-day hour:minute:second).

Remember that date() accepts a timestamp as its second parameter to format any specific moment in time, not just the current moment.

Input format: One line containing a Unix timestamp as a number (example: 1710515425)

Expected output: The formatted date and time in the format Y-m-d H:i:s

Cheat sheet

A Unix timestamp represents a specific moment in time as the number of seconds since January 1, 1970, at 00:00:00 UTC (Unix Epoch).

Use time() to get the current Unix timestamp:

<?php
$currentTimestamp = time();
echo $currentTimestamp;  // Outputs something like: 1710515425
?>

You can pass a timestamp as the second parameter to date() to format any specific moment in time:

date("Y-m-d H:i:s", $timestamp);

Use mktime() to create specific timestamps by providing date and time components:

$timestamp = mktime(hour, minute, second, month, day, year);

Try it yourself

<?php
// Read the Unix timestamp
$timestamp = intval(fgets(STDIN));

// TODO: Write your code below to format the timestamp using date()

// Output the formatted date and time
echo $formattedDate;
?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow