Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. date() / time() / mktime()

date() / time() / mktime() Since: PHP 4(2000)

Retrieves the current date/time or a UNIX timestamp, and formats it as a date string. These are the most fundamental functions for working with dates and times in PHP.

Syntax

// Formats a date/time value and returns it as a string.
date($format, $timestamp);

// Returns the current UNIX timestamp.
time();

// Generates a UNIX timestamp for a specified date and time.
mktime($hour, $minute, $second, $month, $day, $year);

// Formats a date/time value in GMT and returns it as a string.
gmdate($format, $timestamp);

Function List

FunctionDescription
date($format, $timestamp)Formats a date/time value according to the format string. If the second argument is omitted, the current date and time is used.
time()Returns the current UNIX timestamp as an integer representing seconds. Takes no arguments.
mktime($hour, $minute, $second, $month, $day, $year)Returns the UNIX timestamp for a specified date and time. Out-of-range values are automatically carried over or rolled back.
gmdate($format, $timestamp)Works the same as date(), but outputs the time in GMT instead of the local timezone.

Common Format Characters

CharacterMeaningExample
Y4-digit year2025
mZero-padded 2-digit month01–12
dZero-padded 2-digit day01–31
HZero-padded 2-digit hour (24-hour clock)00–23
iZero-padded 2-digit minute00–59
sZero-padded 2-digit second00–59
NDay of the week (ISO-8601)1 = Monday, 7 = Sunday
wDay of the week0 = Sunday, 6 = Saturday
DAbbreviated day nameMon, Tue, Wed
UUNIX timestampSame value as time()

Sample Code

<?php
// Format the current date.
echo date("Y-m-d"); // Outputs something like "2025-04-15".
echo date("Y-m-d"); // Outputs something like "2025-04-15".

// Format the date and time together.
echo date("Y-m-d H:i:s"); // Outputs something like "2025-04-15 10:30:45".

// Get the day of the week.
$week = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
echo $week[date("w")]; // Outputs the current day of the week.

// Get the current timestamp with time().
echo time(); // Outputs an integer like "1744684245".

// Calculate the timestamp one hour from now.
$one_hour_later = time() + 3600;
echo date("H:i:s", $one_hour_later); // Outputs the time one hour from now.

// Generate a timestamp for a specific date with mktime().
$ts = mktime(0, 0, 0, 12, 31, 2025);
echo date("Y-m-d D", $ts); // Outputs "2025-12-31 Wed".

// Use mktime() overflow to find the last day of a month.
$last_day = mktime(0, 0, 0, 4, 0, 2025); // Day 0 of April is treated as the last day of March.
echo date("Y-m-d", $last_day); // Outputs "2025-03-31".

// Get the current time in GMT with gmdate().
echo gmdate("Y-m-d H:i:s"); // Outputs the time 9 hours behind Japan Standard Time.

// Practical example: setting a cookie expiration date.
$expire = time() + (60 * 60 * 24 * 30); // Timestamp 30 days from now.
echo date("Y-m-d", $expire); // Outputs the date 30 days from now.

Notes

date() is one of the most commonly used date functions in PHP. You can combine format characters to build any date string you need. A convenient feature is that non-format characters are passed through as-is, so you can include literal text directly in the format string.

The UNIX timestamp returned by time() represents the number of seconds elapsed since January 1, 1970, 00:00:00 UTC. You can easily calculate past or future dates by adding or subtracting seconds from a timestamp. The timezone is configured via date.timezone in php.ini, or by calling date_default_timezone_set() in your script. For Japan, use 'Asia/Tokyo'.

For more flexible date and time manipulation, use the DateTime class. To parse a date string into a timestamp, use strtotime().

If you find any errors or copyright issues, please .