Date & Time in PHP:

Dates and time are a very important part of our every day life. PHP provides a powerful tool for date and time. In PHP you can manipulate date and time with “date()” function.

You can change the format of date with very easily by date() function.

Syntax:

            date(format, timestamp);

In date function there are two parameters:

  • format: it is a required parameter and it specifies the format of timestamps
  • timestamp: it is an optional parameter and by default it takes the current date and time

 Get A Simple Date in PHP:

The “format” parameter in the date function specifies how the date and time will display. Here is a list of characters that a date function can contain:

Format Description Example
A ‘am’ or ‘pm’ in lowercase am
A ‘AM’ or “PM’ in uppercase AM
D Day of month, a number with leading zeroes 25
D Day of week (three letters) Sun
F Name of Month December
H Hour(12 hr format leading with zero) 11
H Hour(24 hr format leading with zero) 23
G Hour(12 hr format no leading with zero) 11
G Hour(24 hr format no leading with zero) 23
I Minutes(0-59) 45
J Day of month, a number with no leading zeroes 25
l(small L) Day of the week Sunday
L Leap Year (if yes than”1” if no than “0”)
M Month of Year (leading zero) 02
M Month of year (three letters) Feb
N Month of Year (no leading zero) 2
S Seconds of hrs 30
U Timestamp 948327434
Y Year in two digits 08
Y Year in four digits 2008
Z Day of year (0-365) 225
Z Offset in seconds from GMT +5

 Example:

Now we will use some of format characters in our example

 Get A Simple Time in PHP:

We can also get the time with the date() function. Here is a list of characters that are use to get the time:

Format Description Example
h 12 hr format leading with zero 02 and 11
i Minutes leading with zero 06 and 45
s Seconds leading with zero 09
a am and pm in lower case am

 Example:

 Date Creation in PHP with the help of mktime() function:

You know that if you don’t specify the optional timestamp parameter it will take the current date and time. The mktime() function is use to create a new date.

Syntax:

      mktime(hour,min, second, month, day, year);

Example:

 strtotime() function in PHP:

The strtotime() function is used to convert the human readable string into the Unix time.

Example:

This is a simple example of converting string into unix time

 Example:

Now we will take some another example that shows how the PHP language is clever

 

Leave a Reply

Your email address will not be published. Required fields are marked *