热烈祝贺台州朗动科技的站长论坛隆重上线!(2012-05-28)    热烈庆祝伟大的祖国60周年生日 点击进来我们一起为她祝福吧(2009-09-26)    站长论坛禁止发布广告,一经发现立即删除。谢谢各位合作!.(2009-08-08)    热烈祝贺台州网址导航全面升级,全新版本上线!希望各位一如既往地支持台州网址导航的发展.(2009-03-28)    台州站长论坛恭祝各位新年快乐,牛年行大运!(2009-01-24)    台州Link正式更名为台州网址导航,专业做以台州网址为主的网址导航!(2008-05-23)    热烈祝贺台州Link资讯改名为中国站长资讯!希望在以后日子里得到大家的大力支持和帮助!(2008-04-10)    热烈祝贺台州Link论坛改名为台州站长论坛!希望大家继续支持和鼓励!(2008-04-10)    台州站长论坛原[社会琐碎]版块更名为[生活百科]版块!(2007-09-05)    特此通知:新台州站长论坛的数据信息全部升级成功!">特此通知:新台州站长论坛的数据信息全部升级成功!(2007-09-01)    台州站长论坛对未通过验证的会员进行合理的清除,请您谅解(2007-08-30)    台州网址导航|上网导航诚邀世界各地的网站友情链接和友谊联盟,共同引领网站导航、前进!(2007-08-30)    禁止发广告之类的帖,已发现立即删除!(2007-08-30)    希望各位上传与下载有用资源和最新信息(2007-08-30)    热烈祝贺台州站长论坛全面升级成功,全新上线!(2007-08-30)    
便民网址导航,轻松网上冲浪。
台州维博网络专业开发网站门户平台系统
您当前的位置: 首页 » PHP/Perl编程 » php日期与时间函式库:time()函数

php日期与时间函式库:time()函数

论坛链接
  • php日期与时间函式库:time()函数
  • 发布时间:2007-10-04 17:30:34    浏览数:5729    发布者:tznktg    设置字体【   
我今天看了phpwind和discuz的论坛的数据库,发现数据表中时间数据以10位数字存进去的
又问了phper,知道用的[日期与时间函式库:time()函数].

time
(PHP 3, PHP 4, PHP 5)

time -- 返回当前的 Unix 时间戳
说明
int time ( void )

返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。

例子 1. time() 例子

[color=Green]<?php
$nextWeek = time() + (7 * 24 * 60 * 60);
// 7 days; 24 hours; 60 mins; 60secs
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
?>

上例的输出类似于:

Now: 2005-03-30
Next Week: 2005-04-07




参见 date() 和 microtime()。



add a note User Contributed Notes

send at mail dot 2aj dot net
09-Jun-2006 02:58
If you want to create a "rounded" time stamp, for example, to the nearest 15 minutes use this as a reference:

<?php
$round_numerator = 60 * 15 // 60 seconds per minute * 15 minutes equals 900 seconds
//$round_numerator = 60 * 60 or to the nearest hour
//$round_numerator = 60 * 60 * 24 or to the nearest day

// Calculate time to nearest 15 minutes!
$rounded_time = ( round ( time() / $round_numerator ) * $round_numerator );

//If it was 12:40 this would return the timestamp for 12:45;
//3:04, 3:00; etc.
?>
AT-HE (at_he at h0tm4il dot com)
06-Jun-2006 02:54
egingell:
better use gmdate() function which has no need to shift local time ;)

<?
function gmtime() {
return gmdate();
}
?>
egingell at sisna dot com
03-May-2006 04:00
<?

/**
* Returns the true time in seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
*
* Calling date() with gmtime() as the second parameter is identical to calling
* gmdate() with time() as the second parameter.
**/
function gmtime() { // Get GM offset. See http://www.php.net/manual/en/function.date.php
return time() - (int) date('Z');
}

?>
info at exitorange dot com
22-Feb-2006 12:11
in order to get the timestamp of the beginning of the current day (useful for synchronising) just do this:

$time = time();
$start_time = mktime(0, 0, 0, date('m', $time),date('d', $time),date('Y', $time));
emory dot smith at gmail dot com
20-Feb-2006 08:17
heres another way to convert a mysql timestamp to a unix timestamp without using the function UNIX_TIMESTAMP in mysql:

<?php
$unix_timestamp = strtotime($mysql_timestamp);
?>
aidan at php dot net
08-Oct-2005 08:14
* A simple function for calculating the number of seconds, minutes, etc in a timestamp is here:
http://aidan.dotgeek.org/repos/?file=Duration.php

Example:
<?php
$time = 60*60*2 + 20*60 + 5;

// Gives 2 hours, 20 minutes, 5 seconds
echo Duration::toString($time);

?>

* For manipulating arbitrary format, or length timestamps, see the PEAR::Date class.
http://pear.php.net/package/Date/

* PHP 6 will be shipping a new inbuilt date and timestamp manipulation API. It's available on PECL here:
http://pecl.php.net/package/date_time
mayank_arya at hotmail dot com
29-May-2003 09:13
Here's one way to generate all intermediate dates (in mySQL format) between any 2 dates.
Get start and end dates from user input, you'd need to do the basic validations that :
- start and end dates are valid dates
- start date <= end date.

<?php
//start date 2001-02-23
$sm=2;
$sd=23;
$sy=2001;

//end date 2001-03-14
$em=3;
$ed=14;
$ey=2001;

//utc of start and end dates
$s=mktime(0,0,0,$sm, $sd, $sy);
$e=mktime(0,0,0,$em, $ed, $ey);

while($s<=$e){
print date('Y-m-d',$s)."< br >"; //display date in mySQL format
$s=$s+86400; //increment date by 86400 seconds(1 day)
}

Hope this helps :)

?>
paul at honeylocust dot com
14-Jun-2002 03:56
Be careful about using the database clock (say UNIX_TIMESTAMP() in MySQL) and the time() function if you're writing an application that may have the database be on a different machine than the web server. In that situation, applications can break because of clock skew -- use a single authority for timestamps if possible.
matt at blockdev dot net
22-Sep-2001 10:04
Lots of MySQL traffic, little PostgreSQL. PG hasn't UNIX_TIMESTAMP()- instead, use:

extract(epoch from ____)

As in:

SELECT extract(epoch from mytimestamp) FROM mytable WHERE mycondition = true;
08-Sep-2000 03:42
To convert a MySQL timestamp to a Unix-style timestamp, use MySQL's UNIX_TIMESTAMP function.

For Example:
$result=mysql_query ("SELECT UNIX_TIMESTAMP(timestamp_column) as epoch_time FROM table");

$unix_timestamp = mysql_result ($result, 0, 0);
      $nowtime = time();
echo $nowtime;
echo date("Y-m-d H:i:s",$nowtime);

显示:1169713394
2007-01-25 08:23:14
娱乐休闲专区A 影视预告B 音乐咖啡C 英语阶梯D 生活百科
网页编程专区E AMPZF HTMLG CSSH JSI ASPJ PHPK JSPL MySQLM AJAX
Linux技术区 N 系统管理O 服务器架设P 网络/硬件Q 编程序开发R 内核/嵌入
管理中心专区S 发布网址T 版主议事U 事务处理