|
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()。
andrew dot macrobert at gmail dot com
30-Oct-2006 06:33
An improved version of my previous function:
<?
function ago($timestamp){
$difference = time() - $timestamp;
$periods = array("second", "minute", "hour", "day", "week", "month", "years", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] ago";
return $text;
}
?>
andrew dot macrobert at gmail dot com
23-Oct-2006 02:47
This function takes a timestamp and returns how long ago it was, in seconds, minutes, hours, days, or weeks (it will return it in minutes if it was >= than 60 seconds ago, hours if it was >= 60 minutes, etc.).
<?php
function ago($timestamp){
$difference = time() - $timestamp;
if($difference < 60)
return $difference." seconds ago";
else{
$difference = round($difference / 60);
if($difference < 60)
return $difference." minutes ago";
else{
$difference = round($difference / 60);
if($difference < 24)
return $difference." hours ago";
else{
$difference = round($difference / 24);
if($difference < 7)
return $difference." days ago";
else{
$difference = round($difference / 7);
return $difference." weeks ago";
}
}
}
}
}
?>
krisdover at hotmail dot com
09-Sep-2006 12:54
# a simple html/php formatted calendar which returns
# the date as a unix timestamp when the required day
# is selected. Also allows for setting of time in 24hr format
# kris dover, 2006-09-09
<?php
$sel_date = isset($_REQUEST['sel_date']) ? $_REQUEST['sel_date'] : time();
if( isset($_POST['hrs']) ){
$t = getdate($sel_date);
$sel_date = mktime($_POST['hrs'], $_POST['mins'], $t['seconds'], $t['mon'], $t['mday'], $t['year']);
}
$t = getdate($sel_date);
$start_date = mktime($t['hours'], $t['minutes'], $t['seconds'], $t['mon'], 1, $t['year']);
$start_date -= 86400 * date('w', $start_date);
$prev_year = mktime($t['hours'], $t['minutes'], $t['seconds'], $t['mon'], $t['mday'], $t['year'] - 1);
$prev_month = mktime($t['hours'], $t['minutes'], $t['seconds'], $t['mon'] - 1, $t['mday'], $t['year']);
$next_year = mktime($t['hours'], $t['minutes'], $t['seconds'], $t['mon'], $t['mday'], $t['year'] + 1);
$next_month = mktime($t['hours'], $t['minutes'], $t['seconds'], $t['mon'] + 1, $t['mday'], $t['year']);
?>
<form method="post">
<table width="180" border="0" cellspacing="1"
style="border: 1px solid black; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: x-small; text-align: center">
<tr>
<td width="14%" bgcolor="#66FF99">
<a href="?sel_date=<?= $prev_year ?>" style="text-decoration: none" title="Prevous Year"><<</a></td>
<td width="14%" bgcolor="#66FF99">
<a href="?sel_date=<?= $prev_month ?>" style="text-decoration: none" title="Prevous Month"><</a></td>
<td colspan="3" bgcolor="#66FF99">
<?= date('M Y', $sel_date) ?>
</td>
<td width="14%" bgcolor="#66FF99">
<a href="?sel_date=<?= $next_month ?>" style="text-decoration: none" title="Next Month">></a></td>
<td width="14%" bgcolor="#66FF99">
<a href="?sel_date=<?= $next_year ?>" style="text-decoration: none" title="Next Year">>></a></td>
</tr>
<tr>
<td bgcolor="#0099FF">Sun</td>
<td bgcolor="#0099FF">Mon</td>
<td width="14%" bgcolor="#0099FF">Tue</td>
<td width="14%" bgcolor="#0099FF">Wed</td>
<td width="14%" bgcolor="#0099FF">Thu</td>
<td bgcolor="#0099FF">Fri</td>
<td bgcolor="#0099FF">Sat</td>
</tr>
<?php
$day = 1;
for($i = $start_date; $day <= 42; $i+=86400, $day++){
if( $day % 7 == 1 ) echo "<tr>\n";
if( $t['mon'] == date('n', $i ) )
if( $i == $sel_date )
echo ' <td bgcolor="gold">'. date('j', $i) ."</td>\n";
else
echo ' <td><a href="?sel_date='. $i .'" style="text-decoration: none">'. date('j', $i) ."</a></td>\n";
else
echo ' <td ><a href="?sel_date='. $i .'" style="text-decoration: none"><font color="silver">'. date('j', $i) ."</font></a></td>\n";
if( $day % 7 == 0 ) echo "</tr>\n";
}
?>
<tr>
<td colspan="7" align="left" bgcolor="silver">Time:
<select name="hrs" onchange="document.forms[0].submit()">
<?php
for($i = 0; $i < 24; $i++)
echo ' <option '. (date('G', $sel_date)==$i ? 'selected':'') .'>'. sprintf('%02d', $i) ."</option>\n";
?>
</select>:
<select name="mins" onchange="document.forms[0].submit()">
<?php
for($i = 0; $i < 60; $i++)
echo ' <option '. (date('i', $sel_date)==$i ? 'selected':'') .'>'. sprintf('%02d', $i) ."</option>\n";
?>
</select> hrs
<input type="hidden" name="sel_date" value="<?= $sel_date ?>">
</td>
</tr>
</table>
</form>
STaRDoGGCHaMP
22-Jul-2006 02:34
This function formate a timestamp into days, hours, minutes and seconds.
e.g the time until your birthday.
<?php
function formatetimestamp($until){
$now = time();
$difference = $until - $now;
$days = floor($difference/86400);
$difference = $difference - ($days*86400);
$hours = floor($difference/3600);
$difference = $difference - ($hours*3600);
$minutes = floor($difference/60);
$difference = $difference - ($minutes*60);
$seconds = $difference;
$output = "You have to wait $days Days, $hours Hours, $minutes Minutes and $seconds Seconds until this Day.";
return $output;
}
//int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )
echo formatetimestamp(mktime(0,0,0,12,31,2006)); //output: e.g "You have to wait 162 Days, 4 Hours, 38 Minutes and 46 Seconds until this Day"
?>
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.
?>
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);
?>
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);
| |