PHP Velho Oeste 2024

getdate

(PHP 4, PHP 5, PHP 7)

getdate날짜/시간 정보를 가져온다

설명

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

timestamp의 날짜 정보를 포함하는 연관 배열이나, timestamp가 주어지지 않는다면 현재 로컬 시간를 반환합니다.

인수

timestamp

선택적인 timestamp 인수는 timestamp가 주어지지 않았을 경우, 현재 로컬 시간을 기본값으로 가지는 integer 유닉스 타임스탬프입니다. 즉, 기본값은 time() 값입니다.

반환값

timestamp에 관련된 정보를 가진 연관 array를 반환합니다. 반환되는 연관 배열 원소는 다음과 같습니다:

반환되는 연관 배열의 키 원소들
설명 Example returned values
"seconds" 초의 숫자 표현 0 부터 59
"minutes" 분의 숫자 표현 0 부터 59
"hours" 시간의 숫자 표현 0 부터 23
"mday" 일의 숫자 표현 1 부터 31
"wday" 요일의 숫자 표현 0 (일요일) 부터 6 (토요일)
"mon" 월의 숫자 표현 1 부터 12
"year" 연도의 숫자표현, 4 자리 예시: 1999 또는 2003
"yday" 연도의 일차 0 부터 365
"weekday" 요일, 완전한 문자 Sunday 부터 Saturday
"month" 월, 완전한 문자, January 나 March January 부터 December
0 유닉스 Epoch (January 1 1970 00:00:00 GMT)로부터의 초, time()에 의해 반환되는 값과 유사하고, date()에 의해서 사용됩니다. 시스템에 의존합니다, 일반적으로 -2147483648 부터 2147483647.

예제

Example #1 getdate() 예제

<?php
$today 
getdate(); 
print_r($today);
?>

위 예제의 출력 예시:

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

참고

  • date() - 로컬 날짜/시간을 형식화합니다
  • time() - Return current Unix timestamp
  • setlocale() - Set locale information

add a note add a note

User Contributed Notes 6 notes

up
33
vimal866 at gmail dot com
8 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'];
up
19
Yura Pylypenko (plyrvt at mail dot ru)
20 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:
<?php
echo strtotime ("+1 day"), "\n";
echo
strtotime ("+1 week"), "\n";
echo
strtotime ("+1 week 2 days 4 hours 2 seconds"), "\n";
echo
strtotime ("next Thursday"), "\n";
echo
strtotime ("last Monday"), "\n";
?>
up
-13
andre at anlex dot co dot za
17 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.

Example:
<?php
//-----------------------------
$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];
echo
"$d-$m-$y"; //getdate converted day

?>

Allot less code makes everyone happy..
up
-8
info at ajawbk dot com
5 years ago
another gmgetdate that is a little faster/suscint (no loops).

<?php
function gmgetdate2($ts = null){
       
$k = array('seconds','minutes','hours','mday',
               
'wday','mon','year','yday','weekday','month',0);
        return(
array_combine($k,split(":",
               
gmdate('s:i:G:j:w:n:Y:z:l:F:U',is_null($ts)?time():$ts))));
        }
?>

https://ajawbk.com/
up
-74
timforte at gmail dot com
16 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...

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

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

<?php
function gmgetdate2($ts = null){
       
$k = array('seconds','minutes','hours','mday',
               
'wday','mon','year','yday','weekday','month',0);
        return(
array_combine($k,split(":",
               
gmdate('s:i:G:j:w:n:Y:z:l:F:U',is_null($ts)?time():$ts))));
        }
?>

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