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