Backend Development 5 min read

Common PHP Time Functions: Getting Current Time, Formatting Dates, Calculations, and Timezone Settings

This article explains how to use PHP's time‑related functions—including time(), date(), strtotime(), and date_default_timezone_set()—to obtain the current timestamp, format dates, perform date arithmetic, and configure timezones, providing clear code examples for each operation.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Common PHP Time Functions: Getting Current Time, Formatting Dates, Calculations, and Timezone Settings

In PHP development, handling dates and times is a frequent task. PHP offers powerful time functions that help developers format, calculate, and convert time values. This article introduces the most commonly used PHP time functions and explains their usage in detail.

Getting the Current Time

In PHP you can use time() to obtain the current Unix timestamp, which represents the number of seconds since January 1, 1970. Example:

<code>$current_time = time();
echo $current_time;</code>

The code outputs a timestamp such as 1636193742 . To convert this timestamp into a readable date and time, use the date() function:

<code>$current_time = time();
$date = date('Y-m-d H:i:s', $current_time);
echo $date;</code>

This prints a formatted date like 2021-11-06 15:35:42 . The first argument of date() is the format string, the second is the timestamp.

Formatting Dates and Times

The date() function can format dates using various specifiers:

Y – four‑digit year

m – two‑digit month

d – two‑digit day

H – hour in 24‑hour format

i – minutes

s – seconds

Example:

<code>$current_time = time();
$date = date('Y-m-d H:i:s', $current_time);
echo $date;</code>

Outputs the current date and time, e.g., 2021-11-06 15:35:42 .

Time Calculations

Use strtotime() to convert human‑readable date strings into timestamps for arithmetic. Example – get tomorrow’s date:

<code>$future_time = strtotime('+1 day');
$date = date('Y-m-d', $future_time);
echo $date;</code>

Outputs 2021-11-07 . For more complex calculations, such as the number of days between two dates:

<code>$start_date = strtotime('2021-11-01');
$end_date   = strtotime('2021-11-05');
$diff_days  = ($end_date - $start_date) / (60 * 60 * 24);
echo $diff_days;</code>

This prints 4 , the day difference.

Timezone Settings

Set the default timezone with date_default_timezone_set() . Example for Shanghai:

<code>date_default_timezone_set('Asia/Shanghai');
$current_time = time();
$date = date('Y-m-d H:i:s', $current_time);
echo $date;</code>

The output reflects the Shanghai timezone.

Summary

The article covered common PHP time functions—including obtaining the current time, formatting dates, performing calculations, and setting timezones. Proper use of these functions improves development efficiency and helps handle a wide range of time‑related requirements.

timezoneDateTime Functionsstrtotime
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.