文字

getdate

(PHP 4, PHP 5, PHP 7)

getdate取得日期/时间信息

说明

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

返回一个根据 timestamp 得出的包含有日期信息的关联数组 array 。如果没有给出时间戳则认为是当前本地时间。

参数

timestamp

可选的 timestamp 参数是一个 integer 的 Unix 时间戳,如未指定,参数值默认为当前本地时间。也就是说,其值默认为 time() 的返回值。

返回值

返回一个根据 timestamp 得出的包含有日期信息的关联数组 array 。 返回的关联数组中的键名单元有以下几个:

返回的关联数组中的键名单元
键名 说明 返回值例子
"seconds" 秒的数字表示 059
"minutes" 分钟的数字表示 059
"hours" 小时的数字表示 023
"mday" 月份中第几天的数字表示 131
"wday" 星期中第几天的数字表示 0 (周日) 到 6 (周六)
"mon" 月份的数字表示 112
"year" 4 位数字表示的完整年份 比如: 19992003
"yday" 一年中第几天的数字表示 0365
"weekday" 星期几的完整文本表示 SundaySaturday
"month" 月份的完整文本表示,比如 January 或 March JanuaryDecember
0 自从 Unix 纪元开始至今的秒数,和 time() 的返回值以及用于 date() 的值类似。 系统相关,典型值为从 -21474836482147483647

范例

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() - 格式化一个本地时间/日期
  • idate() - 将本地时间日期格式化为整数
  • localtime() - 取得本地时间
  • time() - 返回当前的 Unix 时间戳
  • setlocale() - 设置地区信息

用户评论:

[#1] eric dot schultz at NOSPAM dot CyVon dot com [2008-12-16 18:33:50]

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.

[#2] timforte at gmail dot com [2008-01-10 08:07:45]

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]

[#3] andre at anlex dot co dot za [2006-12-13 04:38:52]

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(000date("m"$now)  , date("d"$now)-$subdate("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..

[#4] Yura Pylypenko (plyrvt at mail dot ru) [2003-09-15 06:29:38]

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"
?>

上一篇: 下一篇: