The 'date()' Function
Part of the Logic & Flow section of Coddy's PHP journey — lesson 46 of 68.
The date() function is PHP's primary tool for formatting dates and times into readable strings. It takes the current date and time and displays them according to format characters you specify, making it essential for showing dates to users in your applications.
The function uses single-letter format characters to represent different parts of a date or time. The most common ones include Y for a four-digit year, m for a two-digit month, d for a two-digit day, H for hours in 24-hour format, i for minutes, and s for seconds.
<?php
echo date("Y-m-d"); // Outputs: 2024-03-15
echo date("H:i:s"); // Outputs: 14:30:25
echo date("Y-m-d H:i:s"); // Outputs: 2024-03-15 14:30:25
?>You can combine these format characters with any separators or text you need. The date() function automatically uses the current date and time when called, making it perfect for timestamps, logs, or displaying "last updated" information on your web pages.
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.
Try it yourself
This lesson doesn't include a code challenge.
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