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:

$current_time = time();
echo $current_time;

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

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

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:

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

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:

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

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

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

This prints 4, the day difference.

Timezone Settings

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

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

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPTimezonetime functionsdate()strtotime()
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

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.