All TalkersCode Topics

Follow TalkersCode On Social Media

PHP Date And Time

PHP Date and Time is used to get and format the date and time with there respective functions

Get A Simple Date

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



Code Explanation

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

echo "Today is " . date("Y/m/d");

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

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

Get A Simple Time

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

echo "The time is " . date("h:i:sa");

  • 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

Create Date

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


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 month, a number with leading zeroes. For eg. 08

  • D: Day of 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 year (number - leading zeroes). For eg. 1

  • M: Month of 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 hour. For eg. 24

  • U: Time stamp. 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

❮ PrevNext ❯