Page 132 - Open Soource Technologies 304.indd
P. 132
Unit 8: Working with Strings, Date and Time
[dsttime] => 0 Notes
)
Localtime() and getdate() both return an array. The elements contain information belonging to
the (optional) timestamp passed to the function. The returned arrays are not exactly the same.
Table shows what the elements in the arrays mean.
Table 1: Elements in Arrays Returned by localtime( ) and getdate( )
Meaning Index (localtime()) Index (localtime()) Index (localtime())
Hours tm_hour hours
Day of month tm_mday mday
Month tm_mon mon For localtime: January=0;
for getdate:January=1
Year tm_year Year
Day of week tm_wday wday With 0 being Sunday
and 6 being Saturday
Day of year tm_yday yday With 0 being January
1st and 366 being
December 32nd
DST in effect tm_isdst Set to true if Daylight
Savings Time is in effect
Textual day of weekday English name of week
the weekday
Textual month month English name of the
month
Timestamp Number of seconds since
01-01-1970
The tm_isdst element of localtime() is especially interesting. It’s the only way in PHP to see
whether the server is in DST. Also, note that the month number in the return array of localtime()
starts with 0, not with 1, which makes December month 11. The first parameter for both functions
is a time stamp, allowing the functions to return date information based on the time you pass
them, rather than just on the current time. localtime() normally returns an array with numerical
indices, rather than the indices as described in the previous table. To signal the function to return
an associative array, you need to pass true as the second parameter. If you want to return this
associative array with information about the current time, you need to pass the time() function
as first parameter:
<?php
print_r(localtime(time(), true));
?>
LOVELY PROFESSIONAL UNIVERSITY 127