In this lesson, you will learn about the date and time in PHP. Every web developer has to deal with time and date at some point. Displaying and manipulating times and date may seem simple, but as uses may live in different time zone and probably have a different way of understanding time formats, it gets more challenging.

Date and Time in PHP

Computers don’t understand human readable date and time formats. PHP makes it easy by taking time in seconds elapsed since the Unix epoch: midnight on January 1, 1970 (UTC). This fact may not sense at first, but it makes calculating time and date intervals easy. PHP has lots of functions to help you to work with the human-readable time and Unix timestamps.

The function mktime() converts time into epoch timestamps, while date() returns a formatted time string.

Let’s say you ask the user to enter his date of birth and returns the day he was born.

Data Time Example PHP

<form class="" action="date_ac.php" method="post">
<p>Enter your age</p>
Month: <input type="text" name="m" value="" size="1">
Date: <input type="text" name="d" value="" size="1">
Year: <input type="text" name="y" value="" size="1"><br><br>
<input type="submit" name="" value="Submit">
</form>

To style the button, add the following CSS code.

input[type="submit"]{outline:0; border:0; background-color:#ADCF1A; border-bottom:4px solid #8CBF1C; width:40%; padding:5px;}

date_ac.php

<?php

if(isset($_POST['submit'])){
$m = $_POST['m'];
$d = $_POST['d'];
$y = $_POST['y'];
$timestamp = mktime(0,0,0,$m, $d, $y);
echo date('l', $timestamp);

?>

Get Current Date and Time in PHP

To get the current date and time, use the following function.

<?php
echo date('r'); // Thu, 18 Apr 2019 12:10:49 +0000
?>

The output can vary depending on the date and time this script is executed.

The parameter ‘r’ represents the the RFC 2822 formatted date (e.g. Fri, 18 Apr 2019 12:01:05 +0200). You can read more about these predefined date constants here. (https://www.php.net/manual/en/function.date.php)

Some of the data constants are:

Date format parameters

The format is a string that can contain multiple characters to generate a human-readable date. Some of these formatting characters are as follows:

  • D – the day of the week – Mon to Sun
  • d – the day of the month in digits with leading zeros (01 to 31)
  • M – the month – Jan to Dec
  • m – the month in digits – (01 to 12)
  • Y – the year in four digits – (2019)
  • y – the year in two digits (19)
<?php
echo date("h:i:s") . "<br/>";
echo date("M,d,Y h:i:s A") . "<br/>";
echo date("h:i a");
?>

PHP DateTime Format

   <?php
 echo date("h:i:s") . "<br/>";
 echo date("M,d,Y h:i:s A") . "<br/>";
 echo date("h:i a");
   ?>

PHP DateTime Format

The following piece of code will display copyright information (Year established to the current year).

<?php

echo "&copy; 2007 - ". date("Y");

?>

To display the current date and time, you can also use the DateTime() function.

<?php
$date = new DateTime('now');
echo $date->format('d-m-Y H:i:s');
?>
PHP File Upload Tutorial Home PHP Error Handling
Last modified: April 25, 2019