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
| Format | Meaning | Example |
|---|---|---|
Y | 4-digit year | 2026 |
m | 2-digit month | 05 |
d | 2-digit day | 04 |
H | hour in 24-hour format | 14 |
i | minutes | 30 |
s | seconds | 12 |
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
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:
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');
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
DateTimefor 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:
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
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
| 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 outputtime()is for the raw current timestamp
date() vs DateTime
date()is quick and simpleDateTimeis better for larger applications and timezone-aware logic
DateTime vs DateTimeImmutable
DateTimecan be changed after creationDateTimeImmutablereturns a new object when modified
For beginners, DateTime is easier to start with. In larger systems, many developers prefer DateTimeImmutable to avoid accidental changes.
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
time()returns a numberdate()returns a stringimeans minutes, notm- 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.
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
Are PDO Prepared Statements Enough to Prevent SQL Injection in PHP?
Learn how PDO prepared statements prevent SQL injection in PHP, what they protect, and the mistakes that still leave MySQL apps vulnerable.
Can You Bind an Array to an IN Clause in PHP PDO?
Learn how PDO handles placeholders in IN() clauses, why arrays cannot be bound directly, and the safe PHP pattern to build dynamic queries.
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.