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()
.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.