PHP date Uses and Example

This tutorial help to understand date classes and methods with uses using simple daily use example. PHP is methods rich programming language.Its providing a lot of method for manipulation of date and time.
The date can be divided into two part, one is date and other is time, let’s take a simple example to understand date time in PHP.

How to get Current TimeStamp Using PHP

PHP provides time() function to get current date and time,This function return integer value which has full information of current date time.This method do not need any parameter.

The output is –

The time() function return the above integer value, This is the number of seconds, its also known as UNIX epoch timestamp. You can canvert this integer value into human readable form using other PHP method.You could not show this integer value to end user.

You can also check other recommended Date PHP tutorials,

Convert Timestamp into Date Format Using Date()

We have unix timestamp and now we need to convert timestamp integer value into human readable form. PHP date() function convert timestamp into date format.Its very easy to use.You need to pass datetime timestamp value and format of date which would be resulted format of timestamp.

timestamp is optional, its use the current time if no timestamp is given.

There are following options code available to format string can contain:

Sr.NoFormat & DescriptionExample
1

a

‘am’ or ‘pm’ lowercase

pm
2

A

‘AM’ or ‘PM’ uppercase

PM
3

d

Day of month, a number with leading zeroes

20
4

D

Day of week (three letters)

Thu
5

F

Month name

January
6

h

Hour (12-hour format – leading zeroes)

12
7

H

Hour (24-hour format – leading zeroes)

22
8

g

Hour (12-hour format – no leading zeroes)

12
9

G

Hour (24-hour format – no leading zeroes)

22
10

i

Minutes ( 0 – 59 )

23
11

j

Day of the month (no leading zeroes

20
12

l (Lower ‘L’)

Day of the week

Thursday
13

L

Leap year (‘1’ for yes, ‘0’ for no)

1
14

m

Month of year (number – leading zeroes)

1
15

M

Month of year (three letters)

Jan
16

r

The RFC 2822 formatted date

Thu, 21 Dec 2000 16:01:07 +0200
17

n

Month of year (number – no leading zeroes)

2
18

s

Seconds of hour

20
19

U

Time stamp

948372444
20

y

Year (two digits)

06
21

Y

Year (four digits)

2006
22

z

Day of year (0 – 365)

206
23

Z

Offset in seconds from GMT

+5

I have taken three examples to understand PHP date function, to convert php timestamp into Y-m-d format with 24-hour format , convert current time stamp into m/d/y format with 12-hour format and uppercase ante meridian and post meridian notation, third one is converting unix timestamp into full date string representation.

The result are –

How to Use getdate() PHP method

Sometime we need to extract only some portion of date variable, like we need to show month, current day and year etc, that time we will play with php getdate() method, PHP getdate() method returns an associative array containing information about the passed datetime.

Simple usage of getdate() method

I have taken two example of datetime using getdate php method, firstone using timestamp parameter and second one without parameter.

Result getdate with Parameter-

Result getdate without parameter –

Sr.NoKey & DescriptionExample
1

seconds

Seconds past the minutes (0-59)

20
2

minutes

Minutes past the hour (0 – 59)

29
3

hours

Hours of the day (0 – 23)

22
4

mday

Day of the month (1 – 31)

11
5

wday

Day of the week (0 – 6)

4
6

mon

Month of the year (1 – 12)

7
7

year

Year (4 digits)

1997
8

yday

Day of year ( 0 – 365 )

19
9

weekday

Day of the week

Thursday
10

month

Month of the year

January
11

0

Timestamp

948370048

The function getdate() optionally accepts a time stamp and returns an associative array containing information about the date. If you omit the time stamp, it works with the current time stamp as returned by time().

Conclusion:

We have create timestamp using PHP method and converted integer timetsamp into different human readable format date using PHP date method.I hope, You enjoyed this PHP date time article, You can get here full list of Date & Time Functions.