DATE PHP | 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 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:

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:

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.

Output: 1581445636

Unix timestamp value

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

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 ante meridiem and post meridiem, 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 Ante meridiem and Post meridiem, like AM or PM

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..!

Subscribe For Latest Updates

Sign up to best of PHP Code Solutions, informed analysis and opinions on what matters to you.
Invalid email address
We promise not to spam you. You can unsubscribe at any time.

About Bikash

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.
View all posts by Bikash →

Leave a Reply

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