PHP localtime – Get current datetime as an array

The localtime function is used to get the current date and time from the operating system as a C style associative array or list.

Syntax:

array localtime ()
array localtime (integer $Time)
array localtime (integer $Time, boolean $Associative)


Arguments:
$Time
The number of seconds since midnight before January 1, 1970. (UNIX style.)
Default $Time
The default $Time is the current date and time from the operating system.
$Associative
Iff true, the return value will be an associative array
Default $Associative
false

if $Associative is false (or absent), the return value is an indexed array containing:

0 The seconds of the minute (0..59)
1 The minutes of the hour (0..59)
2 The hour of the day (0..23)
3 The day of the month (1..31)
4 The month of the year as a number (1..12)
5 The year (4 digits)
6 The day of the week as a number (0..6)
7 The day of the year (0..365)
8 true iff daylight savings time is in effect

if $Associative is true, the return value is an associative array containing:

tm_sec The seconds of the minute (0..59)
tm_min The minutes of the hour (0..59)
tm_hour The hour of the day (0..23)
tm_mday The day of the month (1..31)
tm_mon The month of the year as a number (1..12)
tm_year The year (4 digits)
tm_wday The day of the week as a number (0..6)
tm_yday The day of the year (0..365)
tm_isdst true iff daylight savings time is in effect

Example code:

<?php
 
$Now = localtime();
echo "As a list (indexed array):\n";
foreach ($Now as $Key => $Value) {
  echo "$Key => $Value\n";
}
 
$Now = localtime(time(), true);
echo "As an associative array:\n";
foreach ($Now as $Key => $Value) {
  echo "$Key => $Value\n";
}
 
?>

Tags:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.