Current Location: Home> Latest Articles> Effortlessly Format Time with PHP Using localtime() and strftime() Functions

Effortlessly Format Time with PHP Using localtime() and strftime() Functions

gitbox 2025-09-04

In PHP, handling time and date is a common development requirement, especially in scenarios such as logging, displaying data, and file naming. Time formatting often plays an important role in these cases. PHP offers many functions to help developers format time, and the combination of localtime() and strftime() allows you to easily implement versatile time formatting.

1. Overview of localtime() Function

localtime() is a function used to retrieve local time. It returns an array containing different components of the local time, such as year, month, day, hour, minute, and second. The returned information is based on the current timestamp, and you can specify a parameter to get the local time of a given timestamp.

<span><span><span class="hljs-variable">$time</span></span><span> = </span><span><span class="hljs-title function_ invoke__">localtime</span></span><span>();
</span><span><span class="hljs-title function_ invoke__">print_r</span></span><span>(</span><span><span class="hljs-variable">$time</span></span><span>);
</span></span>

The returned array looks like this:

<span><span>Array
(
    [</span><span><span class="hljs-meta">0</span></span><span>] =&gt; Seconds
    [</span><span><span class="hljs-meta">1</span></span><span>] =&gt; Minutes
    [</span><span><span class="hljs-meta">2</span></span><span>] =&gt; Hours
    [</span><span><span class="hljs-meta">3</span></span><span>] =&gt; Day of the week (</span><span><span class="hljs-number">0</span></span><span>=Sunday, </span><span><span class="hljs-number">1</span></span><span>=Monday...)
    [</span><span><span class="hljs-meta">4</span></span><span>] =&gt; Month (</span><span><span class="hljs-number">0</span></span><span>=</span><span><span class="hljs-number">1</span></span>st month, </span><span><span class="hljs-number">11</span></span>=</span><span><span class="hljs-number">12</span></span>th month)
    [</span><span><span class="hljs-meta">5</span></span><span>] =&gt; Years since </span><span><span class="hljs-number">1900</span></span>
    [</span><span><span class="hljs-meta">6</span></span><span>] =&gt; Day of the year (</span><span><span class="hljs-number">1</span></span> to </span><span><span class="hljs-number">366</span></span>)
    [</span><span><span class="hljs-meta">7</span></span><span>] =&gt; Daylight saving flag (</span><span><span class="hljs-number">0</span></span>=No, </span><span><span class="hljs-number">1</span></span>=Yes)
)
</span></span>

By accessing this array, we can accurately retrieve different parts of the time, such as hours, minutes, or months.

2. Overview of strftime() Function

The strftime() function is used to format time according to a specified format string. It takes two parameters: the first is the format string, and the second is an optional timestamp. If no timestamp is provided, it defaults to the current time.

<span><span><span class="hljs-variable">$format</span></span><span> = </span><span><span class="hljs-string">"%Y-%m-%d %H:%M:%S"</span></span><span>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">strftime</span></span><span>(</span><span><span class="hljs-variable">$format</span></span><span>);
</span></span>

Common format specifiers include:

  • %Y: Four-digit year (e.g., 2025)

  • %m: Two-digit month (01 to 12)

  • %d: Two-digit day (01 to 31)

  • %H: Two-digit hour (00 to 23)

  • %M: Two-digit minute (00 to 59)

  • %S: Two-digit second (00 to 59)

This way, we can transform time into any desired format.

3. Using localtime() and strftime() Together

By combining localtime() and strftime(), we can achieve flexible time formatting. First, use localtime() to get the local time array, then convert it into a timestamp, and finally use strftime() to format and output it.

<span><span><span class="hljs-comment">// Get local time info</span></span><span>
</span><span><span class="hljs-variable">$localTime</span></span><span> = </span><span><span class="hljs-title function_ invoke__">localtime</span></span><span>();
<p></span>// Build a timestamp<br>
$timestamp = mktime(<br>
$localTime[</span>2],   // Hour<br>
</span>$localTime[</span>1],   // Minute<br>
</span>$localTime[</span>0],   // Second<br>
</span>$localTime[</span>4</span>] + 1, // Month (+1 since array starts at 0)<br>
</span>$localTime[</span>3</span>] + 1, // Day (+1 for offset)<br>
</span>$localTime[</span>5</span>] + 1900 // Year (+1900)<br>
);</p>
<p></span>// Format time with strftime<br>
$format = </span>"%Y-%m-%d %H:%M:%S";<br>
</span>echo </span>strftime(</span>$format, </span>$timestamp);<br>
</span></span>

This code first retrieves the local time with localtime(), builds a valid timestamp, and finally uses strftime() to format and output the time.

4. Common Use Cases

  1. Log file timestamps:

    In logging systems, each log entry often requires a timestamp with strict formatting. By using localtime() and strftime(), you can generate multiple time formats as needed.

    <span><span><span class="hljs-variable">$logTime</span></span> = </span><span><span class="hljs-title function_ invoke__">strftime</span></span>("%Y-%m-%d %H:%M:%S");
    </span><span><span class="hljs-keyword">echo</span></span> "Log Time: $logTime\n";
    </span></span>
  2. Custom date formats:

    In some applications, date formats may need to be customized based on region or user requirements. With strftime(), you can easily output formats like "July 13, 2025" or "2025-07-13 14:35:20".

    <span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-title function_ invoke__">strftime</span></span>("%Y年%m月%d日", $timestamp);  </span><span><span class="hljs-comment">// Outputs "2025年07月13日"</span></span>
    </span></span>
  3. Dynamically generated filenames:

    When naming files, using a timestamp as part of the filename ensures uniqueness. For example: log_2025-07-13_14-35-20.txt.

    <span><span><span class="hljs-variable">$filename</span></span> = "log_" . </span><span><span class="hljs-title function_ invoke__">strftime</span></span>("%Y-%m-%d_%H-%M-%S", $timestamp) . ".txt";
    </span><span><span class="hljs-keyword">echo</span></span> "Filename: $filename\n";
    </span></span>

5. Conclusion

By combining localtime() and strftime(), PHP developers can handle time and date more flexibly, producing time strings in specific formats. Whether for logging, file naming, or dynamic date displays, mastering these two functions can greatly improve development efficiency and code readability.