How to use the Date and Time in PHP ?
Last Updated :
13 Feb, 2024
Date and time handling in PHP is crucial for various web applications, ranging from simple date displays to complex calculations involving time zones and intervals.
PHP provides a rich set of functions and classes to manage dates and times efficiently. Understanding these functionalities enables developers to perform tasks like date formatting, parsing, arithmetic, and time zone conversions effectively.
Approach
- Getting Current Date and Time: Retrieve the current date and time using functions like
date()
or DateTime
class. - Formatting Dates: Customize the display format of dates using format codes with functions like
date()
or DateTime::format()
. - Manipulating Dates: Perform operations like adding or subtracting days, months, or years using functions like
strtotime()
or methods like DateTime::modify()
. - Parsing Dates: Convert string representations of dates into timestamps using functions like
strtotime( )
or DateTime::createFromFormat()
. - Time Zone Handling: Convert between different time zones using
DateTimeZone
class and DateTime::setTimezone()
method.
Getting the current date and time
It retrieves the current date and time in the specified formats using PHP's date()
function and DateTime
object.
$currentDate = date('Y-m-d'); // [date] => 2024-02-12 11:39:49.272639
$currentTime = date('H:i:s'); // [timezone_type] => 3
$dateTime = new DateTime( ); // [timezone] => UTC
Formatting dates
It formats a given date ('2024-02-11') into a human-readable format ('February 11, 2024') using strtotime()
and DateTime::format()
.
$formattedDate = date('F j, Y', strtotime('2024-02-11')); // February 11, 2024
$formattedDateTime = $dateTime->format('Y-m-d H:i:s'); // 2024-02-12 11:43:38
Manipulating dates
It calculates the date for the next week, modifies the current date to the next day, and formats them accordingly using strtotime()
, date()
, and DateTime::modify()
.
$nextWeek = strtotime('+1 week');
$newDate = date('Y-m-d', $nextWeek);
$dateTime->modify('+1 day');
$modifiedDate = $dateTime->format('Y-m-d');
Parsing dates
It converts a string date ('2024-02-11') into a human-readable format ('February 11, 2024') using strtotime()
and date()
. Additionally, creates a DateTime
object from a string date and time ('2024-02-11 15:30:00') using DateTime::createFromFormat()
.
$timestamp = strtotime('2024-02-11');
$parsedDate = date('F j, Y', $timestamp);
$parsedDateTime = DateTime::createFromFormat('Y-m-d H:i:s', '2024-02-11 15:30:00');
//output
DateTime Object
(
[date] => 2024-02-11 15:30:00.000000
[timezone_type] => 3
[timezone] => UTC
)
Each letter in the format string represents a part of the date and time:
Y
: Four-digit year (e.g., 2024)m
: Two-digit month (01 to 12)d
: Two-digit day (01 to 31)H
: Two-digit hour in 24-hour format (00 to 23)i
: Two-digit minute (00 to 59)s
: Two-digit second (00 to 59)
Similar Reads
How to Start and Stop a Timer in PHP ? You can start and stop a timer in PHP using the microtime() function in PHP. The microtime() function is an inbuilt function in PHP which is used to return the current Unix timestamp with microseconds. In this article, you will learn the uses of the microtime() function. Syntax: microtime( $get_as_f
2 min read
How to convert timestamp to time ago in PHP ? Given a time and the task is to convert timestamp to time ago. The time ago format removes the problem of different time zones conversions. Given below is a function to do the time conversions. In this function, taking the timestamp as an input and then subtract it from the current timestamp to conv
3 min read
PHP Date and Time PHP provides functions to work with dates and times, allowing developers to display the current date/time, manipulate and format dates, and perform operations like date comparisons, time zone adjustments, and more.In this article, we'll discuss PHP date and time.Why are Date and Time Important in PH
5 min read
How to convert DateTime to String using PHP ? Converting a `DateTime` to a string in PHP involves formatting the `DateTime` object into a human-readable format using the `format()` method. This process allows you to represent dates and times as strings in various formats, such as Y-m-d H:i:s. There are some following approachesTable of ContentB
4 min read
How to Build a Calendar Table in PHP? In PHP, by using the Date and DateTime Functions, we can build a dynamic Calendar Table based on user input. Below are the approaches to Build a Calendar Table in PHP: Table of Content Using PHP's Date FunctionsUsing PHP's DateTime ObjectUsing PHP's Date FunctionsIn this approach, we are using PHP's
3 min read