In PHP, getting the current timestamp is a common task, especially when dealing with time-related operations. A timestamp usually refers to the number of seconds that have passed since the Unix epoch (January 1, 1970, 00:00:00 UTC). PHP provides two commonly used functions to retrieve the current time: date() and time(). These two functions can be used together to handle and format time information more flexibly.
The time() function returns the current Unix timestamp, which is the number of seconds since January 1, 1970, up to the present moment. This function does not require any parameters and returns an integer timestamp.
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">time</span></span><span>(); </span><span><span class="hljs-comment">// Outputs the current timestamp</span></span><span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
The code above will output a number like 1633041234, which represents the current timestamp.
The date() function is used to format a timestamp and return the current time in a specified format. The first parameter is a format string, and the second parameter is an optional timestamp. If no timestamp is provided, date() uses the current time by default.
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">date</span></span><span>(</span><span><span class="hljs-string">'Y-m-d H:i:s'</span></span><span>); </span><span><span class="hljs-comment">// Outputs the current date and time in "YYYY-MM-DD HH:MM:SS" format</span></span><span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
In this example, date() will output something like 2025-08-30 14:30:00.
By combining time() with date(), you can format the current Unix timestamp into a more human-readable form. A common use case is to display a formatted date and time along with the timestamp. Here’s an example:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$timestamp</span></span><span> = </span><span><span class="hljs-title function_ invoke__">time</span></span><span>(); </span><span><span class="hljs-comment">// Get the current timestamp</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">date</span></span><span>(</span><span><span class="hljs-string">'Y-m-d H:i:s'</span></span>, </span><span><span class="hljs-variable">$timestamp</span></span><span>); </span><span><span class="hljs-comment">// Output the formatted date and time</span></span><span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
This snippet will return a formatted string of the current date and time, such as 2025-08-30 14:30:00. First, we use time() to get the timestamp, then date() to convert it into the desired format.
You can customize the format string of date() as needed. For example, to output the current weekday and time:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">date</span></span><span>(</span><span><span class="hljs-string">'l, H:i:s'</span></span><span>); </span><span><span class="hljs-comment">// Outputs in the format "Day of the week, HH:MM:SS"</span></span><span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
This code might output something like Saturday, 14:30:00, showing the current weekday and time.
The time() function is not only for retrieving the current timestamp—it can also be used in calculations with other timestamps. For example, calculating the timestamp for one hour later:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$future_timestamp</span></span><span> = </span><span><span class="hljs-title function_ invoke__">time</span></span><span>() + </span><span><span class="hljs-number">3600</span></span><span>; </span><span><span class="hljs-comment">// Current timestamp plus one hour (3600 seconds)</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">date</span></span><span>(</span><span><span class="hljs-string">'Y-m-d H:i:s'</span></span>, </span><span><span class="hljs-variable">$future_timestamp</span></span><span>); </span><span><span class="hljs-comment">// Outputs the time one hour later</span></span><span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
This code will output the date and time one hour ahead of the current time.
By combining the time() and date() functions, PHP provides a flexible and efficient way to handle and format time information. time() retrieves the current timestamp, while date() converts it into a human-readable format. Whether you need to get the current time or perform time calculations, PHP makes it straightforward. Mastering these basic functions can provide strong support for time management in your projects.