Functions for working with date and time in PHP

Noor

Administrator
Staff member
Sometimes when writing scripts, you will need to work with date and time. PHP has several functions that allow you to do this without much difficulty. In this article I want to talk about them.

To get the current time in PHP, there is a time() function. This function displays the current time in UNIX format, that is, the time elapsed since the night of January 1970 in seconds. To get the current time value, use this script:

Code:
<?php

$time = time();//Get the current time in UNIX format
echo($time);//Display the resulting time on the screen.

?>

Of course, you will find it inconvenient to use such a time system, so in PHP there is a time type conversion function date().

Syntax of the date() function:

date(Arguments of the type of time received, [Time in UNIX format]);

If you do not specify a UNIX Time when using this function, PHP will take this value as the current time (as the time() function would return).

Using this function, you can design the type of time you want to receive. To do this, when using the function, you need to set some of these parameters:

a – “am” or “pm”
A – “AM” or “PM”
B – Swatch Internet time
d – day (day) of the month, 2 digits with a leading zero, if necessary; i.e. from “01″ to “31″
D – day of the week, alphabetic, 3 letters; for example, “Fri”
F – month, alphabetic, long; for example, “January”
g – hour, 12-hour format without leading zeros; those. from “1″ to “12″
G – hour, 24-hour format without leading zeros; those. from “0″ to “23″
h – hour, 12-hour format; those. from “01″ to “12″
H – hour, 24-hour format; those. from “00″ to “23″
i – minutes; those. from “00″ to “59″
I (capital i) – “1″ if Daylight Savings Time, “0″ otherwise.
j – day (day) of the month without leading zeros; those. from “1″ to “31″
l ('L' in lower case) – day of the week, alphabetic, long; for example, “Friday”
L – boolean indicating whether the year is a leap year; those. “0″ or “1″
m – month; those. from “01″ to “12″
M – month, letter, 3 letters; for example, “Jan”
n – month without leading zeros; those. from “1″ to “12″
O – Difference with Greenwich time, in hours; for example, “+0200″
r – RFC 822 date format; for example, “Thu, 21 Dec 2000 16:01:07 +0200″ (introduced in PHP 4.0.4)
s – seconds; those. from “00″ to “59″
S – simple English suffix for the day (day) of the month, 2 characters; those. “st”, “nd”, “rd” or “th”
t – number of days in a given month; those. from “28″ to “31″
T – setting Timezone/Time zone on this machine; for example "EST" or "MDT"
U – Unix Epoch seconds (starting from January 1 1970 00:00:00 GMT)
w – day of the week, numeric, i.e. from “0″ (Sunday) to “6″ (Saturday)
W – ISO-8601 week number in the year, weeks starting from Monday/Monday (introduced in PHP 4.1.0)
Y – year, 4 digits; for example, “1999″
y – year, 2 digits; for example, “99″
z – day of the year; those. from “0″ to “365″
Z – time zone offset, in seconds (i.e. from “-43200″ to “43200″).
 
Back
Top