Summary: in this tutorial, you’ll learn how to work with the PHP time()
function to get the current timestamp in the local timezone.
Introduction to Unix timestamps
Computers store a date and time as a UNIX timestamp or a timestamp in short.
A timestamp is an integer that refers to the number of seconds between 1970-01-01 00:00:00 UTC (Epoch) and the date and time to be stored.
Computers store dates and times as timestamps because it is easier to manipulate an integer. For example, to add one day to a timestamp, it simply adds the number of seconds to the timestamp.
PHP provides some helpful functions that manipulate timestamps effectively.
Getting the current time
To get the current time, you use the time()
function:
function time(): int
Code language: PHP (php)
The time()
function returns the current UNIX timestamp since Epoch (January 1 1970 00:00:00 GMT). For example:
<?php
echo time(); // 1626752728
Code language: PHP (php)
The return value is a big integer that represents the number of seconds since Epoch. To make the time human-readable, you use the date()
function. For example:
<?php
$current_time = time();
echo date('Y-m-d g:ia', $current_time) . '<br>';
Code language: PHP (php)
Output:
2021-07-13 5:47am
Code language: PHP (php)
The date()
function has two parameters.
- The first parameter specifies the date and time format. Here’s a complete list of valid date and time formats.
- The second parameter is an integer that specifies the timestamp.
Since the time()
function returns a timestamp, you can add seconds to it.
Adding to / subtracting from a timestamp
The following example shows how to add a week to the current time:
<?php
$current_time = time();
// 7 days later
$one_week_later = $current_time + 7 * 24 * 60 * 60;
echo date('Y-m-d g:ia',$one_week_later);
Code language: PHP (php)
In this example, we add 7 days * 24 hours * 60 minutes * 60 seconds to the current time.
Also, you can represent a time in the past by subtracting a number of seconds from the current time. For example:
<?php
$current_time = time();
// 1 day ago
$yesterday = $current_time - 24 * 60 * 60;
echo date('Y-m-d g:ia',$yesterday);
Code language: PHP (php)
timezone
By default, the time()
function returns the current time in the timezone specified in the PHP configuration file (php.ini).
To get the current timezone, you can use the date_default_timezone_get()
function:
<?php
echo echo date_default_timezone_get();
Code language: PHP (php)
To set a specific timezone, you use the date_default_timezone_set()
. It’s recommended that you use the UTC timezone.
The following shows how to use the date_default_timezone_set()
function to set the current timezone to the UTC timezone:
<?php
date_default_timezone_set('UTC');
Code language: PHP (php)
Making a Unix timestamp
To make a Unix timestamp, you use the mktime()
function:
mktime(
int $hour,
int|null $minute = null,
int|null $second = null,
int|null $month = null,
int|null $day = null,
int|null $year = null
): int|false
Code language: PHP (php)
The mktime()
function returns a Unix timestamp based on its arguments. If you omit an argument, mktime()
function will use the current value according to the local date and time instead.
The following example shows how to use the mktime()
function to show that July 13, 2020, is on a Tuesday:
<?php
echo 'July 13, 2021 is on a ' . date('l', mktime(0, 0, 0, 7, 13, 2021));
Code language: PHP (php)
Summary
- Use the
time()
function to return the current timestamp since Epoch in local timezone. - Use the
date_default_timezone_set()
function to set a specific timezone. - Use the
date()
function to format the timestamp. - Use
mktime()
function to create a timestasmp based on the year, month, day, hour, minute, and second.