Quick Reach
- 1 PHP Date Introduction
- 2 The date Syntax
- 3 Formatting Characters in date
- 4 A date format Example
- 5 A date with another separator example
- 6 Date formatting in Year-month-day format example
- 7 Date formatting in day number, Month name, and year format example
- 8 Example of time and date
- 9 An example of time zone
- 10 Example of time zone abbreviation
- 11 Related
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.
Experience this example online
1
2
3
4
5
6
7
8
9
|
<?php
// PHP will take your current date and format date according to specified characters
// Lets says, its 15 Jan 2009
echo date(“m/d/y”);
?>
|
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.
Experience this example online
1
2
3
4
5
6
7
8
9
|
<?php
// PHP will take your current date and format date according to specified characters
// Lets says, its 15 Jan 2009
echo date(“m.d.y”);
?>
|
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
1
2
3
4
5
6
7
8
9
|
<?php
// PHP will take your current date and format date according to specified characters
// Lets says, its 15 Jan 2009
echo date(“Y-m-d”);
?>
|
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!
Experience this example online
1
2
3
4
5
6
7
8
9
|
<?php
// PHP will take your current date and format date according to specified characters
// Lets says, its 15 jan 2009
echo date(“d F, Y”);
?>
|
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.
Experience this example online
1
2
3
4
|
<?php
$t=time();
echo(date(“m/d/y h:i:s”,$t));
?>
|
An example of time zone
The following example will return the time zone by using ‘e’ formatting character with date function.
Experience this example online
1
2
3
4
5
|
<?php
echo date(“e”);
?>
|
Example of time zone abbreviation
The following example will return the time zone abbreviation by using the ‘T’.
Experience this example online
1
2
3
4
5
|
<?php
echo date(“T”);
?>
|
Further reading: PHP time | PHP mktime | timezone usage | getdate() function
Leave A Comment?