PHP 7.4.24 Released!

time

(PHP 4, PHP 5, PHP 7, PHP 8)

time現在の Unix タイムスタンプを返す

説明

time(): int

現在時刻を Unix エポック (1970 年 1 月 1 日 00:00:00 GMT) からの通算秒として返します。

パラメータ

この関数にはパラメータはありません。

返り値

現在のタイムスタンプを返します。

例1 time() の例

<?php
$nextWeek 
time() + (24 60 60);
                   
// 7 日 * 24 時間 * 60 分 * 60 秒
echo 'Now:       'date('Y-m-d') ."\n";
echo 
'Next Week: 'date('Y-m-d'$nextWeek) ."\n";
// あるいは strtotime() を使用します
echo 'Next Week: 'date('Y-m-d'strtotime('+1 week')) ."\n";
?>

上の例の出力は、 たとえば以下のようになります。

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

注意

ヒント

$_SERVER['REQUEST_TIME'] によって、リクエスト開始時のタイムスタンプが取得できます。

参考

  • date() - ローカルの日付/時刻を書式化する
  • microtime() - 現在の Unix タイムスタンプをマイクロ秒まで返す

add a note add a note

User Contributed Notes 21 notes

up
318
Timo K
10 years ago
The documentation should have this info. The function time() returns always timestamp that is timezone independent (=UTC).

<?php
date_default_timezone_set
("UTC");
echo
"UTC:".time();
echo
"<br>";

date_default_timezone_set("Europe/Helsinki");
echo
"Europe/Helsinki:".time();
echo
"<br>";
?>

Local time as string can be get by strftime() and local timestamp (if ever needed) by mktime().
up
26
Ray.Paseur sometimes uses Gmail
5 years ago
The number of 86,400 seconds in a day comes from the assumption of 60 seconds per minute, 60 minutes per hour and 24 hours per day: 60*60*24.  But not every day has 24 hours.  This edge case occurs on the days that clocks change as we enter and leave daylight savings (summer) time.  Date and time arithmetic is logically consistent and correct when you use PHP built-in functions, but it may not always work as expected if you try to write your own date and time arithmetic.
up
49
salladin
9 years ago
Two quick approaches to getting the time elapsed in human readable form.

<?php

function time_elapsed_A($secs){
   
$bit = array(
       
'y' => $secs / 31556926 % 12,
       
'w' => $secs / 604800 % 52,
       
'd' => $secs / 86400 % 7,
       
'h' => $secs / 3600 % 24,
       
'm' => $secs / 60 % 60,
       
's' => $secs % 60
       
);
       
    foreach(
$bit as $k => $v)
        if(
$v > 0)$ret[] = $v . $k;
       
    return
join(' ', $ret);
    }
   

function
time_elapsed_B($secs){
   
$bit = array(
       
' year'        => $secs / 31556926 % 12,
       
' week'        => $secs / 604800 % 52,
       
' day'        => $secs / 86400 % 7,
       
' hour'        => $secs / 3600 % 24,
       
' minute'    => $secs / 60 % 60,
       
' second'    => $secs % 60
       
);
       
    foreach(
$bit as $k => $v){
        if(
$v > 1)$ret[] = $v . $k . 's';
        if(
$v == 1)$ret[] = $v . $k;
        }
   
array_splice($ret, count($ret)-1, 0, 'and');
   
$ret[] = 'ago.';
   
    return
join(' ', $ret);
    }
   

   
   
$nowtime = time();
$oldtime = 1335939007;

echo
"time_elapsed_A: ".time_elapsed_A($nowtime-$oldtime)."\n";
echo
"time_elapsed_B: ".time_elapsed_B($nowtime-$oldtime)."\n";

/** Output:
time_elapsed_A: 6d 15h 48m 19s
time_elapsed_B: 6 days 15 hours 48 minutes and 19 seconds ago.
**/
?>
up
9
yasmary at gmail dot com
12 years ago
A time difference function that outputs the time passed in facebook's style: 1 day ago, or 4 months ago. I took andrew dot macrobert at gmail dot com function and tweaked it a bit. On a strict enviroment it was throwing errors, plus I needed it to calculate the difference in time between a past date and a future date.

<?php
function nicetime($date)
{
    if(empty(
$date)) {
        return
"No date provided";
    }
   
   
$periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
   
$lengths         = array("60","60","24","7","4.35","12","10");
   
   
$now             = time();
   
$unix_date         = strtotime($date);
   
      
// check validity of date
   
if(empty($unix_date)) {   
        return
"Bad date";
    }

   
// is it future date or past date
   
if($now > $unix_date) {   
       
$difference     = $now - $unix_date;
       
$tense         = "ago";
       
    } else {
       
$difference     = $unix_date - $now;
       
$tense         = "from now";
    }
   
    for(
$j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
       
$difference /= $lengths[$j];
    }
   
   
$difference = round($difference);
   
    if(
$difference != 1) {
       
$periods[$j].= "s";
    }
   
    return
"$difference $periods[$j] {$tense}";
}

$date = "2009-03-04 17:45";
$result = nicetime($date); // 2 days ago

?>
up
2
johan dot lindskog at humlab dot umu dot se
11 years ago
I needed to convert between Unix timestamps and Windows/AD timestamps, so I wrote a pair of simple functions for it.

<?php
function unix_time_to_win_time($unix_time) {
 
//add the seconds between 1601-01-01 and 1970-01-01 and make it 100-nanosecond precision
 
$win_time = ($unix_time + 11644477200) * 10000000;
  return
$win_time;
}

function
win_time_to_unix_time($win_time) {
 
//round the win timestamp down to seconds and remove the seconds between 1601-01-01 and 1970-01-01
 
$unix_time = round($win_time / 10000000) - 11644477200;
  return
$unix_time;
}
?>
up
0
correa dot rafael at live dot com
2 years ago
Here's a little tweak for those having trouble with cookies being set in the future or past (even after setting the date.timezone directive in php.ini or using the function):

<?php
    setcookie
('userAgent',$_SERVER['HTTP_USER_AGENT'], strtotime(date('r', time()).'+2 days'));
?>

The example above will fetch the current time() in Unix Epoch, convert to your date() and timezone, and covert the result to Unix Epoch again with strtotime() so you can use it to set cookie expiration dates.
up
-1
Dan J
3 years ago
It seems that the output of time()> is a few hundred microseconds behind the output of microtime() - be wary of mixing these functions.

Here's a snippet of code that demonstrates the difference:

<?php

// Find the next second
$nextSecond = time() + 1;

// Sleep until 2.5 milliseconds before the next second
time_sleep_until(floatval($nextSecond) - 2.5e-3);

// Check time() and microtime() every 250 microseconds for 20 iterations
for ($i = 0; $i < 20; $i ++) {
   
$time = time();
   
$uTime = microtime(true);
   
   
printf("TIME: %d  uTIME: %0.6f", $time, $uTime);
   
    if (
floor($uTime) > $time) {
        echo
"  time() is behind";
    }
   
    echo
"\n";
   
usleep(250);
}

// Example output:

// TIME: 1525735820  uTIME: 1525735820.997716
// TIME: 1525735820  uTIME: 1525735820.998137
// TIME: 1525735820  uTIME: 1525735820.998528
// TIME: 1525735820  uTIME: 1525735820.998914
// TIME: 1525735820  uTIME: 1525735820.999287
// TIME: 1525735820  uTIME: 1525735820.999657
// TIME: 1525735820  uTIME: 1525735821.000026  time() is behind
// TIME: 1525735820  uTIME: 1525735821.000367  time() is behind
// TIME: 1525735820  uTIME: 1525735821.000705  time() is behind
// TIME: 1525735820  uTIME: 1525735821.001042  time() is behind
// TIME: 1525735820  uTIME: 1525735821.001379  time() is behind
// TIME: 1525735821  uTIME: 1525735821.001718
// TIME: 1525735821  uTIME: 1525735821.002070
// TIME: 1525735821  uTIME: 1525735821.002425
// TIME: 1525735821  uTIME: 1525735821.002770
// TIME: 1525735821  uTIME: 1525735821.003109
// TIME: 1525735821  uTIME: 1525735821.003448
// TIME: 1525735821  uTIME: 1525735821.003787
// TIME: 1525735821  uTIME: 1525735821.004125
// TIME: 1525735821  uTIME: 1525735821.004480
up
0
tav
9 years ago
Calculates the difference between $start and $s, returns a formatted string Xd Yh Zm As, e.g. 15d 23h 54m 31s. Empty sections will be stripped, returning 12d 4s, not 12d 0h 0m 4s.

Argument order (begin date, end date) doesn't matter.

<?php

function time_diff_conv($start, $s) {
   
$t = array( //suffixes
       
'd' => 86400,
       
'h' => 3600,
       
'm' => 60,
    );
   
$s = abs($s - $start);
    foreach(
$t as $key => &$val) {
        $
$key = floor($s/$val);
       
$s -= ($$key*$val);
       
$string .= ($$key==0) ? '' : $$key . "$key ";
    }
    return
$string . $s. 's';
}

?>
up
0
anon
12 years ago
Below, a function to create TNG-style stardates, taking 2009 to start stardate 41000.0.  In fact, the offset is trivial to adjust if you wish to begin from a different date.

<?php
function getStardate(void)
{
 
$offset = 2000;
 
$seconds_per_stardate = 31449.6; // is the number of seconds in a year divided by 1000, for hopefully obvious reasons
 
return time() / $seconds_per_stardate + $offset;
}
?>

Other series use less reliable stardate formats, which makes it difficult [read: nigh impossible] to create a function that converts a unix timestamp into a stardate.
up
-2
jaak dot tamre at gmail dot com
4 years ago
Does anyone know if the year 2038 issue will be solved in PHP?

Lets imagine it's year 2039 and the time() function will return negative numbers? This is not acceptable.

Using the DateTime interface is nice, but will these timestamp helper functions be removed or fixed?
up
-3
send at mail dot 2aj dot net
15 years ago
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.
?>
up
-2
jnostuh at gmail dot com
7 years ago
I built this function to get the strtotime numbers for the beginning and ending of the month and return them as arrays in an object.  Cheers.

        //Returns values in the format of strtotime for evaluating purposes!
        function returnDateFirstAndLast($numberOfMonths) {
            $time=time();
            $lastdayofcurrentmonth=strtotime('+1 month -1 day',strtotime(date('Y-m-1 24:59:59',$time)));
            $values=new stdClass();
           
            for ( $i = 0;$i < $numberOfMonths;$i++ ) {
                $time=time();
                $firstdayofmonth=strtotime('-'.$i.' month',strtotime(date('Y-m-1 00:00:00',$time)));
                $lastdayofmonth=strtotime('-'.($i - 1).' month -1 day',strtotime(date('Y-m-1 24:59:59',$time)));
                $first[$i]=$firstdayofmonth;
                if ( $i == 0 ) {
                    $last[$i]=$lastdayofcurrentmonth;
                } else {
                    $last[$i]=$lastdayofmonth;
                }
            }
            $values->first=$first;
            $values->last=$last;
           
            return $values;
        }
up
-11
Peter F
6 years ago
Javier:

The issue are highlighting is with the date() function, not with time(). the following code demonstrates this:

<?php
// server in BST Timezone
print 'server timezone<br>';
$time1 = date('H:i:s', time() - date('Z')); // 12:50:29
$time2 = date('H:i:s', gmdate('U')); // 13:50:29
$time3 = date('H:i:s', time()); // 13:50:29
$time4 = time() - date('Z'); // 1433418629
$time5 = gmdate('U'); // 1433422229
$time6 = time(); // 1433422229
print $time1.'<br>';
print
$time2.'<br>';
print
$time3.'<br>';
print
$time4.'<br>';
print
$time5.'<br>';
print
$time6.'<br>';

ini_set('date.timezone', 'Europe/Berlin');
print
'Europe/Berlin<br>';
$time1 = date('H:i:s', time() - date('Z')); // 12:50:29
$time2 = date('H:i:s', gmdate('U')); // 14:50:29
$time3 = date('H:i:s', time()); // 14:50:29
$time4 = time() - date('Z'); // 1433415029
$time5 = gmdate('U'); // 1433422229
$time6 = time(); // 1433422229
print $time1.'<br>';
print
$time2.'<br>';
print
$time3.'<br>';
print
$time4.'<br>';
print
$time5.'<br>';
print
$time6.'<br>';

ini_set('date.timezone', 'UTC');
print
'UTC<br>';
$time1 = date('H:i:s', time() - date('Z')); // 12:50:29
$time2 = date('H:i:s', gmdate('U')); // 12:50:29
$time3 = date('H:i:s', time()); // 12:50:29
$time4 = time() - date('Z'); // 1433422229
$time5 = gmdate('U'); // 1433422229
$time6 = time(); // 1433422229
print $time1.'<br>';
print
$time2.'<br>';
print
$time3.'<br>';
print
$time4.'<br>';
print
$time5.'<br>';
print
$time6.'<br>';
?>
up
-7
APM
3 years ago
A better way to get a nice time-format (1 year ago, 2 months until) without all the trailing months, days, hours, minutes, seconds in the result is by using the DateTime format and using the date_diff function as they both does most of the heavy lifting for you

Function below as example

<?php
   
   
// Get time interval function
   
function getTimeInterval($date) {
       
       
// Set date as DateTime object
       
$date = new DateTime ($date);
       
       
// Set now as DateTime object
       
$now = date ('Y-m-d H:i:s', time()); // If you want to compare two dates you both provide just delete this line and add a $now to the function parameter (ie. function getTimeInterval($date, $now))
       
$now = new DateTime ($now);
       
       
// Check if date is in the past or future and calculate timedifference and set tense accordingly
       
if ($now >= $date) {
           
$timeDifference = date_diff ($date , $now);
           
$tense = " ago";
        } else {
           
$timeDifference = date_diff ($now, $date);
           
$tense = " until";
        }
       
       
// Set posible periods (lowest first as to end result with the highest value that isn't 0)
       
$period = array (" second", " minute", " hour", " day", " month", " year");
       
       
// Set values of the periods using the DateTime formats (matching the periods above)
       
$periodValue= array ($timeDifference->format('%s'), $timeDifference->format('%i'), $timeDifference->format('%h'), $timeDifference->format('%d'), $timeDifference->format('%m'), $timeDifference->format('%y'));
       
       
// Loop through the periods (ie. seconds to years)
       
for ($i = 0; $i < count($periodValue); $i++) {
           
// If current value is different from 1 add 's' to the end of current period (ie. seconds)
           
if ($periodValue[$i] != 1) {
               
$period[$i] .= "s";
            }
           
           
// If current value is larger than 0 set new interval overwriting the lower value that came before ensuring the result shows only the highest value that isn't 0
           
if ($periodValue[$i] > 0) {
               
$interval = $periodValue[$i].$period[$i].$tense; // ie.: 3 months ago
           
}
        }
       
       
// If any values were larger than 0 (ie. timedifference is not 0 years, 0 months, 0 days, 0 hours, 0 minutes, 0 seconds ago) return interval
       
if (isset($interval)) {
            return
$interval;
       
// Else if no values were larger than 0 (ie. timedifference is 0 years, 0 months, 0 days, 0 hours, 0 minites, 0 seconds ago) return 0 seconds ago
       
} else {
            return
"0 seconds" . $tense;
        }
    }
   
   
// Ex. (time now = November 23 2017)
   
getTimeInterval("2016-05-04 12:00:00"); // Returns: 1 year ago
   
getTimeInterval("2017-12-24 12:00:00"); // Returns: 1 month until
   
?>
up
-6
lsd25 at hotmail dot com
14 years ago
I did an article on floating point time you can download from my website. Roun movements is the radial ounion movement and there is a quantum ounion movement as well, this code will generate the data for http://www.chronolabs.org.au/bin/roun-time-article.pdf which is an article on floating point time, I have created the calendar system as well for this time. It is compatible with other time and other solar systems with different revolutions of the planets as well as different quantumy stuff.

Thanks:

<?php
if ($gmt>0){
       
$gmt=-$gmt;
    } else {
       
$gmt=$gmt+$gmt+$gmt;
    }
   
   
$ptime = strtotime('2008-05-11 10:05 AM')+(60*60*gmt);
   
$weight = -20.22222222223+(1*gmt);

   
$roun_xa = ($tme)/(24*60*60);
   
$roun_ya = $ptime/(24*60*60);
   
$roun = (($roun_xa -$roun_ya) - $weight)+(microtime/999999);
   
   
$nonedeficient = array("seq1" => array(31,30,31,30,30,30,31,30,31,30,31,30),
                          
"seq2" => array(31,30,31,30,31,30,31,30,31,30,31,30),   
                          
"seq3" => array(31,30,31,30,30,30,31,30,31,30,31,30),
                          
"seq4" => array(31,30,31,30,30,30,31,30,31,30,31,30));

   
$deficient =     array("seq1" => array(31,30,31,30,30,30,31,30,31,30,31,30),
                          
"seq2" => array(31,30,31,30,31,30,31,30,31,30,31,30),   
                          
"seq3" => array(31,30,31,30,31,30,31,30,30,30,31,30),
                          
"seq4" => array(30,30,31,30,31,30,31,30,31,30,31,30));

   
$monthusage = isset($_GET['deficienty']) ? ${$_GET['deficienty']} : $deficient;
   
    foreach(
$monthusage as $key => $item){
       
$i++;
        foreach(
$item as $numdays){
           
$ttl_num=$ttl_num+$numdays;
        }
    }
   
   
$revolutionsperyear = $ttl_num / $i;
   
$numyears = round((round(ceil($roun)) / $revolutionsperyear),0);
   
   
$jtl = abs(abs($roun) - ceil($revolutionsperyear*($numyears+1)));
   
    while(
$month==0){
       
$day=0;
        foreach(
$monthusage as $key => $item){
           
$t++;
           
$u=0;
            foreach(
$item as $numdays){
                if (
$ii<abs($roun)){
                   
$isbelow=true;
                }
               
$ii=$ii+$numdays;
                if (
$ii>abs($roun)){
                   
$isabove=true;
                }
                if (
$isbelow==true&&$isabove==true){
                   
$daynum = floor(($ii-$numday)-abs($roun));
                   
$month = $u;
                   
$month++;
                   
$isbelow=false;
                   
$isabove=false;
                   
$nodaycount=true;
                }
                if (
$nodaycount==false)
                   
$day++;
               
$u++;
            }
        }
   
    }
   
   
$timer = substr($roun, strpos($roun,'.')+1,strlen($roun)-strpos($roun,'.')-1);
   
   
$roun_out= $numyears.'-'.$month.'-'.$daynum.' '.$day.".$timer";

?>
up
-6
mayank_arya at hotmail dot com
18 years ago
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 :)

?>
up
-10
Yi
10 years ago
A method return GMT time (gmttime):

<?php
$gmtimenow
= time() - (int)substr(date('O'),0,3)*60*60;
echo
$gmtimenow . "\n";
?>

it convert current time to GMT based on time zone offset.

by frank.
up
-10
siegfried at anggit dot com
9 years ago
elapsed time function with precision:

<?php

function elapsed_time($timestamp, $precision = 2) {
 
$time = time() - $timestamp;
 
$a = array('decade' => 315576000, 'year' => 31557600, 'month' => 2629800, 'week' => 604800, 'day' => 86400, 'hour' => 3600, 'min' => 60, 'sec' => 1);
 
$i = 0;
    foreach(
$a as $k => $v) {
      $
$k = floor($time/$v);
      if ($
$k) $i++;
     
$time = $i >= $precision ? 0 : $time - $$k * $v;
     
$s = $$k > 1 ? 's' : '';
      $
$k = $$k ? $$k.' '.$k.$s.' ' : '';
      @
$result .= $$k;
    }
  return
$result ? $result.'ago' : '1 sec to go';
}

echo
elapsed_time('1234567890').'<br />'; // 3 years 5 months ago
echo elapsed_time('1234567890', 6); // 3 years 5 months 1 week 2 days 57 mins 4 secs ago

?>
up
-6
nickr at visuality dot com
11 years ago
Here is a version for the difference code that displays "ago" code.

It does use some precision after the time difference is longer than a day. ( ie days are more than 60 * 60 * 24 hours long )

<?php

   
function ago($datefrom,$dateto=-1)
    {
       
// Defaults and assume if 0 is passed in that
        // its an error rather than the epoch
   
       
if($datefrom==0) { return "A long time ago"; }
        if(
$dateto==-1) { $dateto = time(); }
       
       
// Make the entered date into Unix timestamp from MySQL datetime field

       
$datefrom = strtotime($datefrom);
   
       
// Calculate the difference in seconds betweeen
        // the two timestamps

       
$difference = $dateto - $datefrom;

       
// Based on the interval, determine the
        // number of units between the two dates
        // From this point on, you would be hard
        // pushed telling the difference between
        // this function and DateDiff. If the $datediff
        // returned is 1, be sure to return the singular
        // of the unit, e.g. 'day' rather 'days'
   
       
switch(true)
        {
           
// If difference is less than 60 seconds,
            // seconds is a good interval of choice
           
case(strtotime('-1 min', $dateto) < $datefrom):
               
$datediff = $difference;
               
$res = ($datediff==1) ? $datediff.' second ago' : $datediff.' seconds ago';
                break;
           
// If difference is between 60 seconds and
            // 60 minutes, minutes is a good interval
           
case(strtotime('-1 hour', $dateto) < $datefrom):
               
$datediff = floor($difference / 60);
               
$res = ($datediff==1) ? $datediff.' minute ago' : $datediff.' minutes ago';
                break;
           
// If difference is between 1 hour and 24 hours
            // hours is a good interval
           
case(strtotime('-1 day', $dateto) < $datefrom):
               
$datediff = floor($difference / 60 / 60);
               
$res = ($datediff==1) ? $datediff.' hour ago' : $datediff.' hours ago';
                break;
           
// If difference is between 1 day and 7 days
            // days is a good interval               
           
case(strtotime('-1 week', $dateto) < $datefrom):
               
$day_difference = 1;
                while (
strtotime('-'.$day_difference.' day', $dateto) >= $datefrom)
                {
                   
$day_difference++;
                }
               
               
$datediff = $day_difference;
               
$res = ($datediff==1) ? 'yesterday' : $datediff.' days ago';
                break;
           
// If difference is between 1 week and 30 days
            // weeks is a good interval           
           
case(strtotime('-1 month', $dateto) < $datefrom):
               
$week_difference = 1;
                while (
strtotime('-'.$week_difference.' week', $dateto) >= $datefrom)
                {
                   
$week_difference++;
                }
               
               
$datediff = $week_difference;
               
$res = ($datediff==1) ? 'last week' : $datediff.' weeks ago';
                break;           
           
// If difference is between 30 days and 365 days
            // months is a good interval, again, the same thing
            // applies, if the 29th February happens to exist
            // between your 2 dates, the function will return
            // the 'incorrect' value for a day
           
case(strtotime('-1 year', $dateto) < $datefrom):
               
$months_difference = 1;
                while (
strtotime('-'.$months_difference.' month', $dateto) >= $datefrom)
                {
                   
$months_difference++;
                }
               
               
$datediff = $months_difference;
               
$res = ($datediff==1) ? $datediff.' month ago' : $datediff.' months ago';

                break;
           
// If difference is greater than or equal to 365
            // days, return year. This will be incorrect if
            // for example, you call the function on the 28th April
            // 2008 passing in 29th April 2007. It will return
            // 1 year ago when in actual fact (yawn!) not quite
            // a year has gone by
           
case(strtotime('-1 year', $dateto) >= $datefrom):
               
$year_difference = 1;
                while (
strtotime('-'.$year_difference.' year', $dateto) >= $datefrom)
                {
                   
$year_difference++;
                }
               
               
$datediff = $year_difference;
               
$res = ($datediff==1) ? $datediff.' year ago' : $datediff.' years ago';
                break;
               
        }
        return
$res;

?>
up
-9
Anonymous
13 years ago
A cleaner example (half the comparisons) of distanceOfTimeInWords() function below:

<?php
public static function distanceOfTimeInWords($fromTime, $toTime = 0, $showLessThanAMinute = false) {
   
$distanceInSeconds = round(abs($toTime - $fromTime));
   
$distanceInMinutes = round($distanceInSeconds / 60);
       
        if (
$distanceInMinutes <= 1 ) {
            if ( !
$showLessThanAMinute ) {
                return (
$distanceInMinutes == 0) ? 'less than a minute' : '1 minute';
            } else {
                if (
$distanceInSeconds < 5 ) {
                    return
'less than 5 seconds';
                }
                if (
$distanceInSeconds < 10 ) {
                    return
'less than 10 seconds';
                }
                if (
$distanceInSeconds < 20 ) {
                    return
'less than 20 seconds';
                }
                if (
$distanceInSeconds < 40 ) {
                    return
'about half a minute';
                }
                if (
$distanceInSeconds < 60 ) {
                    return
'less than a minute';
                }
               
                return
'1 minute';
            }
        }
        if (
$distanceInMinutes < 45 ) {
            return
$distanceInMinutes . ' minutes';
        }
        if (
$distanceInMinutes < 90 ) {
            return
'about 1 hour';
        }
        if (
$distanceInMinutes < 1440 ) {
            return
'about ' . round(floatval($distanceInMinutes) / 60.0) . ' hours';
        }
        if (
$distanceInMinutes < 2880 ) {
            return
'1 day';
        }
        if (
$distanceInMinutes < 43200 ) {
            return
'about ' . round(floatval($distanceInMinutes) / 1440) . ' days';
        }
        if (
$distanceInMinutes < 86400 ) {
            return
'about 1 month';
        }
        if (
$distanceInMinutes < 525600 ) {
            return
round(floatval($distanceInMinutes) / 43200) . ' months';
        }
        if (
$distanceInMinutes < 1051199 ) {
            return
'about 1 year';
        }
       
        return
'over ' . round(floatval($distanceInMinutes) / 525600) . ' years';
}
?>
up
-5
derinus at gmail dot com
1 year ago
Please do NOT use above code example to calculate $nextWeek.
In zones with daylight saving time it's wrong twice a year.
To Top