PHP Date And Time

PHP Date And Time

PHP Date and Time is used to get and format the date and time with there respective functions.You can get the current date and time with these functions.The date and time will depend on your server.

PHP Date And Time

PHP Date And Time

Get PHP Date

You can get the date with PHP date() function.

Syntax

date(format,timestamp)

Code Explanation

  • format: it is used to display timestamp in a particular format.
  • timestamp: it is an optional parameter it specifies a timestamp. The default is the current date and time.

 

You can add other characters in place of / like “.”, or “-” .

  • Y is year
  • M is month
  • D is day

 

Get PHP Time

You can get the time with PHP date() function with time format.

  • h is 12-hour format of an hour with leading zeros (01 to 12)
  • i is Minutes with leading zeros (00 to 59)
  • s is Seconds with leading zeros (00 to 59)
  • a is Lowercase am or pm

How to Create Date format in PHP

You can create your own date with PHP mktime() function.

Example:

How to Get Your Time Zone using PHP

Example

More Explanation with an example:

Some Other string formats of date and time functions.

 

    • a: ‘am’ or ‘pm’ lowercase. For eg. pm

 

    • A: ‘AM’ or ‘PM’ uppercase. For eg. PM

 

    • d: Day of the month, a number with leading zeroes. For eg. 08

 

    • D: Day of the week (three letters). For eg. Tue

 

    • F: Month name. For eg. January

 

    • h: Hour (12-hour format – leading zeroes). For eg. 06

 

    • H: Hour (24-hour format – leading zeroes). For eg. 13

 

    • g: Hour (12-hour format – no leading zeroes). For eg. 6

 

    • G: Hour (24-hour format – no leading zeroes). For eg. 13

 

    • i: Minutes ( 0 – 59 ). For eg. 12

 

    • j: Day of the month (no leading zeroes. For eg. 13

 

    • l: (Lower ‘L’) Day of the week. For eg. Tuesday

 

    • L: Leap year (‘1’ for yes, ‘0’ for no). For eg. 1

 

    • m: Month of the year (number – leading zeroes). For eg. 1

 

    • M: Month of the year (three letters). For eg. Jan

 

    • r: The RFC 2822 formatted date. For eg. Tue, 13 Jan 2015 13:01:07 +0200

 

    • n: Month of year (number – no leading zeroes). For eg. 1

 

    • s: Seconds of the hour. For eg. 24

 

    • U: Timestamp. For eg. 687358984

 

    • y: Year (two digits). For eg. 15

 

    • Y: Year (four digits). For eg. 2015

 

    • z: Day of year (0 – 365). For eg. 20

 

    • Z: Offset in seconds from GMT. For eg. +5

 

PHP Date And Time

Read previous post:
angularjs logo
Inject Laravel 4 CSRF Token into AngularJS

Inject Laravel 4 CSRF Token into AngularJS At some point, you might want to add some basic security to your...

Close