PHP DATE | PHP Date Function

In this article, we talk about PHP Date functionality and all the operations which we used on web applications.

Generally, we use the PHP date() function for showing dates in a different format.

Some important points about Date PHP function,

  • Computer system store the date-time on UNIX timestamp format.
  • This measure the time on the second basis since beginning of epoch (midnight Greenwich Mean Time on January 1, 1970, i.e. January 1, 1970 00:00:00 GMT).
  • PHP Date function converts the timestamp time format to a human-readable format.
PHP date Function

PHP Date Syntax

date(format, timestamp);

PHP Date() function Syntax

Example:

<?php
$CurrentDate = date("d/m/Y");
echo $CurrentDate;
?>
Output: 11/02/2020

NOTE:
PHP Date function date()

PHP date() returns date and time according to the builtin server or where you run that script.

How to Change PHP Date Format

Changing date format in PHP, PHP date function allowed some characters for changing the format like AM, PM and the day of the week. Some of the characters which is mainly used on the time of development are as follows,

  • Y – As a character suggest, refer to Year in four-digit like 2004 or 2020.
  • y – Small y also for a year but with last two-digit like 08 or 20.
  • M – Represent month as an acronym for Jan to Dec.
  • m – Represents numeric of the months like 01 to 12.
  • D – Represent the current day of the week, Mon to Sun.
  • d – Represents the day of the month with a numeric value like 01 to 31.

PHP Date format representation:

<?php
echo date("d-m-Y") . "<br>";
echo date("d/m/Y") . "<br>";
echo date("d.m.Y");
?>

Output:

11-02-2020
11/02/2020
11.02.2020

PHP time() Function

Same as the PHP date function, PHP time function is used to get the current time as UNIX timestamp value.

<?php
// Executed at February 11, 2020 07:27:16
$CurrentTimestamp = time();
echo($CurrentTimestamp);
?>

Output: 1581445636

Unix timestamp value

We can convert that UNIX timestamp value to human-readable value using PHP Date function. Check below,

<?php
$CurrentTimestamp = 1581445636;
echo(date("F d, Y h:i:s", $CurrentTimestamp));
?>

Output: February 11, 2020 07:27:16

Change PHP time Format Representation

As PHP date, similarly PHP time() function has some character for formatting the time.

  • s – seconds with leading zeros, like 00 to 59
  • a – lowercase anti meridian and post meridian, like am or pm
  • h – an hour in 12-hour format with leading zeros, like 01 to 12.
  • H – an hour in 24-hour format with leading zeros, like 00 to 23
  • i – minutes with leading zeros, like 00 to 59
  • A – uppercase Anti meridian and Post meridian, like AM or PM
<?php
echo date("F d, Y h:i:s A") . "<br>";
echo date("h:i:s") . "<br>";
echo date("h:i a");
?>

Output:

February 11, 2020 07:37:36 PM
07:37:36
07:37 pm

If you want you can use out PHP ONLINE EDITOR for checking those example source code execution.

How to Validate Date String in PHP

http://phpcoder.tech/how-to-validate-date-string-in-php/

Want to know more check PHP official website https://www.php.net/manual/en/function.date.php

Happy Coding..!

Was this article helpful?
YesNo
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH, my native place in Odisha. I am a Web Programmer by profession and working on more than 50 projects to date. My passion is working on the web-based project using PHP and relate to all CMS and frameworks which is based on PHP.
Posts created 163

One thought on “PHP DATE | PHP Date Function

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
mariobet - supertotobet - escort -

takip7.com

We are now accepting Guest Posting and creation of Backlinks
This is default text for notification bar