The PHP Getdate Function

The PHP Getdate Function
Working with time is a frequent task for most PHP programs. For example, in a shopping cart program you would want to store the date and time of a purchase made by your customer. Later you will also want to store the date and time the purchase was shipped to the customer. One way to do this is to store each date and time in the format of the Unix timestamp.

Let's take a look at an example. At this moment it is 3:06 am on October 28, 2005 and I just purchased two DVD's from Amazon.com. The timestamp for my order would be 1130486817 and this number is all that will be stored in the database. After it is stored, we can retrieve it for use in displaying an order summery for this purchase. But this number is not very reader-friendly is it? That's when the getdate function comes in handy. We will use the getdate function on the timestamp to get an array that contains all sorts of information about the timestamp such as the month, day, year and time of day. Then we can display any or all of the parts of the array with the echo statement. Here is the code for this function and the resulting $purchase array.

$purchase = getdate(1130486817);

Array (
[seconds] => 57
[minutes] => 6
[hours] => 3
[mday] => 28
[wday] => 5
[mon] => 10
[year] => 2005
[yday] => 300
[weekday] => Friday
[month] => October
[0] => 1130486817 )


So what does all this information in the array mean?

[seconds]
Numeric value in sections for the time of purchase (from 0 to 59)
In our example, it was 57 sections after 3:06 am

[minutes]
Numeric value in minutes for the time of purchase (from 0 to 59)
In our example, it was 6 minutes after 3am

[hours]
Numeric value in hours for the time of purchase (from 0 to 23)
It was 3 am

[mday]
Numeric value for the day of the month for the date of purchase (from 1 to 31)
It was the 28th

[wday]
Numeric value for the day of the week for the date of purchase
(from 0 for Sunday through 6 for Saturday)
It is friday so the numeric value is 5

[mon]
Numeric value for the month for the date of purchase (from 1 through 12)
It is October so the numeric value is 10

[year]
Numeric value for the year for the date of purchase (4 digits)
It is 2005

[yday]
Numeric value for the day of the year for the date of purchase (from 0 to 365)
It is the 300th day of this year

[weekday]
Text value for the day of the week for the date of purchase (Sunday through Saturday)
It is Friday

[month]
Text value for the month for the date of purchase (January through December)
It is October

[0]
The timestamp
1130486817

So now all we need to do to display the date and time of the purchase (October 28, 2005 - 3:06:57) is to use an echo statement.

echo "$purchase[month] $purchase[mday], $purchase[year] - $purchase[hours]:$purchase[minutes]:$purchase[seconds]";





This site needs an editor - click to learn more!



RSS
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map





Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.