WarDrome Blog

PHP Guide: Handling date fields

Hi all, after a long period of inactivity, I propose a new tutorial on handling dates in PHP.

All PHP functions that handle dates are based on a given in particular: the TIMESTAMP. The TIMESTAMP is nothing more than a whole number that has been used for a long time on UNIX systems and represents the number of seconds elapsed since 1st January 1970.

The functions available to us are the following:

time()

The time () function returns the current timestamp, the time elapsed since 1 January 1970 to now.

date($format [$timestamp])

The function uses the date stamp provided (or the current one if none is specified) to transform it into a format specified in the first parameter.
Below is a list of all possible formats:

// PERFORMANCE DAYS

echo date('d') // will return the day of the month as a number with a leading 0 (in case of months less than 10).
echo date('D') // will return the day of the month in 3 letter (Mon, Tue, etc.)
echo date('j') // will return the day of the month as a number, but without the initial 0
echo date('l') // will return the day of the month, expressed in letters, will display the full name of the day (Monday, Tuesday, Wednesday etc. ..)
echo date('N') // will return the date expressed in ISO-8601, ie 1 for Monday to 7 for Sunday.
echo date('S') // will return the day, expressed in English with the short form. That is, (st, nd, etc.)
echo date('w') // Numeric representation of the day of the week (0 for Sunday, 6 to Sunday)
echo date('z') // The day of the year (from 0 to 365)

// REPRESENTATION WEEKS
echo date('W') / / Representation of the week in the year (example: 42 is the 42nd week of the year)

/ / REPRESENTATION MONTHS
echo date('F'); // Full text representing the name of the month. (Example: January, March, etc.)
echo date('m') // Month number with leading 0
echo date('M') // Number of months with 3 letters (Jan, Mar, etc.)
echo date('n') // Month number without a leading 0
echo date('t') // Number of days in the month (eg February 28, Nov. 30)

/ / Represent years
echo date('L') // Returns 1 if the year is a leap year, 0 otherwise
echo date('o') // ISO-8601 representation of a year in letters. And 'the equivalent of' Y 'but if the ISO week number of the' W 'is part of a year or earlier, it is used.
echo date('Y') // Representation of the year in 4-digit numeric format (eg: 1999)
echo date('y') // Numeric representation of the year in numeric format, 2 digits (eg: 99)

/ / TIME REPRESENTATION
echo date('a') // If the time is or postmeridiano morning session (am or pm [lower case])
echo date('A') // If the time is or postmeridiano morning session (AM or PM [upper])
echo date('B') // Check the Internet (from 000 to 999)
echo date('g') // Hours in 12 hour format without leading zeros (0 to 12)
echo date('G') // Time in 24 hour format without leading zeros (0 to 24)
echo date('h') // Hours in 12 hour format with leading zero (0 to 12)
echo date('H') // Time in 24 hour format with leading zero (0 to 24)
echo date('i') // Minutes with leading zero (0 to 59)
echo date('s') // Seconds with leading zero (0 to 59)
echo date('u') // Microseconds (example: 654321)

// TIMEZONE REPRESENTATION
echo date('e') // text representation of timezone (eg UTC, Atlantic, Europe / Rome)
echo date('I') // Returns 1 if the office is now 0 otherwise
echo date('O') // Offset from Greenwich Mean Time (GMT) in hours (example: +0200)
echo date('P') // Offset from Greenwich Mean Time (GMT) in hours separated by a colon (eg: +02:00)
echo date('T') // timezone abbreviation (example: EST, MDT)
echo date('Z') // Time zone offset in seconds (the part west of GMT is always negative, the positive part of the east)

// DATE COMPLETE REPRESENTATION
echo date('c') // Date ISO-8601 full-size (example: 2004-02-12T15: 19:21 +00:00)
echo date('r') // Date format complete RFC 2822 (example: Thu, 21 Dec 2000 16:01:07 +0200)
echo date('U') // Seconds since the Unix (January 1, 1970 00:00:00 GMT)

The formats that can be used are many but most useful are those who will represent year, month, day, and the full hour.

In conclusion, so if you want to print the current date system just do:

echo date('Y-m-d H:i:s') // It will print 2011-10-17 22:49:00 (current post time)

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Optionally add an image (JPEG only)

Cookies help us deliver our services. By using our services, you agree to our use of cookies. More Info | Close