w3resource

PHP: localtime() function

Description

The localtime() function is used to get the localtime.

Version:

(PHP 4 and above)

Syntax:

localtime(timestamp, is_associative) 

Parameters:

Name Description Required/ Optional Type
timestamp The timestamp parameter is a Unix timestamp.If no timestamp is specified, it uses the current local time. Optional Integer
is_associative If the argument is set to true the array returned an indexed array otherwise the array returned is an associative  array.
The following list describes the elements of the returned array. tm_sec - seconds
tm_min - minutes
tm_hour - hour
tm_mday - the day of the month.
"tm_mon" - month of the year, starting with 0 for January
"tm_year" - Years since 1900
"tm_wday" - Day of the week
"tm_yday" - Day of the year
"tm_isdst" - Is daylight savings time in effect
Optional Boolean

Return value:

An array.

Value Type: Array.

Example:

<?php
print_r(localtime(time(),true));
echo '<br />';
print_r(localtime());
?>

Sample Output:

Array ( [tm_sec] => 52 [tm_min] => 20 [tm_hour] => 7 [tm_mday] => 19 [tm_mon] => 0 [tm_year] => 111 [tm_wday] => 3 [tm_yday] => 18 [tm_isdst] => 0 )
Array ( [0] => 52 [1] => 20 [2] => 7 [3] => 19 [4] => 0 [5] => 111 [6] => 3 [7] => 18 [8] => 0 ) 

View the example in the browser

See also

PHP Function Reference

Previous: idate
Next: microtime



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