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.
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.
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] => </span><span><span class="hljs-number">23</span></span><span>
[minutes] => </span><span><span class="hljs-number">15</span></span><span>
[hours] => </span><span><span class="hljs-number">10</span></span><span>
[mday] => </span><span><span class="hljs-number">14</span></span><span>
[wday] => </span><span><span class="hljs-number">3</span></span><span>
[mon] => </span><span><span class="hljs-number">6</span></span><span>
[year] => </span><span><span class="hljs-number">2025</span></span><span>
[yday] => </span><span><span class="hljs-number">164</span></span><span>
[weekday] => Wednesday
[month] => June
[</span><span><span class="hljs-number">0</span></span><span>] => </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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
<span><span><span class="hljs-meta"><?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">'seconds'</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">'minutes'</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">'hours'</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">'weekday'</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">'month'</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">'year'</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">'yday'</span></span>] . </span><span><span class="hljs-string">"th day of the year\n"</span></span>;
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
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.