Monday, May 9, 2016

WDP - PHP Date & Time

PHP Date and Time

The date() function formats a local date and time, and returns the formatted date string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().

date(format,timestamp);

string date ( string $format [, int $timestamp = time() ] )
The required format parameter of the date() function specifies how to format the date (or time).
Here are some characters that are commonly used for dates:
  • d - Represents the day of the month (01 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)
  • l (lowercase 'L') - Represents the day of the week
Other characters, like"/", ".", or "-" can also be inserted between the characters to add additional formatting.
<?php// set the default timezone to use. Available since PHP 5.1date_default_timezone_set('UTC'. "<br>";

// Prints something like: Mondayecho date("l"
. "<br>";
// Prints something like: Monday 8th of August 2005 03:12:46 PMecho date('l jS \of F Y h:i:s A'
. "<br>";

// Prints: July 1, 2000 is on a Saturdayecho "July 1, 2000 is on a " date("l"mktime(000712000)) 
. "<br>";
/* use the constants in the format parameter */
// prints something like: Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822
. "<br>";
// prints something like: 2000-07-01T00:00:00+00:00echo date(DATE_ATOMmktime(000712000)) 
. "<br>";
?>
Here are some characters that are commonly used for times:
  • h - 12-hour format of an hour with leading zeros (01 to 12)
  • i - Minutes with leading zeros (00 to 59)
  • s - Seconds with leading zeros (00 to 59)
  • a - Lowercase Ante meridiem and Post meridiem (am or pm)
The example below outputs the current time in the specified format:

Example

<?php
echo "The time is " . date("h:i:sa");
?>
Output: The time is 09:54:12pm
Other formats...

No comments:

Post a Comment