Current Location: Home> Latest Articles> getdate() Function: What Does Its Returned Array Contain and How to Use Each Element

getdate() Function: What Does Its Returned Array Contain and How to Use Each Element

gitbox 2025-09-11

In PHP, the getdate() function is a highly practical date and time function that returns detailed information about the current date and time in an array format. Through this array, developers can conveniently access various elements related to the current date and time. This article will provide a detailed explanation of the elements contained in the array returned by getdate() and analyze the purpose and usage of each element.

1. Overview of the getdate() Function

The getdate() function returns detailed information about the current date and time. The returned array includes various time-related data such as year, month, day, hour, minute, second, and more. It not only provides the components of the current system time but also other important information related to the time, such as the day of the week and the timestamp.

2. Structure of the Array Returned by getdate()

When calling the getdate() function, it returns an associative array where each element corresponds to a part of the time. Below is a typical array structure returned by getdate():

<span><span><span class="hljs-title function_ invoke__">Array</span></span><span>
(
    [seconds] =&gt; </span><span><span class="hljs-number">23</span></span><span>
    [minutes] =&gt; </span><span><span class="hljs-number">15</span></span><span>
    [hours] =&gt; </span><span><span class="hljs-number">10</span></span><span>
    [mday] =&gt; </span><span><span class="hljs-number">14</span></span><span>
    [wday] =&gt; </span><span><span class="hljs-number">3</span></span><span>
    [mon] =&gt; </span><span><span class="hljs-number">6</span></span><span>
    [year] =&gt; </span><span><span class="hljs-number">2025</span></span><span>
    [yday] =&gt; </span><span><span class="hljs-number">164</span></span><span>
    [weekday] =&gt; Wednesday
    [month] =&gt; June
    [</span><span><span class="hljs-number">0</span></span><span>] =&gt; </span><span><span class="hljs-number">1686741323</span></span><span>
)
</span></span>

Next, we will explain the meaning and usage of each array element in detail.

3. Explanation of Each Element in the getdate() Array

1. seconds

  • Type: Integer

  • Description: The current second (an integer between 0 and 59).

  • Usage: Can be used to get the current second. For example, if you want to display the current seconds, you can directly use this value.

2. minutes

  • Type: Integer

  • Description: The current minute (an integer between 0 and 59).

  • Usage: Used to get the current minute, commonly applied in time displays and timing functions.

3. hours

  • Type: Integer

  • Description: The current hour (an integer between 0 and 23, in 24-hour format).

  • Usage: Can be used to get the current hour and is often applied for time comparison or formatting.

4. mday

  • Type: Integer

  • Description: The day of the month (an integer between 1 and 31).

  • Usage: Used to get the current day of the month. For example, if today is June 14th, mday will return 14.

5. wday

  • Type: Integer

  • Description: Numeric representation of the day of the week (0 to 6, with 0 for Sunday, 1 for Monday, and so on).

  • Usage: Often used together with weekday to determine the day of the week.

6. mon

  • Type: Integer

  • Description: The current month (an integer between 1 and 12).

  • Usage: Use mon to get the current month, commonly for date comparison or formatting.

7. year

  • Type: Integer

  • Description: The current year (four digits).

  • Usage: Useful for obtaining the current year, such as generating date tags or performing year-based calculations.

8. yday

  • Type: Integer

  • Description: The day of the year (an integer from 0 to 365, with 0 representing January 1 and 365 representing December 31).

  • Usage: Used to calculate the position of a specific day within the year, applicable for date difference calculations.

9. weekday

  • Type: String

  • Description: The English name of the day of the week (e.g., "Sunday", "Monday").

  • Usage: Commonly used to display the day of the week by name, for example, when showing dates, you may want to show "Monday" instead of a number.

10. month

  • Type: String

  • Description: The English name of the current month (e.g., "January", "February").

  • Usage: Used together with mon to convert numeric months to their English names, suitable for formatted date displays.

11. [0] (Unix Timestamp)

  • Type: Integer

  • Description: The Unix timestamp of the current time (seconds since January 1, 1970, 00:00:00 UTC).

  • Usage: Unix timestamps are widely used in time calculations, database storage, and other scenarios.

4. Usage Example

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$date_info</span></span> = </span><span><span class="hljs-title function_ invoke__">getdate</span></span>();
</span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-string">"Current seconds: "</span></span> . </span><span><span class="hljs-variable">$date_info</span></span>[</span><span><span class="hljs-string">&#039;seconds&#039;</span></span>] . </span><span><span class="hljs-string">"\n"</span></span>;
</span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-string">"Current minutes: "</span></span> . </span><span><span class="hljs-variable">$date_info</span></span>[</span><span><span class="hljs-string">&#039;minutes&#039;</span></span>] . </span><span><span class="hljs-string">"\n"</span></span>;
</span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-string">"Current hour: "</span></span> . </span><span><span class="hljs-variable">$date_info</span></span>[</span><span><span class="hljs-string">&#039;hours&#039;</span></span>] . </span><span><span class="hljs-string">"\n"</span></span>;
</span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-string">"Today is: "</span></span> . </span><span><span class="hljs-variable">$date_info</span></span>[</span><span><span class="hljs-string">&#039;weekday&#039;</span></span>] . </span><span><span class="hljs-string">"\n"</span></span>;
</span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-string">"Current month: "</span></span> . </span><span><span class="hljs-variable">$date_info</span></span>[</span><span><span class="hljs-string">&#039;month&#039;</span></span>] . </span><span><span class="hljs-string">"\n"</span></span>;
</span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-string">"Current year: "</span></span> . </span><span><span class="hljs-variable">$date_info</span></span>[</span><span><span class="hljs-string">&#039;year&#039;</span></span>] . </span><span><span class="hljs-string">"\n"</span></span>;
</span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-string">"Today is the "</span></span> . </span><span><span class="hljs-variable">$date_info</span></span>[</span><span><span class="hljs-string">&#039;yday&#039;</span></span>] . </span><span><span class="hljs-string">"th day of the year\n"</span></span>;
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

5. Conclusion

The getdate() function is an extremely convenient tool for obtaining detailed information about the current date and time. The array it returns includes various time elements from seconds, minutes, hours, and days to weekdays and months, helping developers quickly access and process date and time data. In many practical development scenarios, such as date comparisons, event scheduling, and log recording, the getdate() function can be flexibly used to handle time information.