Question

How to Get the Current Date and Time in PHP

phpdatetime

Question

In PHP, which function or built-in feature should I use to return the current date and time?

For example, I want to understand how to get:

  • the current date
  • the current time
  • or both date and time together

What is the standard way to do this in PHP?

Short Answer

By the end of this page, you will understand how PHP gets the current date and time, when to use date(), time(), and DateTime, and how to format the result correctly for real programs.

Concept

PHP can work with the current date and time in a few different ways, and the best choice depends on what you need.

The most common tools are:

  • date() for formatting a date/time as a readable string
  • time() for getting the current Unix timestamp
  • DateTime for more flexible and modern date/time handling

A Unix timestamp is the number of seconds since January 1, 1970 UTC. PHP often uses this internally when dealing with time.

date()

The date() function returns a formatted string representing a date and/or time.

echo date('Y-m-d H:i:s');

This might output:

2026-05-04 14:30:12

time()

The time() function returns the current Unix timestamp.

echo time();

Example output:

1777905012

You usually pass that timestamp into date() if you want a readable result.

DateTime

DateTime is an object-oriented way to work with dates and times.

$now = new DateTime();
echo $now->format('Y-m-d H:i:s');

This is often the best choice in real applications because it is easier to extend, compare, modify, and manage with timezones.

Why this matters

Dates and times appear everywhere in software:

  • showing when a post was published
  • saving timestamps in a database
  • logging errors
  • checking expiration times
  • scheduling tasks

If you choose the wrong format or ignore timezones, your app can show incorrect times or store inconsistent data.

Mental Model

Think of PHP date/time tools like different ways to read a clock:

  • time() gives you the raw machine number behind the clock
  • date() gives you a formatted label you can read easily
  • DateTime gives you the whole clock object that you can inspect, change, and format in different ways

So:

  • use time() when you want the raw current moment
  • use date() when you want to display it
  • use DateTime when you need more control
Take Quiz

Syntax and Examples

Basic syntax

Get the current date and time as a formatted string

echo date('Y-m-d H:i:s');

Get only the current date

echo date('Y-m-d');

Get only the current time

echo date('H:i:s');

Get the current Unix timestamp

echo time();

Use DateTime

$now = new DateTime();
echo $now->format('Y-m-d H:i:s');

Common format characters

FormatMeaningExample
Y4-digit year2026
m2-digit month05
d2-digit day04
Hhour in 24-hour format14
iminutes30
sseconds12

Example: display a readable timestamp

<?php

echo 'Current date: ' . date('Y-m-d') . PHP_EOL;
echo 'Current time: ' . date('H:i:s') . PHP_EOL;
echo 'Current date and time: ' . date('Y-m-d H:i:s') . PHP_EOL;

This uses date() with different format strings to show exactly the part you want.

Example: using DateTime with formatting

<?php

$now = new DateTime();

echo $now->format('l, F j, Y') . PHP_EOL;
echo $now->format('Y-m-d H:i:s') . PHP_EOL;

Possible output:

Monday, May 4, 2026
2026-05-04 14:30:12

Step by Step Execution

Consider this example:

<?php

$timestamp = time();
echo $timestamp . PHP_EOL;

echo date('Y-m-d H:i:s', $timestamp) . PHP_EOL;

Step by step

  1. time() gets the current Unix timestamp.
  2. That numeric value is stored in $timestamp.
  3. echo $timestamp prints the raw number.
  4. date('Y-m-d H:i:s', $timestamp) converts that timestamp into a readable date/time string.
  5. echo prints the formatted result.

Example flow

If time() returns:

1777905012

Then this line:

date('Y-m-d H:i:s', $timestamp)

might produce:

2026-05-04 14:30:12

So the raw timestamp and the formatted string represent the same moment in different forms.

Real World Use Cases

Logging events

$logTime = date('Y-m-d H:i:s');

Used for:

  • error logs
  • audit logs
  • request tracking

Saving created-at timestamps

When creating a new database record, you may store the current date/time.

$createdAt = date('Y-m-d H:i:s');

Showing publish times

Blogs, news sites, and admin panels display when content was created or updated.

echo 'Published on ' . date('Y-m-d');

Expiration checks

You may compare the current time with a saved timestamp.

if (time() > $expiresAt) {
    echo 'This link has expired.';
}

Scheduling and reminders

Apps often calculate future or past times using DateTime.

$now = new DateTime();
$now->modify('+1 day');
echo $now->format('Y-m-d H:i:s');
Take Quiz

Real Codebase Usage

In real PHP projects, developers often prefer DateTime or DateTimeImmutable for anything beyond simple display.

Common patterns

Simple output formatting

For small scripts or templates:

echo date('Y-m-d H:i:s');

Store timestamps in a consistent format

Many applications store values in formats like:

  • Unix timestamp via time()
  • SQL-style datetime via Y-m-d H:i:s
$createdAt = date('Y-m-d H:i:s');

Set timezone explicitly

A real codebase should avoid relying on unknown server defaults.

date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s');

Use DateTime for modification and comparison

$start = new DateTime();
$end = new DateTime('+2 hours');

if ($start < $end) {
    echo 'The end time is later.';
}

Use guard clauses for invalid dates

When user input is involved, developers validate before formatting or saving.

$date = DateTime::createFromFormat('Y-m-d', $input);
if (!$date) {
    die('Invalid date format');
}

Practical recommendation

  • Use date() for quick formatting.
  • Use time() for raw timestamps and comparisons.
  • Use DateTime for maintainable application code.

Common Mistakes

1. Forgetting about timezones

If you do not set the timezone, PHP may use the server default, which might not match your users.

Broken example:

echo date('Y-m-d H:i:s');

Better:

date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s');

2. Using the wrong format characters

A common mistake is mixing up month and minutes.

Broken example:

echo date('Y-m-d H:m:s');

Problem:

  • m means month, not minutes
  • minutes should use i

Correct version:

echo date('Y-m-d H:i:s');

3. Expecting time() to return a readable date

Broken expectation:

echo time();

This prints a Unix timestamp, not a formatted date.

Use:

echo date('Y-m-d H:i:s', time());

4. Hardcoding date strings manually

Broken example:

echo '2026-05-04';

This is not dynamic and does not show the current date.

Correct approach:

echo date('Y-m-d');

5. Using only strings when you need date logic

If you need to add days, compare dates, or handle timezones, DateTime is usually safer than string manipulation.

Better example:

$now = new DateTime();
$now->modify('+7 days');
echo $now->format('Y-m-d');

Comparisons

ToolReturnsBest forExample
date()Formatted stringDisplaying date/timedate('Y-m-d H:i:s')
time()Unix timestamp integerComparisons, storage, calculationstime()
DateTimeDateTime objectComplex date/time logicnew DateTime()

date() vs time()

  • date() is for readable output
  • time() is for the raw current timestamp

date() vs DateTime

  • date() is quick and simple
  • DateTime is better for larger applications and timezone-aware logic

DateTime vs DateTimeImmutable

  • DateTime can be changed after creation
  • DateTimeImmutable returns a new object when modified

For beginners, DateTime is easier to start with. In larger systems, many developers prefer DateTimeImmutable to avoid accidental changes.

Take Quiz

Cheat Sheet

// Current date and time
echo date('Y-m-d H:i:s');

// Current date only
echo date('Y-m-d');

// Current time only
echo date('H:i:s');

// Current Unix timestamp
echo time();

// DateTime object
$now = new DateTime();
echo $now->format('Y-m-d H:i:s');

// Set timezone
date_default_timezone_set('UTC');

Important format characters

  • Y = year
  • m = month
  • d = day
  • H = 24-hour hour
  • i = minutes
  • s = seconds

Remember

  • time() returns a number
  • date() returns a string
  • i means minutes, not m
  • timezone settings affect output

FAQ

What is the simplest way to get the current date and time in PHP?

Use:

echo date('Y-m-d H:i:s');

How do I get only the current date in PHP?

Use:

echo date('Y-m-d');

How do I get only the current time in PHP?

Use:

echo date('H:i:s');

What does time() return in PHP?

It returns the current Unix timestamp as an integer.

Should I use date() or DateTime in PHP?

Use date() for simple output. Use DateTime when you need comparisons, modification, or better structure.

Why is my PHP date showing the wrong time?

The timezone may be incorrect. Set it explicitly with date_default_timezone_set().

What is the standard database-friendly date format in PHP?

A common format is:

Y-m-d H:i:s

This works well for SQL datetime fields.

Take Quiz

Mini Project

Description

Build a small PHP script that prints the current date, current time, current timestamp, and a formatted DateTime value. This helps you practice the main ways PHP works with the current moment.

Goal

Create a PHP program that shows the current date and time using both procedural and object-oriented approaches.

Requirements

  1. Display the current date in Y-m-d format.
  2. Display the current time in H:i:s format.
  3. Display the current Unix timestamp.
  4. Create a DateTime object and print the current date and time from it.
  5. Set the timezone explicitly before displaying values.
Take Quiz

Keep learning

Related questions

View more