Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

PHP Tutorial

PHP HOME PHP Intro PHP Install PHP Syntax PHP Comments PHP Variables PHP Echo / Print PHP Data Types PHP Strings PHP Numbers PHP Casting PHP Math PHP Constants PHP Magic Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP Loops PHP Functions PHP Arrays PHP Superglobals PHP RegEx PHP RegEx Functions

PHP Forms

PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete

PHP Advanced

PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Callback Functions PHP JSON PHP Exceptions

PHP OOP

PHP What is OOP PHP Classes/Objects PHP Constructor PHP Destructor PHP Access Modifiers PHP Inheritance PHP Constants PHP Abstract Classes PHP Interfaces PHP Traits PHP Static Methods PHP Static Properties PHP Namespaces PHP Iterables

MySQL Database

MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data

PHP XML

PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat PHP XML DOM

PHP - AJAX

AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll

PHP Examples

PHP Examples PHP Compiler PHP Quiz PHP Exercises PHP Server PHP Syllabus PHP Study Plan PHP Certificate

PHP Reference

PHP Overview PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Exception PHP Filesystem PHP Filter PHP FTP PHP JSON PHP Keywords PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP Network PHP Output Control PHP RegEx PHP SimpleXML PHP Stream PHP String PHP Variable Handling PHP XML Parser PHP Zip PHP Timezones

PHP Date and Time


PHP Date and Time Functions

PHP has many built-in date and time functions for handling dates and time.

The core functions for date and time is:

  • date() - formats a local date and/or a time
  • mktime() - returns the Unix timestamp for a date
  • strtotime() - converts an English textual datetime string into a Unix timestamp
  • time() - returns the current time as a Unix timestamp
  • date_default_timezone_set() - sets the default timezone to be used
  • date_default_timezone_get() - gets the default timezone

The PHP Date() Function

The date() function is used to format a local date and/or a time to a more readable date and time.

Syntax

date(format, timestamp)
Parameter Description
format Required. Specifies the format of the timestamp
timestamp Optional. Specifies a Unix timestamp. Default is the current local date and time

Using the Date() Function

The required format parameter of the date() function specifies how to format the date (or time).

Here are some characters that are commonly used for dates:

  • d - Represents the day of the month (01 to 31)
  • j - Represents the day of the month (1 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)
  • l (lowercase 'L') - Represents the day of the week
  • F - Represents the month

Other characters, like"/", ".", or "-" can also be inserted between the format parameters to add additional formatting.

The example below formats the local date in five different ways:

Example

<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l"). "<br>";
echo date('l, F j, Y');
?>
Try it Yourself »


PHP Tip - Automatic Copyright Year

Use the date() function to automatically update the copyright year on your website:

Example

&copy; 2010-<?php echo date("Y");?>
Try it Yourself »

Display the Time

The date() function is also used to format a local time to a more readable format.

Here are some characters that are commonly used for times:

  • H - 24-hour format of an hour (00 to 23)
  • h - 12-hour format of an hour with leading zeros (01 to 12)
  • i - Minutes with leading zeros (00 to 59)
  • s - Seconds with leading zeros (00 to 59)
  • a - Lowercase am/pm

The example below outputs the current time in two different formats:

Example

<?php
echo "The time is " . date("H:i:s") . "<br>";
echo "The time is " . date("h:i:s a");
?>
Try it Yourself »

The PHP date_default_timezone_set() Function

Note that the date() function will return the current local date/time of your server!

To get the date and time to be correct according to a specific location, also set the timezone you want to use, with the date_default_timezone_set() function.

The example below sets the timezone to "America/New_York", then outputs the current date and time in the specified format:

Example

<?php
date_default_timezone_set("America/New_York");
echo "The current date and time is " . date("Y-m-d H:i:s");
?>
Try it Yourself »

The PHP date_default_timezone_get() Function

To return the default timezone used by all date/time functions in the script, use the date_default_timezone_get() function:

Example

<?php
echo date_default_timezone_get();
?>
Try it Yourself »

The PHP mktime() Function

The optional timestamp parameter in the date() function specifies a timestamp. If omitted, the current date and time will be used (as in the examples above).

The mktime() function returns the Unix timestamp for a date. The Unix timestamp is the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Syntax

mktime(hour, minute, second, month, day, year)

The following example returns the Unix timestamp for a date, and then use it to find the day of that specific date:

Example

<?php
// Set the default timezone to use
date_default_timezone_set("UTC");

$d = mktime(0, 0, 0, 10, 3, 1975);
echo "October 3, 1975 was on a " . date("l", $d);
?>
Try it Yourself »

The PHP time() Function

The time() function returns the current time as a Unix timestamp. The Unix timestamp is the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Syntax

time()

Example

<?php
echo "Now: " . time();
?>
Try it Yourself »

Here we format the timestamp to a readable date and time:

Example

<?php
// Get the current Unix timestamp
$ts = time();

// Format timestamp
$curDate = date('Y-m-d H:i:s', $ts); 

echo $curDate;
?>
Try it Yourself »

The PHP strtotime() Function

The strtotime() function converts an English textual datetime string into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Syntax

strtotime(datetime_string, basetimestamp)

The strtotime() function is quite clever about converting a string to a date, so you can put in various values:

Example

<?php
$d = strtotime("10:30pm November 15 2025");
echo "Date is " . date("Y-m-d H:i:s", $d) . "<br>";

$d = strtotime("now");
echo "Date is " . date("Y-m-d H:i:s", $d) . "<br>";

$d = strtotime("+5 days");
echo "Date is " . date("Y-m-d H:i:s", $d) . "<br>";

$d = strtotime("+2 weeks 4 days 2 hours 20 seconds");
echo "Date is " . date("Y-m-d H:i:s", $d) . "<br>";

$d = strtotime("last Sunday");
echo "Date is " . date("Y-m-d H:i:s", $d);
?>
Try it Yourself »

Tip: The strtotime() is not perfect, so remember to check the strings you put in there!


More Date Examples

The following example outputs the dates for the next six Saturdays:

Example

<?php
$startdate = strtotime("Saturday");
$enddate = strtotime("+6 weeks", $startdate);

while ($startdate < $enddate) {
  echo date("M d", $startdate) . "<br>";
  $startdate = strtotime("+1 week", $startdate);
?>
Try it Yourself »

Complete PHP Date Reference

For a complete reference of all date functions, go to our complete PHP Date Reference.

The reference contains a brief description, and examples of use, for each function!



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.