How to Use Date & Time Function in PHP?

Friends, today I am going to tell you about date and time function.Date and time are used in PHP as well as in all programming languages. If you want to use date and time then you have to use date() and time() functions. You will be able to use date and time easily through Date() and time() functions.
Example: – If you want to display the time and date on a website, as you must have seen in many websites that the date and time keep moving at the top, for that the Date() and time() functions have to be used. .
Lets understand with a simple example.
WordPress is a CMS (Content Management System) which is made of PHP, through which you can create any website or create your own blog. In WordPress, if you create a blog or in any blog website, you see a new post at the top, the post which has been posted today is the first to be seen, how is it that is because we use date and time in the database And from there use the date and time to show the data. Or even if we insert, date and time are stored in the database only through date() and time() functions.If we want to show new data, then we display the data by latest date, so we need date() and time() function.
The PHP date() and time() functions are widely used in the MYSQL database.
like –
1. Display data from MYSQL database tables using date or time.
2. Using the new or old date and time, you can set the data at the top. Means the data related to the new date should appear first on the page or appear in the latest category etc.
3. You can also search the data by date and time using MySQL data. Like if we want data of last 2 months then we can do that also with date() and time() function etc.

date() function in PHP

The date() function is used to display the date and time in a user readable format. The date() function is used to insert the data in MYSQL and which date to display the data from MYSQL and which data should be displayed first, old or new, all that can be done with the date() function.

The date() function converts the timestamp (system generated format) into a format for the user to understand. Timestamp is a format of date and time, which the user does not understand, it is only understood by the system. We use the date() function only to convert that timestamp format so that any user can understand it easily.

Date function syntax in PHP

date(format,[timestamp]);

date() – is a function which is used to convert time and date in human readable format so that user can understand easily.
“format” -Formae refers to the presentation of date and time. In which format do you want to use date and time? For example – “DD/ MM/YYYY” -(12/10/1990) For this format, we define the format parameters like PHP ‘d-m-Y’ or ‘d /m /Y’ etc. You can display year only using PHP date format parameter’Y’ (uppercase).
‘D’ – is a format paramter of date() function by which we can use day in “Sun to Sat” format. Similarly, you will get all the format parameters, using which you will be able to use date and time in different ways.
“Timestamp” Timestamps are optional. If you do not define a timestamp, PHP gets access to the PHP current date time on the server. You will understand all this in the examples given below.

Examples of PHP date() and time() functions –

In the example below, you can see an example of the date() and time() functions along with the output.

Example-

<?php
$date=date('m-d-y');
echo $date;
?>

In above example we use some format parameters from which date can be used in different format, here we pass those parameters to PHP date() function and use echo() function to see format By doing the date has been displayed. By which you know in which format the dates are coming.

date() function format parameter in PHP

The following format parameter is used to display date and time in different formats.
‘m’- m format parameter denotes the month. To use the month in numbers 1 to 12, the m format parameter is used.

Month Example-

<?php
$var=date('m');
echo $var;
?>

‘d’- d- d parameter is for the date of the month. The d (lowercase) parameter is used to convert the date into a numeral.

Example-

<?php
$var=date('d');
echo $var;
?>

‘y’- The y parameter of the PHP date() function is used to display the year. Display the year in numbers. eg 07, 10,11

Example-

<?php
$var=date('y');
echo $var;
?>

‘D’- D (Uppercase) parameter is used for the day of the week. It shows the name of week days in “Sun to Sat” format.

Example-

<?php
$var=date('D');
echo $var;
?>

‘M’ – Uppercase M parameter represents the month name in a format such as January to December. e.g. “Jan to Dec”

Example-

<?php
$var=date('M');
echo $var;
?>

‘Y’ – Uppercase Y represents the four digits (YYYY) of the year. Like 2010, 2019, 2050.

Example-

<?php
$var=date('Y');
echo $var;
?>

‘h’ –Lowecase h format parameter of date() function shows hour in 12 hour format (01 to 12).

Example-

<?php
$var=date('h');
echo $var;
?>

‘H’ -Uppercase H format parameter of the date() function represents hours in 12-hour format (01 to 24).

Example-

<?php
$var=date('H');
echo $var;
?>

‘i’ – The lowercase i parameter represents minutes from 00 to 59.

Example-

<?php
$var=date('i');
echo $var;
?>

‘s’ – The lowercase s parameter denotes seconds from 000 to 59.

Example-

<?php
$var=date('s');
echo $var;
?>

‘a’ – The lowercase a parameter (lower case am , pm format) denotes am and pm to indicate whether am is now pm.

Example-

<?php
$var=date('a');
echo $var;
?>

‘A’ – Uppercase A is a format parameter of the date() function, which (uppercase AM, PM) indicates AM and PM is now AM or PM.

Example-

<?php
$var=date('A');
echo $var;
?>

‘F’-uppercase F parameter represents the month name of the year from January to December e.g. January to December.

Example-

<?php
$var=date('F');
echo $var;
?>

‘t’- t denotes the days in the month e.g. 28 to 31. How many days are in the month?

Example-

<?php
$var=date('t');
echo $var;
?>

‘z’- This represents the day of the year from 0 to 365.

Example-

<?php
$var=date('z');
echo $var;
?>

‘g’-lowercase g denotes hours without zero(0). It does not contain any zero (0) digits. [1 to 12], .

Example-

<?php
$var=date('g');
echo $var;
?>

‘G’ – The uppercase G parameter of the date() function represents the hour [0 to 24].

Example-

<?php
$var=date('G');
echo $var;
?>

‘r’-r parameter of date() function represents the complete date and time.

Example-

<?php
$var=date('r');
echo $var;
?>

‘l’- parameter(lowercase) of the date() function l denotes the name of the week [[Sunday to Saturday]

Example-

<?php
$var=date('l');
echo $var;
?>

Displaying time in 12 hour format

This is the best practice if you want to display the time in fromat 12 hours. (Real system current time)

Example-

<?php
date_default_timezone_set('Asia/Kolkata');
echo date('g:i a');
?>

‘g’—g parameter represents the hour (in 12 hours) as you would have seen in the parameter above.

i- i parameter denotes seconds.

a – AM, PM represent periods.

‘Asia/Kolkata’ – ‘Asia/Kolkata’ are time zone identifiers by which you can use your local time, its complete list will be found in the bottom link, similarly you can use all the parameters together. And you can use time and date in different formats.

Date and time together

You can display the date and time together in a function.

Example-

<?php
date_default_timezone_set('Asia/Kolkata');
echo date('d-m-Y g:i a');
?>