w3resource

PHP: getdate() function

Description

The getdate() function accepts a timestamp and returns an associative array that contains the date and time information for a Unix timestamp.

Version:

(PHP 4 and above)

Syntax:

getdate(timestamp)

Parameters:

Name Description Required /
Optional
Type
timestamp Unix timestamp.
Default value : current local time.
Required String

Return value:

An associative array. Details are as follows:

Key Description Example returned values
seconds Numeric value of seconds. 0 to 59
minutes Numeric value of minutes. 0 to 59
hours Numeric value of hours. 0 to 23
mday Numeric value of the day of the month. 1 to 31
wday Numeric value of the day of the week. 0(for Sunday) through 6 (for Saturday)
mon Numeric value of a month. 1 through 12
year Four-digit numeric value of a year. Examples: 1999 or 2003
yday Numeric value of the day of the year. 0 through 365
weekday Full name of the day of the week. Sunday through Saturday
month Full name of a month. January through December
0 Seconds since the Unix Epoch, similar to the values returned by time() and used by date(). System Dependent, typically -2147483648 through 2147483647

Value Type: Array

Example:

<?php
$tod = getdate(date("U"));
print_r($tod);
?> 

View the example in the browser

See also

PHP Function Reference

Previous: date
Next: gettimeofday



PHP: Tips of the Day

Getting all defined constants

To get all defined constants including those created by PHP use the get_defined_constants function:

Example:

<?php
$constants = get_defined_constants();
var_dump($constants); // pretty large list

Output:

array(2250) {
  ["E_ERROR"]=>
  int(1)
  ["E_RECOVERABLE_ERROR"]=>
  int(4096)
  ["E_WARNING"]=>
  .....
  .....
  resource(1) of type (stream)
  ["STDOUT"]=>
  resource(2) of type (stream)
  ["STDERR"]=>
  resource(3) of type (stream)
}

To get only those constants that were defined by your app call the function at the beginning and at the end of your script (normally after the bootstrap process):

<?php
$constants = get_defined_constants();
define("HELLO", "hello");
define("WORLD", "world");
$new_constants = get_defined_constants();
$myconstants = array_diff_assoc($new_constants, $constants);
var_export($myconstants);

Output:

array (
  'HELLO' => 'hello',
  'WORLD' => 'world',
)

It's sometimes useful for debugging