PHP Tutorial

PHP Examples

PHP Practice

PHP getdate() Function (With Examples)

In this tutorial, we will learn about the PHP getdate() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP getdate() function

The getdate() function is used to get the local date/time (or it is also used to get the date/time based on the given timestamp.

Syntax

The syntax of the getdate() function:

getdate(timestamp);

Parameters

The parameters of the getdate() function:

  • timestamp – It is an optional parameter which specifies the timestamp (which is based on an integer UNIX timestamp) – if we do not pass the timestamp the function returns the local date/time.

Timestamps

The following are the timestamps,

  • seconds - it is used to get the time in seconds
  • minutes - it is used to get the time in minutes
  • hours - it is used to get the time in hours
  • mday - it is used to get the day of the month
  • wday - it is used to get the day of the week
  • mon - it is used to get the month
  • year - it is used to get the year
  • yday - it is used to get the day of the year
  • weekday - it is used to get the name of the week
  • month - it is used to get the name of the month
  • 0 - it is used to get the seconds since Unix Epoch

Return Value

It returns an array of the date/time information with the timestamp

PHP getdate() Function Example

PHP code to demonstrate the example of getdate() function.

<?php
//getting the complete date/time 
//i.e. local date/time
$result = getdate();
echo "getdate() result...\n";
print_r($result);

//extracting the indivisual values 
//based on the timestamps
echo "seconds:  $result[seconds]\n";
echo "minutes:  $result[minutes]\n";
echo "hours:    $result[hours]\n";
echo "mday:     $result[mday]\n";
echo "wday:     $result[wday]\n";
echo "mon:      $result[mon]\n";
echo "year:     $result[year]\n";
echo "yday:     $result[yday]\n";
echo "weekday:  $result[weekday]\n";
echo "month:    $result[month]\n";
echo "0:        $result[0]\n";
?>

Output

The output of the above example is:

getdate() result...
Array
(
    [seconds] => 28
    [minutes] => 56
    [hours] => 12
    [mday] => 13
    [wday] => 2
    [mon] => 8
    [year] => 2019
    [yday] => 224
    [weekday] => Tuesday
    [month] => August
    [0] => 1565700988
)
seconds:  28
minutes:  56
hours:    12
mday:     13
wday:     2
mon:      8
year:     2019
yday:     224
weekday:  Tuesday
month:    August
0:        1565700988

Reference: PHP getdate() function

To understand the above example, you should have the basic knowledge of the following PHP topics:



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.