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 stringtime()for getting the current Unix timestampDateTimefor 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 clockdate()gives you a formatted label you can read easilyDateTimegives 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
DateTimewhen you need more control
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
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
time()gets the current Unix timestamp.- That numeric value is stored in
$timestamp. echo $timestampprints the raw number.date('Y-m-d H:i:s', $timestamp)converts that timestamp into a readable date/time string.echoprints the formatted result.
Example flow
If time() returns:
1777905012
Then this line:
date('Y-m-d H:i:s', $timestamp)
might produce:
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.
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
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:
mmeans 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
Comparisons
| Tool | Returns | Best for | Example |
|---|---|---|---|
date() | Formatted string | Displaying date/time | date('Y-m-d H:i:s') |
time() | Unix timestamp integer | Comparisons, storage, calculations | time() |
DateTime | DateTime object | Complex date/time logic | new DateTime() |
date() vs time()
date()is for readable output
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= yearm= monthd= dayH= 24-hour houri= minutess= seconds
Remember
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().
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
- Display the current date in
Y-m-dformat. - Display the current time in
H:i:sformat. - Display the current Unix timestamp.
- Create a
DateTimeobject and print the current date and time from it. - Set the timezone explicitly before displaying values.
Keep learning
Related questions
Choosing the Right MySQL Collation for PHP and UTF-8
Learn how MySQL character sets and collations work with PHP, and how to choose a practical UTF-8 setup for web applications.
Convert a PHP Object to an Associative Array
Learn how to convert a PHP object to an associative array, including quick methods, recursion, pitfalls, and practical examples.
Convert a Postman Request to cURL and PHP cURL
Learn how to convert a Postman POST request into a cURL command and use the same request in PHP cURL with headers and body.