Code has been added to clipboard!

How to Get PHP DateTime in Your Websites and Applications

Reading time 5 min
Published Aug 8, 2017
Updated Oct 2, 2019

PHP DateTime represents the date and time, and this tutorial will reveal the ways you can add and format date and time using PHP.

The functions for date manipulation are most widely used when designing websites or executing certain SQL queries. PHP current date might be displayed at the top of the page. Every entry can be dated and every comment marked with the exact time. In PHP time and date are easy to set, as there are inbuilt functions for that.

When working with the functions to receive PHP DateTime, remember to pay attention to the usual time inconsistencies that affect your local server. Don't forget about different timezones, daylight savings, or leap years.

PHP DateTime: Main Tips

  • The PHP date() function is needed to format date/time in your script.
  • It is used to format the timestamp to be more readable. Basically, to line the characters in a more eye-pleasing order.
  • Use mktime() function when you need an Unix date timestamp.
  • You can use PHP functions to make automatic updates on the copyright date of your website.

date() Function Explained

To learn PHP date formatting, let's first look at the correct syntax of date() function:

date(format,timestamp)

The format part is required, as it specifies the format of the timestamp. Entering the timestamp itself is optional: if you don't, PHP current date and time will be displayed by default.

There is a set of characters more commonly used to describe a PHP date format.

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

Symbols like a slash (/), a dot (.), or a dash (-) can be added for additional formatting.

Let's look at an example below. You can see there are four different ways to do PHP date formatting:

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");
?>

Update Dates Automatically

Stuff that requires to be updated the rarest is usually the first to be forgotten. This often happens with the copyright dates deep at the bottom of the website. Luckily, there are ways to avoid this.

To automatically refresh the copyright year on your website, you can use the function displayed in the example script below:

Example
&copy; 2010-<?php echo date("Y");?>

Time Functionalities

We already indicated a set of symbols that are most commonly used for PHP date formatting. There's also a handful of symbols used specifically for displaying time:

  • h - 12-hour format of an hour with leading zeros (01-12)
  • i - minutes with leading zeros (00-59)
  • s - seconds with leading zeros (00-59)
  • a - AM/PM (before/after noon)

Using the script example below will output the current time in the specified format:

Example
<?php
  echo "The time is " . date("h:i:sa");
?>

Note: Have in mind the date() function will fetch back the present PHP DateTime of the server.

Getting Around Timezones

Does the PHP DateTime you are getting not match your current time? It might happen if you are using a server that is located in a different timezone than you. It's not that hard to fix: you need to set a correct PHP timezone for your DateTime format to use.

Look at the example below. You can see that the PHP timezone is set to America/New_York, which means the script will output PHP time that is relevant to New Yorkers.

Example
<?php
  date_default_timezone_set("America/New_York");
  echo "The time is " . date("h:i:sa");
?>

mktime() Method Defined

One more function that you should learn to use to display PHP date time is mktime(). It is used to fetch back the Unix date timestamp.

Unix date timestamp is a long integer that displays the time (in seconds) between the Unix Epoch (1st of January 1, 1970, 00:00:00 GMT) and the time specified. In the syntax formula below you can see the function parameters: hours, minutes, seconds, months, days, and years:

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

Look at the script below to see how the PHP date time is created using parameters from mktime():

Example
<?php
  $date = mktime(1114548122014);
  echo "Created date is " . date("Y-m-d h:i:sa", $date);
?>

strtotime(): From String to PHP Date

The strtotime() function changes a readable string to a Unix time. This is how its syntax looks like:

strtotime(time,now)

In the block of code below, PHP date time is created using strtotime():

Example
<?php
  $d = strtotime("10:30pm April 15 2014");
  echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>

PHP is very intuitive when converting strings to dates. Therefore, you can try and input many values:

Example
<?php
  $d = strtotime("tomorrow");
  echo date("Y-m-d h:i:sa", $d) . "<br>";

  $d = strtotime("next Saturday");
  echo date("Y-m-d h:i:sa", $d) . "<br>";

  $d = strtotime("+3 Months");
  echo date("Y-m-d h:i:sa", $d) . "<br>";
?>

PHP DateTime: Summary

  • Date/time format in PHP codes can be set using date() function. It will also makes the timestamp more readable.
  • When Unix date timestamp is needed, PHP mktime() function should be chosen.
  • PHP DateTime format can be used to make automatic updates on website copyright dates.
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()