13.09.2016 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

473<br />

Using the getdate() Function<br />

Another date-determining function you might find useful is getdate().This function<br />

has the following prototype:<br />

array getdate ([int timestamp])<br />

It takes an optional timestamp as a parameter <strong>and</strong> returns an array representing the parts<br />

of that date <strong>and</strong> time, as shown in Table 21.2.<br />

Table 21.2<br />

Key<br />

Array Key-Value Pairs from getdate() Function<br />

Value<br />

seconds Seconds, numeric<br />

minutes Minutes, numeric<br />

hours Hours, numeric<br />

mday Day of the month, numeric<br />

wday Day of the week, numeric<br />

mon Month, numeric<br />

year Year, numeric<br />

yday Day of the year, numeric<br />

weekday Day of the week, full-text format<br />

month Month, full-text format<br />

0 Timestamp, numeric<br />

After you have these parts in an array, you can easily process them into any required<br />

format.The 0 element in the array (the timestamp) might seem useless, but if you call<br />

getdate() without a parameter, it will give you the current timestamp.<br />

Using the getdate() function, the code<br />

<br />

produces something similar to the following output:<br />

Array (<br />

[seconds] => 45<br />

[minutes] => 6<br />

[hours] => 20<br />

[mday] => 14<br />

[wday] => 3<br />

[mon] => 3<br />

[year] => 2007<br />

[yday] => 72<br />

[weekday] => Wednesday<br />

[month] => March<br />

[0] => 1173917205<br />

)

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

Saved successfully!

Ooh no, something went wrong!