codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

28 / 56 topics
27Date and Time in PHP28Date Functions29Timezones in PHP
Tutorials/PHP/Date Functions
🐘PHP

Date Functions

Updated 2026-05-15
10 min read

Date Functions

Introduction

In PHP, handling dates and times is a common task that can be efficiently managed using built-in functions. These functions allow developers to perform various operations such as getting the current date and time, formatting dates, manipulating dates, and more. In this tutorial, we will explore some of the most commonly used date functions in PHP.

Concept

PHP provides a rich set of functions for working with dates and times. The core functions are part of the DateTime class and its related classes like DateInterval, DatePeriod, and DateTimeImmutable. These functions can be categorized into several groups:

  1. Getting Current Date and Time: Functions like date(), time(), and microtime() help in retrieving the current date and time.
  2. Formatting Dates: Functions such as date_format() and strftime() allow you to format dates according to your needs.
  3. Manipulating Dates: Functions like strtotime(), date_modify(), and DateTime::modify() enable you to add or subtract intervals from a date.
  4. Comparing Dates: Functions such as date_diff() and DateTime::diff() help in comparing two dates.

Examples

1. Getting Current Date and Time

To get the current date and time, you can use the date() function. This function formats a local date and time, and returns the formatted date string on success, or false on failure.

php
1echo date('Y-m-d H:i:s'); // Outputs: 2023-10-05 14:30:00

You can also use the time() function to get the current Unix timestamp, which is the number of seconds since January 1, 1970.

php
1echo time(); // Outputs: 1696524600

2. Formatting Dates

The date() function can also be used to format dates in various ways. The format string is a combination of letters that represent different parts of the date and time.

php
1echo date('F j, Y, g:i a'); // Outputs: October 5, 2023, 2:30 pm

For more complex formatting, you can use strftime(), which formats a local date and time according to locale settings.

php
1echo strftime('%A %d %B %Y'); // Outputs: Thursday 05 October 2023

3. Manipulating Dates

To manipulate dates, you can use functions like strtotime() and date_modify(). The strtotime() function parses a string representing a date and time into a Unix timestamp.

php
1$timestamp = strtotime('+1 day');
2echo date('Y-m-d', $timestamp); // Outputs: 2023-10-06

The date_modify() function modifies the given DateTime object by adding or subtracting a specified interval.

php
1$date = new DateTime();
2$date->modify('+7 days');
3echo $date->format('Y-m-d'); // Outputs: 2023-10-12

4. Comparing Dates

To compare two dates, you can use the DateTime::diff() method, which returns a DateInterval object representing the difference between two dates.

php
1$date1 = new DateTime('2023-10-05');
2$date2 = new DateTime('2023-10-12');
3$interval = $date1->diff($date2);
4echo $interval->days; // Outputs: 7

What's Next?

In the next section, we will explore how to handle timezones in PHP. Understanding and managing timezones is crucial for applications that operate across different regions.

Stay tuned!


PreviousDate and Time in PHPNext Timezones in PHP

Recommended Gear

Date and Time in PHPTimezones in PHP