PHP: time - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

timezone_abbreviations_list strtotime

Last updated: Fri, 26 Jan 2007

view this page in

# time

(PHP 3, PHP 4, PHP 5)

time -- Возвращает текущую метку времени

## Описание

int time ( void )

Возвращает количество секунд, прошедших с начала Эпохи Unix (The Unix Epoch, 1 января 1970, 00:00:00 GMT) до текущего времени.

См. также описание функций date() и microtime().

add a note User Contributed Notes
time
jh2000 at root24 dot eu
25-Feb-2007 12:08
`Just a small notice:
use the time() function instead of date("U"), it is much faster (about 1000 times~)`
tristan at trinicle dot net
23-Nov-2006 02:10
`Initially I was using Andrew's function to convert a timestamp into a formatted age. It has issues with times greater than a day (div by 0). I also added recursion to produce a result similar to "2 decades 6 months 3 weeks 2 days 6 hours 56 minutes 52 seconds" when input a timestamp from 1986. Hopefully someone will find this useful.
<?php
public static function TimeAgo($timestamp){
// Store the current time
$current_time = time();
// Determine the difference, between the time now and the timestamp
$difference = $current_time - $timestamp;
// Set the periods of time
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
// Set the number of seconds per period
$lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
// Determine which period we should use, based on the number of seconds lapsed.
// If the difference divided by the seconds is more than 1, we use that. Eg 1 year / 1 decade = 0.1, so we move on
// Go from decades backwards to seconds
for ($val = sizeof($lengths) - 1; ($val >= 0) && (($number = $difference / $lengths[$val]) <= 1); $val--);
// Ensure the script has found a match
if ($val < 0) $val = 0;
// Determine the minor value, to recurse through
$new_time = $current_time - ($difference % $lengths[$val]);
// Set the current value to be floored
$number = floor($number);
// If required create a plural
if($number != 1) $periods[$val].= "s";
// Return text
$text = sprintf("%d %s ", $number, $periods[$val]);
// Ensure there is still something to recurse through, and we have not found 1 minute and 0 seconds.
if (($val >= 1) && (($current_time - $new_time) > 0)){
$text .= self::TimeAgo($new_time);
}
return $text;
}
?>`
andrew dot macrobert at gmail dot com
29-Oct-2006 02: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
22-Oct-2006 11: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
08-Sep-2006 09: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">&lt;&lt;</a></td>
<td width="14%" bgcolor="#66FF99">
<a href="?sel_date=<?= $prev_month ?>" style="text-decoration: none" title="Prevous Month">&lt;</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">&gt;</a></td>
<td width="14%" bgcolor="#66FF99">
<a href="?sel_date=<?= $next_year ?>" style="text-decoration: none" title="Next Year">&gt;&gt;</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
21-Jul-2006 11: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
08-Jun-2006 11: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
21-Feb-2006 08: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
19-Feb-2006 04: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
07-Oct-2005 05:14
`* A simple function for calculating the number of seconds, minutes, etc in a timestamp is here:
http://aidanlister.com/repos/v/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/datetime`
mayank_arya at hotmail dot com
28-May-2003 06: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
13-Jun-2002 12: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 07: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 12: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);`
add a note
timezone_abbreviations_list strtotime

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST