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

date($format, $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

sample_date.php
<?php
date_default_timezone_set('Asia/Tokyo');

// Format the current date.
echo date("Y-m-d") . "\n"; // Outputs something like "2025-04-15".
echo date("F j, Y") . "\n"; // Outputs something like "April 15, 2025".

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

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

// Get the current timestamp with time().
echo time() . "\n"; // 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) . "\n"; // 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) . "\n"; // 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) . "\n"; // Outputs "2025-03-31".

// Get the current time in GMT with gmdate().
echo gmdate("Y-m-d H:i:s") . "\n"; // Outputs the time in GMT.

// Calculate 30 days after a fictional event date.
$event_ts = mktime(0, 0, 0, 4, 1, 2025); // Fictional event date
$thirty_days_later = $event_ts + (60 * 60 * 24 * 30);
echo date("Y-m-d", $thirty_days_later) . "\n"; // Outputs the date 30 days later.

Running the code produces the following output:

php sample_date.php
2025-04-15
April 15, 2025
2025-04-15 10:30:45
Tue
1744684245
11:30:45
2025-12-31 Wed
2025-03-31
2025-04-15 01:30:45
2025-05-01

Without a timezone setting, you may not get the expected local time

The default timezone used by date() depends on the server's php.ini configuration. To ensure a consistent timezone, call date_default_timezone_set() at the top of your script, or set date.timezone in php.ini.

<?php
// Without a timezone setting, the output depends on the server configuration.
// echo date("Y-m-d H:i:s"); // May not be in your expected timezone.

The same logic can also be written as:

<?php
date_default_timezone_set('Asia/Tokyo');
echo date("Y-m-d H:i:s"); // Outputs Japan time.

date() only accepts integer timestamps

The second argument to date() must be an integer UNIX timestamp. Passing a date string directly does not work as expected. Use strtotime() to convert a string to a timestamp first.

<?php
// Passing a string produces incorrect results.
// echo date("Y-m-d", "2025-04-15"); // Does not work correctly.

The corrected version is:

<?php
// Convert the date string to a timestamp first with strtotime().
$ts = strtotime("2025-04-15");
echo date("Y-m-d", $ts); // Outputs "2025-04-15".

The argument order of mktime()

The arguments for mktime() are in the order: hour, minute, second, month, day, year. This is different from the year-month-day order you might expect.

<?php
// Easy to get wrong: passing year-month-day order.
// $ts = mktime(0, 0, 0, 2025, 4, 15); // Wrong: 2025 is treated as the month.

The corrected version is:

<?php
// Correct: hour, minute, second, month, day, year.
$ts = mktime(0, 0, 0, 4, 15, 2025); // April 15, 2025, 00:00:00
echo date("Y-m-d", $ts); // Outputs "2025-04-15".

Practical Patterns

Calculating elapsed days

Computing how many days have passed since a specific event.

<?php
date_default_timezone_set('Asia/Tokyo');

// Calculating elapsed days from a fictional event date.
$event_ts = mktime(0, 0, 0, 1, 6, 2025);
$today_ts = mktime(0, 0, 0, (int) date("m"), (int) date("d"), (int) date("Y"));
$diff_days = (int) (($today_ts - $event_ts) / 86400);

echo "It has been {$diff_days} days since the event.\n";
sample_elapsed.php
php sample_elapsed.php
It has been 89 days since the event.

Getting the first and last day of a month

Retrieving the first and last day of a given month.

<?php
date_default_timezone_set('Asia/Tokyo');

$year = 2025;
$month = 4;

// First day of the month.
$first_day = mktime(0, 0, 0, $month, 1, $year);
echo date("Y-m-d", $first_day) . "\n"; // Outputs "2025-04-01".

// Day 0 of next month = last day of this month.
$last_day = mktime(0, 0, 0, $month + 1, 0, $year);
echo date("Y-m-d", $last_day) . "\n"; // Outputs "2025-04-30".
sample_month_range.php
php sample_month_range.php
2025-04-01
2025-04-30

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 .