Date and Time in PHP

Date and Time in PHP

In this article i want to give some basic details about date and time in PHP for beginners. In PHP we can display date and time according to our requirement by customizing it. For that PHP includes lots of date and time formats.

DEMO
Find below the demo of date function in PHP

echo date('d-m-Y');
//this will print date like " 20-07-2011 "

DAYS


  • d – Day of the month 2 digits (01-31)
  • j – Day of the month (1-31)
  • D – 3 letter day (Mon – Sun)
  • l – Full name of day (Monday – Sunday)
  • N – 1=Monday, 2=Tuesday, etc (1-7)
  • S – Suffix for date (st, nd, rd)
  • w – 0=Sunday, 1=Monday (0-6)
  • z – Day of the year (1=365)

WEEK


  • W – Week of the year (1-52)

MONTH


  • F – Full name of month (January – December)
  • m – 2 digit month number (01-12)
  • n – Month number (1-12)
  • M – 3 letter month (Jan – Dec)
  • t – Days in the month (28-31)

YEAR


  • L – Leap year (0 no, 1 yes)
  • o – ISO-8601 year number (Ex. 1979, 2006)
  • Y – Four digit year (Ex. 1979, 2006)
  • y – Two digit year (Ex. 79, 06)

TIME


  • a – am or pm
  • A – AM or PM
  • B – Swatch Internet time (000 – 999)
  • g – 12 hour (1-12)
  • G – 24 hour c (0-23)
  • h – 2 Digit 12 hour (01-12)
  • H – 2 Digit 24 hour (00-23)
  • i – 2 Digit minutes (00-59)
  • s 0 2 Digit seconds (00-59)

OTHER


  • e – Timezone (Ex: GMT, CST)
  • I – Daylight savings (1=yes, 0=no)
  • O – Offset GMT (Ex: 0200)
  • Z – Offset in seconds (-43200 – 43200)
  • r – Full RFC 2822 formatted date

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>