|
|
Date and Time Functions in Php
Have you wanted to dynamically output the date or the
current time on your webpage? Well now you can. Everytime
a visitor loads your page, it can output the current date/time.
date() The Php Date Function
This function returns the current, local date and time, formatted
according to your wishes. What you pass into the function is a cryptic-looking
string of characters that are actually codes for formatting the date/time.
Without further ado, here are some examples:
Example Code |
---|
<?php echo date('l'); ?>
| Example Output |
---|
Friday |
Example Code |
---|
<?php echo date ('l, F jS Y'); ?>
| Example Output |
---|
Friday, May 3rd 2024 |
Example Code |
---|
<?php echo date('l, jS of F'); ?>
| Example Output |
---|
Friday, 3rd 2024f May |
So what you can do here is pass special characters to the date
function, such as 'l' (lowercase L) to print the month as a word, or 'Y' to print
the year in 4 digits. You can also pass other chars that have no meaning
to add that text to the formatted date, such as 'of' in the last example. To
expound on the idea of adding other text, spaces and punctuation to the formatting, there
is just one thing to remember. If the text or character that you want to add already
has a special meaning (ie, it is one of the codes to format the time/date), you have
to 'escape' it so that the char is simply printed out instead us being used
to format the date string. To escape a char, you simply put a backslash '\' right
before it (without the quotes). Here is an example ( 't' and 'h' both have
special meaning for the date function):
Example Code |
---|
<?php echo date('l \t\h\e jS'); ?>
| Example Output |
---|
Friday the 3rd |
List of all the Formatting possiblilities for the date() function
Sort by special formatting char | Group by function
Special formatting character | Explaination | Example of what this produces (Using this server's current time and date) (Fri, 03 May 2024 01:11:15 -0400) |
Hours, Minutes & Seconds |
g | 12-hour format of the hour without leading zeros | 1 |
G | 24-hour format of the hour without leading zeros | 1 |
h | 12-hour format of the hour with leading zeros | 01 |
H | 24-hour format of the hour with leading zeros | 01 |
i | Minutes with leading zeros | 11 |
s | Seconds, with leading zeros | 15 |
U | Seconds since the Unix Epoch which is January 1 1970 00:00:00 GMT. (same as calling the time() function). | 1714713075 |
AM & PM |
a | Lowercase Ante meridiem or Post meridiem | am |
A | Uppercase Ante meridiem or Post meridiem | AM |
Day |
d | Day of the month in 2 digits with leading zeros | 03 |
j | Day of the month without leading zeros | 3 |
jS | Just an example of calling jS together ie:
<?php echo date('jS'); ?>
| 3rd |
D | A three-letter abbreviation for the day | Fri |
t | Number of days in the given month | 31 |
z | The current day of the year | 123 |
w | The day of the week as a number. Starts at Sunday with 0 and ends with Saturday being a 6 | 5 |
l | This is a lowercase L. The day of the week, written out | Friday |
Week |
W | Week of the year, using the ISO-8601 standard (weeks starting on Monday) (Must have PHP 4.1.0 or higher) | 18 |
Month |
F | The month, written out. | May |
m | Month of the year as a number, with leading zeros | 05 |
M | A three-letter abbreviation for the month | May |
n | Month of the year as a number, without leading zeros | 5 |
Year |
Y | The year in 4 digits | 2024 |
y | The year in 2 digits | 24 |
Timezone, Daylight Savings time & Leap year |
T | Timezone setting of this computer | EDT |
Z | Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. | -14400 |
I | This is an uppercase i. 1 if we are in Daylight Savings Time, 0 if Standard time. | 1 |
L | 1 if it's a leap year, 0 if it's not. | 1 |
O | Difference in hours from Greenwich time (GMT) to local time | -0400 |
Miscellaneous |
r | The current date in the RFC 822 format | Fri, 03 May 2024 01:11:15 -0400 |
B | Swatch Internet time (Whatever that is) | 257 |
S | English ordinal suffix for the day of the month, these would be: st, nd, rd or th. It's common to use this along with j formatter | rd |
jS | Just an example of calling jS together ie:
<?php echo date('jS'); ?>
| 3rd |
time() The Php Time Function
This function returns the number of seconds since the Unix Epoch which is January 1 1970 00:00:00 GMT. It is the same as calling the
date() function with the U formatter.
Example Code |
---|
<?php echo date('U'); ?>
| Example Output |
---|
1714713075 |
Example Code |
---|
<?php echo time(); ?>
| Example Output |
---|
1714713075 |
|
|