7 examples of PHP date and formatting

PHP Date Introduction

The first thing to learn in PHP date is to understand about timestamps and how it is measured. Basically, the time stamp is the number of seconds from Jan 1, 1970, @ 00:00 GMT. This measurement is used by the date function of PHP.

As you create a timestamp in PHP you simply get a string of numbers that may look meaningless. It represents the number of seconds since the date you mentioned.

An example of date in m/d/y format

PHP gives you many options to format date and time as per the requirement like time zones, showing date in different formats etc. by using the date() function.

See another example of date formatting

The date Syntax

Following is the general syntax of date in PHP:

date(format, timestamp)

  • The format is the required parameter that specifies the timestamp format.
  • The timestamp is an optional parameter. The default is the current date and time.

Formatting Characters in date

Following is the list of characters that can be used as format parameters for formatting Day, Month, Year and Time.

Day formatting characters

  • d – its day of the month and can range from 01 to 31.
  • D – Day of Week in text format which is in 3 characters e.g. Mon, Tue etc.
  • z – returns the number of the day of the year – range is from 0-365.

Month parameters

  • m –It is the month range from 01 to 12.
  • n – returns a numeric number of the month, without leading zeros (1 to 12).
  • M – returns short form of the month in three characters like Jan, Oct, Dec etc.
  • F – returns the full name of the month e.g. January, February, March etc.

Year format characters

  • y – Represents a year (in two digits).
  • Y – represents the year in four digits.
  • w – This returns the numeric value of the day (1 for Monday, 0 for Sunday).

PHP Time parameters

  • a –  Lowercase Ante meridiem and Post meridiem, am or pm.
  • A – Uppercase Ante meridiem and Post meridiem, AM or PM.
  • B – Swatch Internet time, 000 through 999.
  • g  – 12-hour format of an hour without leading zeros, 1 to 12.
  • G – 24-hour format of an hour without leading zeros, 0 to 23.
  • h  – 12-hour format of an hour with leading zeros, 01 to 12.
  • H – 24-hour format of an hour with leading zeros, 00 to 23.
  • i   – Minutes with leading zeros, 00 to 59.
  • s  – Seconds, with leading zeros.

Now let us go through a few examples using PHP date() functions.

A date format Example

The following example uses PHP date function, that will take the current time from the system and format it to m/d/y date format as output.

PHP date formatting

Experience this example online



The output of above code is:

01/15/09

A date with another separator example

As such date formatting is quite flexible in PHP, in above example we use forward slash “/” to format dates. You can use other characters rather than using the forward slash.

The example below will display date by using ‘.’ to format the date using date PHP function. The format of date will be m.d.y.

PHP date seperator

Experience this example online



Output:

01.15.09

Date formatting in Year-month-day format example

Following example displays the current date in the Y-m-d format using the date function. The year will be four digits like 2008 while the month with two digits like 01 for Jan and day number of Month.

Experience this example online



Output:

2009- 01-15

Date formatting in day number, Month name, and year format example

The Following example displays date by day number, Full Month name like January, March etc. and year in four digits like 2008 by using date function!

PHP date

Experience this example online



example output (will be the current date as you view this demo):

15 January, 2009

Example of time and date

The following example returns the time and date by using the time function of PHP. The format will be m/d/y  h: i: s.

PHP date time

Experience this example online



An example of time zone

The following example will return the time zone by using ‘e’ formatting character with date function.

PHP date time zone

Experience this example online



Example of time zone abbreviation

The following example will return the time zone abbreviation by using the ‘T’.

Experience this example online



 

Further reading: PHP time | PHP mktime | timezone usage | getdate() function


Was this article helpful?

Related Articles

Leave A Comment?