Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
How to get the date in PHP?
3.
How to get the time in PHP?
4.
What is automatic copyright year in PHP?
5.
How can we get our own timezone in PHP?
6.
How can we create a Date with mktime()?
7.
Frequently Asked Questions
8.
Key Takeaways
Last Updated: Mar 27, 2024

PHP date and time

Introduction

"PHP: Hypertext Preprocessor" acronyms are known as PHP. It is an open-source scripting language whose scripts run on the server and return to the browser as plain HTML. PHP is a language whose file can contain HTML, CSS, Javascript, or PHP code. These files have extension ".php". This language has many applications such as collecting form data, creating, opening, reading, deleting, writing, closing files on the server, etc. 

This blog focuses on PHP date and time functions used to format date and time. It has various formatting options, predefined functions, and other operations. We use the date() function for formatting date or time. Timestamp gets formatted with the date() function to a more readable date and time. Our computer stores date and time in UNIX timestamp, for example, midnight India Mean Time on January 1, 1999, i.e., January 25, 1999, 23:05:40 GMT.

How to get the date in PHP?

First, we will see the familiar characters we use for dates:

  • The day of the month, which is from 1 to 31, is represented by ‘d’ character 
  • A month which 1 to 12 is defined by ‘m’ character
  • A year that appears in four digits is represented by the ‘y’ character.
  • We represent the day of the week with ‘l’ ( lowercase 'L')

For adding additional formating to the date, we use characters like "/", ".", or "-" in-between characters.

Now we will see how to get a date.

date(format,timestamp)

Here the parameter format is required, which specifies the format of the timestamp. 

The parameter timestamp is optional, which specifies timestamp. By default it specifies the current date and time.

First of all, we will pass the required parameter of the date function with is the format.

<?php
echo "<h2>Coding ninja</h2></br>";
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");
?>
You can also try this code with Online PHP Compiler
Run Code

Output-

How to get the time in PHP?

First, we will see what the familiar characters we use for times are -

  • The ‘H’ character represents the 24-hour format of an hour (00 to 23).
  • The 12-hour format (01 to 12) with leading zeros is characterized by the ‘h’ character.
  • Minutes with leading zeros (00 to 59) is characterized by ‘i’ character.
  • Seconds with leading zeros (00 to 59) are represented by ‘s’ character.
  • We get lowercase Ante meridiem and Post meridiem (am or pm) by the character ‘a’.
  • We get uppercase Ante meridiem and Post meridiem (AM or PM) by the character ‘A’.

Now we'll see an example for getting current time in a particular format -

<?php
echo "<h2>Coding ninja</h2></br>";
echo "The time is " . date("H:i:sa");
?>
You can also try this code with Online PHP Compiler
Run Code

Output-

What is automatic copyright year in PHP?

This is one of the applications of the date() function in which we can automatically update the copyright year of our website.

<?php 
echo "<h2>Coding ninja</h2></br> &copy; 2010-"; echo date("Y");
?>
You can also try this code with Online PHP Compiler
Run Code

Output-

How can we get our own timezone in PHP?

When our server is in another country or set up for a different timezone, the code shows us the wrong time. But if we want the time of our location or any specific location, we can set the timezone we wish to.

For example -

<?php
echo "<h2>Coding ninja</h2></br>";
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:s a");
?>
You can also try this code with Online PHP Compiler
Run Code

Output-

Here we have given America/New_York value to the function date_default_timezone_set() to set the timezone to America/New_York, and the location's current time in a specific format is displayed as output.

How can we create a Date with mktime()?

For creating a timestamp for a specific date and time, we use the time() function. If we don't offer a particular date and time parameter, the current date and time will be provided. Unix timestamp for a date is returned by mktime() function. It calculates the number of seconds between the time specified and Unix Epoch (January 1, 1970, 00:00:00 GMT) and gives output.

Here is the syntax of the function -

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

 example -

<?php
echo "<h2>Coding ninja</h2></br>";
echo mktime(22, 21, 53, 11, 25, 2017);
?>
You can also try this code with Online PHP Compiler
Run Code

Output-

Timestamp for November 25, 2017,22 hrs, 21 mins, 53secs is created by this code.

Must Read PHP Projects With Source Code

Frequently Asked Questions

1.What is another way of getting time without using PHP's date() function?

Ans: We can use the time() function for getting the time in UNIX timestamp.

<?php
$timestamp = time();
echo "<h2>Coding ninja</h2></br>";
echo($timestamp);
echo "</br>";
echo(date("F d, y h:i:s A", $timestamp));
?>
You can also try this code with Online PHP Compiler
Run Code

Output-

2.How can we create a date from a string with strtotime()?

Ans: PHP's strtotime() function converts the human-readable date and time string format to UNIX timestamp format. The syntax of the function is strtotime(time, now).

<?php
echo "<h2>Coding ninja</h2></br>";
$d=strtotime("10:30pm April 15 2014");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
You can also try this code with Online PHP Compiler
Run Code

Output-

Key Takeaways

Don't come to a halt here. Check out our PHP Interview questions: Part 1 | Coding Ninjas Blog. Can you also check out the Why Use PHP programming in 2021? Its Pros and Cons | Coding Ninjas Blog blog. Check out more blogs, including software testing, security testing, etc., here

Live masterclass