PHP时间怎么用?在线等待(100分)

beecat 2003-12-12 04:11:33
本人不是学PHP的,但是目前有一个项目要用到一些代码,因此上来求救

1、如何在页面上获得服务器端的时间,客户端的时间呢?返回什么类型的变量?
2、如果将获得的时间转换为20030101123030这样的格式,就是2003年1月1号12点30分30秒这样的字符串。
3、两个上述的包含时间信息的字符串,如果比较两个时间之间相差多少秒?

急,在线等待,谢谢。
...全文
143 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shadowy 2003-12-13
  • 打赏
  • 举报
回复
建设下载一个php中文手册。
很多地方有的下。
Cain 2003-12-13
  • 打赏
  • 举报
回复
1. 用time,date,getdate
2. date("YmdHis")
3. 减一下
wangdongzjk 2003-12-12
  • 打赏
  • 举报
回复
msdn的jscript帮助:
Description
Enables basic storage and retrieval of dates and times.
Syntax
var newDateObj = new Date()
var newDateObj = new Date(dateVal)
var newDateObj = new Date(year, month, date[, hours[, minutes[, seconds[,ms]]]])
The Date object constructor syntax has these parts:

Part Description
dateVal If a numeric value, dateVal represents the number of milliseconds in Universal Coordinated Time between the specified date and midnight January 1, 1970. If a string, dateVal is parsed according to the rules in the parse method. The dateVal argument can also be a VT_DATE value as returned from some ActiveX® objects.
year Required. The full year, for example, 1976 (and not 76).
month Required. The month as an integer between 0 and 11 (January to December).
date Required. The date as an integer between 1 and 31.
hours Optional. Must be supplied if minutes is supplied. An integer from 0 to 23 (midnight to 11pm) that specifies the hour.
minutes Optional. Must be supplied if seconds is supplied. An integer from 0 to 59 that specifies the minutes.
seconds Optional. Must be supplied if milliseconds is supplied. An integer from 0 to 59 that specifies the seconds.
ms Optional. An integer from 0 to 999 that specifies the milliseconds.


Remarks
A Date object contains a number representing a particular instant in time to within a millisecond. If the value of an argument is greater than its range or is a negative number, other stored values are modified accordingly. For example, if you specify 150 seconds, JScript redefines that number as two minutes and 30 seconds.
If the number is NaN, that indicates that the object does not represent a specific instant of time. If you pass no parameters to the Date object, it is initialized to the current time (UTC). A value must be given to the object before you can use it.

The range of dates that can be represented in a Date object is approximately 285,616 years on either side of January 1, 1970.

The Date object has two static methods that are called without creating a Date object. They are parse and UTC.

getDate Method
getDay Method
getFullYear Method
getHours Method
getMilliseconds Method
getMinutes Method
getMonth Method
getSeconds Method
getTime Method
getTimezoneOffset Method
getUTCDate Method
getUTCDay Method
getUTCFullYear Method
getUTCHours Method
getUTCMilliSeconds Method
getUTCMinutes Method
getUTCMonth Method
getUTCSeconds Method
getVarDate Method
getYear Method
setDate Method
setFullYear Method
setHours Method
setMilliSeconds Method
setMinutes Method
setMonth Method
setSeconds Method
setTime Method
setUTCDate Method
setUTCFullYear Method
setUTCHours Method
setUTCMilliseconds Method
setUTCMinutes Method
setUTCMonth Method
setUTCSeconds Method
setYear Method
toGMTString Method
toLocaleString Method
toUTCString Method
toString Method
valueOf Method


wangdongzjk 2003-12-12
  • 打赏
  • 举报
回复
用php本身无法获得客户端的系统时间,要想获得客户端的系统时间,我觉得得用javascript来实现:
function dateSelector() //构造dateSelector对象
{
var myDate=new Date();
this.year=myDate.getFullYear(); //定义year属性,年份,默认值为当前系统年份。
this.month=myDate.getMonth()+1; //定义month属性,月份,默认值为当前系统月份。
this.date=myDate.getDate(); //定义date属性,日,默认值为当前系统的日。
}

不知道这个函数有没有用:)
hodat 2003-12-12
  • 打赏
  • 举报
回复
1. 用time()或date()获得服务器时间
我觉得没有办法获得客户机的时间,如果一定需要的话,用javascript获得然后传值
PHP本身是弱类型语言

2. 用date("YmdHis",time());获得

3. 靠~~
用减啊~~
蛋蛋爹 2003-12-12
  • 打赏
  • 举报
回复
date
(PHP 3, PHP 4 )

date -- 格式化一个本地时间/日期
说明
string date ( string format [, int timestamp])


返回将整数 timestamp 按照给定的格式字串而产生的字符串。如果没有给出时间戳则使用本地当前时间。

注: 有效的时间戳典型范围是格林威治时间 1901 年 12 月 13 日 20:45:54 到 2038 年 1 月 19 日 03:14:07。(此范围符合 32 位有符号整数的最小值和最大值)。在 Windows 系统中此范围限制为从 1970 年 1 月 1 日到 2038 年 1 月 19 日。

要将字符串表达的时间转换成时间戳,应该使用 strtotime()。此外一些数据库有一些函数将其时间格式转换成时间戳(例如 MySQL 的 UNIX_TIMESTAMP 函数)。


格式字串可以识别以下字符:


a - "am" 或 "pm"

A - "AM" 或 "PM"

B - Swatch Internet Time(【译者注】参见 http://swatch.com/alu_beat/fs_itime.html)

d - 月份中的第几天,有前导零的 2 位数字,例如 "01" to "31"

D - 星期中的第几天,文本表示,3 个字母,例如 "Fri"

F - 月份,完整的文本格式,例如 "January"

g - 小时,12 小时格式,没有前导零,例如 "1" 到 "12"

G - 小时,24 小时格式,没有前导零,例如 "0" 到 "23"

h - 小时,12 小时格式,例如 "01" 到 "12"

H - 小时,24 小时格式,例如 "00" 到 "23"

i - 分钟,例如 "00" 到 "59"

I(“i”的大写的字母)- 如果是夏令时则为 "1",否则为 "0"

j - 月份中的第几天,没有前导零,例如 "1" 到 "31"

l(“L”的小写字母)- 星期中的第几天,完整的文本格式,例如 "Friday"

L - 布尔值表示是否为闰年,例如 "0" 或者 "1"

m - 月份,例如 "01" to "12"

M - 月份,文本表示,3 个字母,例如 "Jan"

n - 月份,没有前导零,例如 "1" 到 "12"

O - 与格林威治时间相差的小时数,例如 "+0200"

r - RFC 822 格式的日期,例如 "Thu, 21 Dec 2000 16:01:07 +0200"(PHP 4.0.4新增)

s - 秒数,例如 "00" 到 "59"

S - 每月天数后面的英文后缀,2 个字符,例如 "st","nd","rd" 或者 "th"

t - 给定月份所应有的天数,例如 "28" 到 "31"

T - 本机所在的时区,例如 "EST" 或 "MDT"(【译者注】在 Windows 下为完整文本格式,例如“Eastern Standard Time”,中文版会显示“中国标准时间”。)

U - 从 Unix 纪元(January 1 1970 00:00:00 GMT)开始至今的秒数

w - 星期中的第几天,数字表示,例如 "0"(星期天)到 "6" (Saturday)

W - ISO-8601 格式年份中的第几周,每周从星期一开始(PHP 4.1.0 新加的)

Y - 年份,4 位数字,例如 "1999"

y - 年费,2 位数字,例如 "99"

z - 年份中的第几天,例如 "0" 到 "365"

Z - 时差偏移量的秒数(例如 "-43200" 到 "43200")。UTC 西边的时区偏移量总是负的,UTC 东边的时区偏移量总是正的。

格式字串中不能被识别的字符将原样显示。“Z”格式在使用 gmdate() 时总是返回“0”。

例子 1. date() 例子

echo date ("l dS of F Y h:i:s A");
echo "July 1, 2000 is on a " . date ("l", mktime(0,0,0,7,1,2000));




在格式字串中的字符前加上反斜线来转义可以避免它被按照上表解释。如果加上反斜线后的字符本身就是一个特殊序列,那还要转义反斜线。 例子 2. 在 date() 中转义字符

echo date("l \\t\h\e jS"); // 显示类似于:Saturday the 8th




可以把 date() 和 mktime() 结合使用来得到未来或过去的日期。 例子 3. date() 和 mktime() 例子

$tomorrow = mktime (0,0,0,date("m") ,date("d")+1,date("Y"));
$lastmonth = mktime (0,0,0,date("m")-1,date("d"), date("Y"));
$nextyear = mktime (0,0,0,date("m"), date("d"), date("Y")+1);




注: 由于夏令时的缘故,这种方法比简单地在时间戳上加减一天或者一个月的秒数更可靠。


一些使用 date() 格式化日期的例子。注意要转义所有其它的字符,因为目前有特殊含义的字符会产生不需要的结果,而其余字符在 PHP 将来的版本中可能会被用上。当转义时,注意用单引号以避免类似 \n 的字符变成了换行符。 例子 4. date() Formatting

/* Today is March 10th, 2001, 5:16:18 pm */
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day z '); // 05-16-17, 10-03-01, 1631 1618 6 Fripm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // It is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 15:16:08 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:17 m is month
$today = date("H:i:s"); // 17:16:



21,882

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧