(PHP 4, PHP 5, PHP 7)

getdateObtener información de la fecha/hora


array getdate ([ int $timestamp = time() ] )

Devuelve un array asociativo que contiene la información de la fecha de timestamp, o el momento local actual si no se da timestamp.



El parámetro opcional timestamp es una marca temporal de Unix de tipo integer que por defecto es la hora local si no se proporciona ningún valor a timestamp. En otras palabras, es de forma predeterminada el valor de la función time().

Valores devueltos

Devuelve un array asociativo con información relacionada con la marca de tiempo timestamp. Los elementos devueltos de la matriz asociativa son los siguientes:

Las claves de la matriz asociativa devuelta
Clave Descripción Ejemplo de valores devueltos
"seconds" Representacion numérica de los segundos 0 a 59
"minutes" Representacion numérica de los minutos 0 a 59
"hours" Representacion numérica de las horas 0 a 23
"mday" Representacion numérica del día del mes 1 a 31
"wday" Representacion numérica del día de la semana 0 (para Domingo) hasta 6 (para Sábado)
"mon" Representacion numérica de un mes 1 hasta 12
"year" Una representacion numérica completa de una año, 4 dígitos Ejemplos: 1999 o 2003
"yday" Representacion numérica del día del año 0 hasta 365
"weekday" Una representación textual completa del día de la semana Sunday hasta Saturday
"month" Una representación textual completa de un mes, como January o March January hasta December
0 Los segundos desde la Época Unix, similar a los valores devueltos por time() y usados por date(). Dependiente del Sistema, típicamente -2147483648 hasta 2147483647.


Ejemplo #1 Ejemplo de getdate()


El resultado del ejemplo sería algo similar a:

    [seconds] => 40
    [minutes] => 58
    [hours]   => 21
    [mday]    => 17
    [wday]    => 2
    [mon]     => 6
    [year]    => 2003
    [yday]    => 167
    [weekday] => Tuesday
    [month]   => June
    [0]       => 1055901520

Ver también

  • date() - Dar formato a la fecha/hora local
  • idate() - Formatea una fecha/hora local como un entero
  • localtime() - Obtiene fecha y hora local
  • time() - Devuelve la fecha Unix actual
  • setlocale() - Establecer la información del localismo

add a note add a note

User Contributed Notes 5 notes

vimal866 at gmail dot com
2 years ago
Andre's code will throw an error. for the following line
     $d = $todayh[mday];
     $m = $todayh[mon];
     $y = $todayh[year];

"Notice : Undefined constant mday ,mon,year"

As is, it was looking for constants called mday, mon, year etc. When it doesn't find such a constant, PHP interprets it as a string.

like any other request it should be wrapped in quotes like this

     $d = $todayh['mday'];
     $m = $todayh['mon'];
     $y = $todayh['year'];
Yura Pylypenko (plyrvt at mail dot ru)
14 years ago
In addition to canby23 at ms19 post:
It's a very bad idea to consider day having 24 hours (86400 secs), because some days have 23, some - 25 hours due to daylight saving changes. Using of mkdate() and strtotime() is always preferred. strtotime() also has a very nice behaviour of datetime manipulations:
echo strtotime ("+1 day"), "\n";
strtotime ("+1 week"), "\n";
strtotime ("+1 week 2 days 4 hours 2 seconds"), "\n";
strtotime ("next Thursday"), "\n";
strtotime ("last Monday"), "\n";
andre at anlex dot co dot za
11 years ago
I thought best to show a posseble way to go about bypassing the end month issue where the first day in a new month will have the monday of the week that it falls in - in the old month. Use the numbering of days as the constant and work you way from there.

$now = time();
$num = date("w");
if (
$num == 0)
$sub = 6; }
else {
$sub = ($num-1); }
$WeekMon  = mktime(0, 0, 0, date("m", $now)  , date("d", $now)-$sub, date("Y", $now));    //monday week begin calculation
$todayh = getdate($WeekMon); //monday week begin reconvert

$d = $todayh[mday];
$m = $todayh[mon];
$y = $todayh[year];
"$d-$m-$y"; //getdate converted day


Allot less code makes everyone happy..
timforte at gmail dot com
9 years ago
It's worth noting that this is local time, not UTC/GMT - gmgetdate doesn't exist :(.

The most logical way to handle date arithmetic without hitting DST problems is to work in UTC...

function add_days($my_date,$numdays) {
$date_t = strtotime($my_date.' UTC');
gmdate('Y-m-d',$date_t + ($numdays*86400));

[it's even faster if you use gmmktime instead of strtotime]
eric dot schultz at NOSPAM dot CyVon dot com
9 years ago
Here is another gmgetdate that is a little faster/suscint (no loops).

function gmgetdate2($ts = null){
$k = array('seconds','minutes','hours','mday',

It also returns the values in the same order as getdate.
To Top