13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Getting the Date <strong>and</strong> Time from <strong>PHP</strong><br />

471<br />

Table 21.1<br />

Code<br />

Continued<br />

Description<br />

t Total number of days in the date’s month.The range is from 28 to 31.<br />

T Time zone setting of the server in three-character format—for example, EST.<br />

U Total number of seconds from January 1, 1970, to this time; also known as a Unix<br />

timestamp for this date.<br />

w Day of the week as a single digit.The range is from 0 (Sunday) to 6 (Saturday).<br />

W Week number in the year; ISO-8601 compliant. (This format code was added at<br />

<strong>PHP</strong> 4.1.0.)<br />

y Year in two-digit format—for example, 08.<br />

Y Year in four-digit format—for example, 2008.<br />

z Day of the year as a number.The range is 0 to 365.<br />

Z Offset for the current time zone in seconds.The range is -43200 to 43200.<br />

Dealing with Unix Timestamps<br />

The second parameter to the date() function is a Unix timestamp. In case you are wondering<br />

exactly what this means, most Unix systems store the current time <strong>and</strong> date as a<br />

32-bit integer containing the number of seconds since midnight, January 1, 1970, GMT,<br />

also known as the Unix Epoch.This concept can seem a bit esoteric if you are not familiar<br />

with it, but it’s a st<strong>and</strong>ard <strong>and</strong> integers are easy for computers to deal with.<br />

Unix timestamps are a compact way of storing dates <strong>and</strong> times, but it is worth noting<br />

that they do not suffer from the year 2000 (Y2K) problem that affects some other compact<br />

or abbreviated date formats.They do have similar problems, though, because they<br />

can represent only a limited span of time using a 32-bit integer. If your software needs to<br />

deal with events before 1902 or after 2038, you will be in trouble.<br />

On some systems including Windows, the range is more limited. A timestamp cannot<br />

be negative, so timestamps before 1970 cannot be used.To keep your code portable, you<br />

should bear this fact in mind.<br />

You probably don’t need to worry about your software still being used in 2038.<br />

Timestamps do not have a fixed size; they are tied to the size of a C long, which is at<br />

least 32 bits. If your software still happens to be in use in 2038, it is exceedingly likely<br />

that your system will be using a larger type by that time.<br />

Although this is a st<strong>and</strong>ard Unix convention, this format is still used by date() <strong>and</strong> a<br />

number of other <strong>PHP</strong> functions even if you are running <strong>PHP</strong> under Windows.The only<br />

difference is that, for Windows, the timestamp must be positive.<br />

If you want to convert a date <strong>and</strong> time to a Unix timestamp, you can use the<br />

mktime() function. It has the following prototype:<br />

int mktime ([int hour[, int minute[, int second[, int month[,<br />

int day[, int year [, int is_dst]]]]]]])

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!