What is time() in PHP?
time()returns the current Unix timestamp.- A Unix timestamp is the number of seconds that have passed since January 1, 1970 00:00:00 GMT (also called the Epoch time).
Syntax
time(): int
- It takes no parameters.
- It returns an integer value (the number of seconds).
Simple Example
<?php
echo time();
?>
Output:
1714203456
(Output will be a big number — it’s the seconds since 1970.)
Practical Usage Examples
1. Get the current date and time
<?php
echo date("Y-m-d H:i:s", time());
?>
Output:
2025-04-27 15:25:56
date()formats the timestamp into a readable date and time.
2. Add 7 days to current time
<?php
$future = time() + (7 * 24 * 60 * 60); // 7 days * 24 hours * 60 minutes * 60 seconds
echo date("Y-m-d", $future);
?>
Output:
2025-05-04
time()+ seconds of 7 days = date 7 days later.
3. Check how many seconds have passed
Suppose you want to measure how long a script runs:
<?php
$start = time();
// some code that takes time
sleep(5); // sleep for 5 seconds
$end = time();
echo "Execution time: " . ($end - $start) . " seconds.";
?>
Output:
Execution time: 5 seconds.
Important Points
time()always gives the server’s current time.- If you need timezone adjustment, you should set it:
date_default_timezone_set('Asia/Kolkata'); echo date('Y-m-d H:i:s', time()); - For more accurate time (like microseconds), PHP has
microtime().
0
likes
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
