Working with Dates and Time in PHP: Functions and Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why You Need to Work with Dates and Time in PHP



Working with dates and time is one of the key tasks in web development. Whether it's logging user actions, calculating age, creating calendars, processing timestamps from a database, or managing subscription periods — you can't do without reliable tools for working with dates. PHP provides developers with a powerful and flexible set of functions and classes for these purposes.



In this article, we will thoroughly examine the main ways to work with dates and time in PHP: from the simple date() and time() functions to the object-oriented approach using the DateTime class. You will learn how to format dates, calculate the difference between them, work with time zones, and avoid common mistakes.



Basic Functions: date(), time() and mktime()



The simplest way to get the current date or time in PHP is to use the date() function. It takes a format string and optionally a timestamp. If no timestamp is passed, the current time is used.



<?php// Current date in "Year-Month-Day" formatecho date('Y-m-d'); // Output: 2025-04-10

// Current time in "Hours:Minutes:Seconds" formatecho date('H:i:s'); // Output: 14:35:20

// Full date and timeecho date('Y-m-d H:i:s'); // Output: 2025-04-10 14:35:20?>


The time() Function

time() returns the current Unix timestamp (the number of seconds since January 1, 1970). This is useful for calculations and comparisons.



<?php$now = time();echo $now; // Output: 1744277720 (number changes every second)

// Date in 7 days$nextWeek = time() + (7 * 24 * 60 * 60);echo date('Y-m-d', $nextWeek); // Output: 2025-04-17?>


The mktime() Function

mktime() allows you to create a timestamp for a specific date and time. This is convenient when you need to get a timestamp for given parameters.



<?php// Create a timestamp for December 25, 2025, 10:00:00$christmas = mktime(10, 0, 0, 12, 25, 2025);echo date('Y-m-d H:i:s', $christmas); // Output: 2025-12-25 10:00:00?>


Date Formatting: Basic Characters

The table below shows the most commonly used formatting characters for the date() function:

  • d — day of the month (01–31)
  • m — month number (01–12)
  • Y — year in 4-digit format (2025)
  • H — hours in 24-hour format (00–23)
  • i — minutes (00–59)
  • s — seconds (00–59)
  • l — full name of the day of the week (Monday)
  • F — full name of the month (April)


Object-Oriented Approach: The DateTime Class

Starting from PHP 5.2, it is recommended to use the DateTime class for working with dates. It provides much more functionality and an intuitive interface.



Creating a DateTime Object

<?php// Current date and time$date = new DateTime();echo $date->format('Y-m-d H:i:s'); // Output: 2025-04-10 14:35:20

// Specific date$birthday = new DateTime('1990-07-15');echo $birthday->format('d.m.Y'); // Output: 15.07.1990

// With a relative format$nextMonth = new DateTime('+1 month');echo $nextMonth->format('Y-m-d'); // Output: 2025-05-10?>


Modifying Dates

The modify() method allows you to easily change the date using relative strings.



<?php$date = new DateTime('2025-04-10');$date->modify('+10 days');echo $date->format('Y-m-d'); // Output: 2025-04-20

$date->modify('first day of next month');echo $date->format('Y-m-d'); // Output: 2025-05-01?>


Dif

Blogs

Book Recommendations