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

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

Date and Time in PHP

Updated 2026-05-15
10 min read

Date and Time in PHP

Introduction

In web development, handling date and time is a common requirement. Whether you're logging user activities, scheduling events, or calculating the duration between two dates, PHP provides robust tools to manage these tasks efficiently. This tutorial will guide you through the basics of working with dates and times in PHP, from setting the default timezone to formatting dates.

Concept

PHP offers several functions and classes to manipulate date and time. The most commonly used class is DateTime, which provides a flexible way to handle date and time operations. Additionally, PHP includes various functions like date(), strtotime(), and mktime() that can be useful for simpler tasks.

Setting the Default Timezone

Before performing any date or time operations, it's crucial to set the default timezone. This ensures that all date-related functions use the correct time zone. You can set the timezone in your PHP script using the date_default_timezone_set() function.

php
1<?php
2// Set the default timezone to Eastern Standard Time
3date_default_timezone_set('America/New_York');
4?>

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 it accepts two parameters: a format string and an optional timestamp.

php
1<?php
2// Get the current date and time in 'Y-m-d H:i:s' format
3echo date('Y-m-d H:i:s');
4?>

Creating DateTime Objects

The DateTime class provides a more object-oriented approach to handling dates. You can create a new DateTime object with or without specifying a date string.

php
1<?php
2// Create a DateTime object for the current date and time
3$currentDate = new DateTime();
4echo $currentDate->format('Y-m-d H:i:s');
5
6// Create a DateTime object for a specific date
7$specificDate = new DateTime('2023-10-05 14:30:00');
8echo $specificDate->format('Y-m-d H:i:s');
9?>

Modifying Dates

You can modify dates using the DateTime class methods. For example, you can add or subtract days, months, or years.

php
1<?php
2$date = new DateTime('2023-10-05');
3$date->modify('+1 day'); // Add one day
4echo $date->format('Y-m-d');
5
6$date->modify('-1 month'); // Subtract one month
7echo $date->format('Y-m-d');
8?>

Comparing Dates

You can compare two DateTime objects to determine which one is earlier or later.

php
1<?php
2$date1 = new DateTime('2023-10-05');
3$date2 = new DateTime('2023-10-10');
4
5if ($date1 < $date2) {
6 echo "Date 1 is earlier than Date 2";
7} else {
8 echo "Date 1 is later than or equal to Date 2";
9}
10?>

Examples

Example 1: Logging User Activities

<?php
// Set the default timezone
date_default_timezone_set('America/New_York');

// Get the current date and time for logging
$logEntry = "User logged in at " . date('Y-m-d H:i:s');
echo $logEntry;
?>

Example 2: Calculating Event Duration

<?php
// Set the default timezone
date_default_timezone_set('America/New_York');

// Create DateTime objects for event start and end times
$startTime = new DateTime('2023-10-05 14:00:00');
$endTime = new DateTime('2023-10-05 16:30:00');

// Calculate the duration in hours
$duration = $endTime->diff($startTime);
echo "Event duration: " . $duration->h . " hours and " . $duration->i . " minutes";
?>

What's Next?

In this tutorial, you learned how to handle dates and times in PHP using both procedural functions and object-oriented classes. In the next section, we will explore more advanced date functions and techniques.

Stay tuned for more tutorials on PHP and other programming topics!


PreviousRegular Expressions in PHPNext Date Functions

Recommended Gear

Regular Expressions in PHPDate Functions