php+mkdate,getdate()

getdate()

(PHP 4, PHP 5, PHP 7)

取得日期/时间信息

说明getdate([int$timestamp= time()]):array

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

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

返回值

返回一个根据$timestamp得出的包含有日期信息的关联数组array。返回的关联数组中的键名单元有以下几个:返回的关联数组中的键名单元键名说明返回值例子"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 或 MarchJanuary到December

0自从 Unix 纪元开始至今的秒数,和time()的返回值以及用于date()的值类似。系统相关,典型值为从-2147483648到2147483647。

范例

Example #1getdate()例子<?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

)

参见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'];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";

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

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

//-----------------------------

$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..another gmgetdate that is a little faster/suscint (no loops).

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/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');

return gmdate('Y-m-d',$date_t + ($numdays*86400));

}

?>

[it's even faster if you use gmmktime instead of strtotime]Here is another gmgetdate that is a little faster/suscint (no loops).

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值