You want to know what the time or date is.
Use date() for a formatted time string, as in Example 3-2.
Example 3-2. Finding the current date and time
1 |
print date('r'); |
It obviously depends on the time and date the code is run, but Example 3-2 prints something like:
1 |
Fri, 01 Feb 2013 14:23:33 -0500 |
Or, use a DateTime object. Its format() method works just like the date() function:
1 2 |
$when = new DateTime(); print $when->format('r'); |
Use getdate() or localtime() if you want time parts. Example 3-3 shows how these functions work.
Example 3-3. Finding time parts
1 2 3 4 |
$now_1 = getdate(); $now_2 = localtime(); print "{$now_1['hours']}:{$now_1['minutes']}:{$now_1['seconds']}\n"; print "$now_2[2]:$now_2[1]:$now_2[0]"; |
Example 3-3 prints:
1 2 |
18:23:45 18:23:45 |
The function date() (and the DateTime object) can produce a variety of formatted time and date strings.
Both localtime() and getdate(), on the other hand, return arrays whose elements are the different pieces of the specified date and time.
The associative array getdate() returns the key/value pairs listed in Table 3-1.
Table 3-1. Return array from getdate()
Key | Value |
seconds
minutes hours mday wday mon year yday weekday month 0 |
Seconds
Minutes Hours Day of the month Day of the week, numeric (Sunday is 0, Saturday is 6) Month, numeric Year, numeric (4 digits) Day of the year, numeric (e.g., 299) Day of the week, textual, full (e.g., “Friday”) Month, textual, full (e.g., “January”) Seconds since epoch (what time() returns) |
Example 3-4 shows how to use getdate() to print out the month, day, and year.
Example 3-4. Finding the month, day, and year
1 2 |
$a = getdate(); printf('%s %d, %d',$a['month'],$a['mday'],$a['year']); |
Example 3-4 prints:
1 |
February 4, 2013 |
Pass getdate() an epoch timestamp as an argument to make the returned array the appropriate values for local time at that timestamp.
The month, day, and year at epoch timestamp 163727100 is shown in Example 3-5.
Example 3-5. getdate() with a specific timestamp
1 2 |
$a = getdate(163727100); printf('%s %d, %d',$a['month'],$a['mday'],$a['year']); |
Example 3-5 prints:
1 |
March 10, 1975 |
The function localtime() also returns an array of time and date parts. It also takes anepoch timestamp as an optional first argument, as well as a boolean as an optional second argument.
If that second argument is true, localtime() returns an associative array instead of a numerically indexed array.
The keys of that array are the same as the members of the tm_struct structure that the C function localtime() returns, as shown in Table 3-2.
Table 3-2. Return array from localtime()
Numeric position | Key | Value |
0
1 2 3 4 5 6 7 8 |
tm_sec
tm_min tm_hour tm_mday tm_mon tm_year tm_wday tm_yday tm_isdst |
Second
Minutes Hour Day of the month Month of the year (January is 0) Years since 1900 Day of the week (Sunday is 0) Day of the year Is daylight saving time in effect? |
Example 3-6 shows how to use localtime() to print out today’s date in month/day/ year format.
Example 3-6. Using localtime()
1 2 3 4 |
$a = localtime(); $a[4] += 1; $a[5] += 1900; print "$a[4]/$a[3]/$a[5]"; |
Example 3-6 prints:
1 |
2/4/2013 |
Similarly, the year is incremented by 1900 because localtime() starts counting years with 0 for 1900.
The functions getdate() and localtime() both use the same internal implementation to generate the returned date and time parts.
They differ only in the format of the returned arrays and in some of the information they return. (For example, local time() includes whether DST is in effect at the specified time.)
The time zone that getdate() and localtime() use for their calculations is the currently active one, as set by the date.timezone configuration variable or a call to date_de fault_timezone_set().