查看: 5701|回复: 1
打印 上一主题 下一主题

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

[复制链接]
跳转到指定楼层
1#
发表于 2007-10-4 17:30:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
我今天看了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() 例子

<?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);
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
2#
 楼主| 发表于 2007-10-4 17:31:08 | 只看该作者
台州网址导航

日期与时间函式库:time()函数得一个例子

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

显示:1169713394
          2007-01-25 08:23:14
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表