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.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyYou 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;
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise7Working with Dates and Times
The 'date()' FunctionUnix Timestamps with 'time()'Intro to the DateTime ObjectFormatting DateTime ObjectsModifying DateTime ObjectsRecap: Date Calculations2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting