2 gün önce, 1 gün önce PHP örneğin önce zaman damgası dönüştürme ...

10 Cevap php

I 2009-09-12 20:57:19 biçiminde bir zaman damgası dönüştürmek ve PHP ile 3 minutes ago gibi bir şey çevirmek için çalışıyorum.

Ben yararlı bir komut bunu buldum, ama ben o zaman değişken olarak kullanılmak üzere farklı bir formatta arıyor düşünüyorum. Ben bu formatı ile çalışmak için değiştirmek isteyen kulüpler script:

function _ago($tm,$rcs = 0) {
    $cur_tm = time(); 
    $dif = $cur_tm-$tm;
    $pds = array('second','minute','hour','day','week','month','year','decade');
    $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);

    for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);
        $no = floor($no);
        if($no <> 1)
            $pds[$v] .='s';
        $x = sprintf("%d %s ",$no,$pds[$v]);
        if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0))
            $x .= time_ago($_tm);
        return $x;
    }

Ben bu ilk birkaç satır script (farklı tarih formatı matematik) gibi görünen bir şey yapmaya çalışıyor olduğunu düşünüyorum:

$dif = 1252809479 - 2009-09-12 20:57:19;

Bunu nasıl (unix?) Biçime benim damgası dönüştürme hakkında gitmek istiyorsunuz?

10 Cevap

Use example :

echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('@1367367755'); # timestamp input
echo time_elapsed_string('2013-05-01 00:22:35', true);

Girdi herhangi olabilir supported date and time format.

Output :

4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago

Function :

function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

Herhangi bir hata olmadan apache ve nginx ortamlarında hem de çalışacak gibi bu kodu kullanabilirsiniz:

function time_elapsed_string($datetime, $full = false) {
   $now = new DateTime;
   $ago = new DateTime($datetime);
   $diff = $now->diff($ago);

   if (isset($diff)) {
        $string = array(
        'y' => 'year',
        'm' => 'month',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute'
    );

    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full)
        $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}
   else {
          return 0;
        }
}

Daha sonra bu gibi bu özel işlevini çağırabilirsiniz:

time_elapsed_string($your_desired_date_to_compare, true);

Hem apache ve nginx ortamda çıkışı gibi olacak:

1 year, 10 months, 15 days, 18 hours, 32 minutes ago

(Benim görüşüme göre daha kullanışlı, ya da mantıksal) olarak özgün işlevini biraz değiştirilmiş.

// display x time ago, $rcs is precision depth
function time_ago ($tm, $rcs = 0) {
  $cur_tm = time(); 
  $dif = $cur_tm - $tm;
  $pds = array('second','minute','hour','day','week','month','year','decade');
  $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);

  for ($v = count($lngh) - 1; ($v >= 0) && (($no = $dif / $lngh[$v]) <= 1); $v--);
    if ($v < 0)
      $v = 0;
  $_tm = $cur_tm - ($dif % $lngh[$v]);

  $no = ($rcs ? floor($no) : round($no)); // if last denomination, round

  if ($no != 1)
    $pds[$v] .= 's';
  $x = $no . ' ' . $pds[$v];

  if (($rcs > 0) && ($v >= 1))
    $x .= ' ' . $this->time_ago($_tm, $rcs - 1);

  return $x;
}

Sen damgası her parça almak zorunda, ve Unix zaman içine dönüştürmek gerekir. Damgası, 2009-09-12 20:57:19 örneğin.

(((2008-1970) 365)+(8 30) +12) * 24 20 size 1 Ocak 1970 yılından beri saat KABA tahmin vermek istiyorum.

Çarpın 60 tarafından, bu sayıyı alın ve dakika almak için 57 ekleyin.

Çarpın 60 tarafından, bu atın ve 19 ekleyin.

Bu ancak çok kabaca ve yanlış dönüştürmek olacaktır.

Sadece başlamak için normal bir Unix zaman alamaz herhangi bir neden var mı?

Bu aslında buldum daha iyi bir çözümdür. Mükemmel çalışıyor ancak jQuery kullanır. Ayrıca refreshes automatically SO benzer şekilde ve Facebook böylece güncellemeleri görmek için sayfayı yenilemek zorunda değilsiniz yoktur.

Bu eklenti <time> etiketinde datetime attr okuyun ve sizin için dolduracaktır.

e.g. "4 minutes ago" or "about 1 day ago

http://timeago.yarp.com/

İşte çözüm bu oyuncak için bir zaman damgası dönüştürür sorundur

years ago if it is more than an year ago months ago if it is more than a month ago week ago days ago hours ago seconds ago

http://itfeast.blogspot.in/2013/08/php-convert-timestamp-into-facebook.html

just pass the date time to this func. it would print out in time ago format for you

    function convert($datetime){
      $time=strtotime($datetime);
      $diff=time()-$time;
      $diff/=60;
      $var1=floor($diff);
      $var=$var1<=1 ? 'min' : 'mins';
      if($diff>=60){
        $diff/=60;
        $var1=floor($diff);
        $var=$var1<=1 ? 'hr' : 'hrs';
      }
      if($diff>=24){
        $diff/=24;
        $var1=floor($diff);
        $var=$var1<=1 ? 'day' : 'days';
      }
      if($diff>=30.4375){
        $diff/=30.4375;
        $var1=floor($diff);
        $var=$var1<=1 ? 'month' : 'months';
      }
      if($diff>=12){
        $diff/=12;
        $var1=floor($diff);
        $var=$var1<=1 ? 'year' : 'years';
       }
      echo $var1,' ',$var,' ago';
      }

Burada PHP zaman önce benim çözümdür.

function ago($time = null) {
    $date = ($time instanceof DateTime ? $time : new DateTime($time));
    $diff = date_create('now', $date->getTimezone())->diff($date);

    if ($diff->days === 0) {
        $s = ($diff->days * 43200) + ($diff->h * 3600) + ($diff->i * 60) + $diff->s;

        if ($s <= 4) return 'now';
        if ($s <= 12) return 'few seconds ago';
        if ($s <= 59) return $s . ' second' . ($diff->s > 1 ? 's' : null) . ' ago';
        if ($s <= 75) return 'a minute ago';
        if ($s > 1740 && $s < 1860) return 'half hour ago';
        if ($s <= 3600) return ($diff->i > 1 ? $diff->i . ' minutes' : 'an hour') . ' ago';
        if ($s > 3600) return ($diff->h > 1 ? $diff->h . ' hours' : 'an hour') . ($diff->i > 1 ? ' ' . $diff->i . ' minutes' : null) . ' ago';
    }

    if ($diff->days === 1 && $diff->h < 1) {
        $h = ($diff->days * 24) + ($diff->h);
        return $h . ' hour' . ($h > 1 ? 's' : null) . ($diff->i > 1 ? ' ' . $diff->i . ' minutes' : null) . ' ago';
    }

    if ($diff->days <= 25) return $diff->days . ' day' . ($diff->days > 1 ? 's' : null) . ($diff->h > 1 ? ' ' . $diff->h . ' hours' : ' and hour') . ' ago';
    if ($diff->days <= 45) return 'a month ago';
    if ($diff->days <= 345) return ($diff->m > 1 ? $diff->m . ' months' : 'an month') . ($diff->d > 1 ? ' ' . $diff->d . ' days' : null) . ' ago';
    if ($diff->days <= 548) return 'a year ago';
    if ($diff->days > 548) return $diff->y . ' years ago';
    if ($diff->days > 365) return $diff->y . ' years ago';
}

Ve burada değerlerin bir örneğidir:

$dates = [
    'now', // now
    '-5 seconds', // few seconds ago
    '-30 seconds', // 30 seconds ago
    '-60 seconds', // a minute ago
    '-29 minutes', // 29 minutes ago
    '-30 minutes', // half hour ago
    '-31 minutes', // 31 minutes ago
    '-45 minutes', // 45 minutes ago
    '-59 minutes', // 59 minutes ago
    '-1 hour', // a hour ago
    '-61 minutes', // an hour ago
    '-75 minutes', // an hour 15 minutes ago
    '-90 minutes', // an hour 30 minutes ago
    '-12 hours', // 12 hours ago
    '-24 hours 1 minutes', // 23 hours 59 minutes ago
    '-24 hours', // 1 day and hour ago
    '-25 hours', // 1 days ago
    '-26 hours', // 1 day 2 hours ago
    '-46 hours', // 1 day 22 hours ago
    '-48 hours', // 2 days and hour ago
    '-50 hours', // 2 days 2 hours ago
    '-6 months', // 6 months ago
    '-11 months', // 6 months ago
    '-1 years', // a year ago
    '-10 years', // 10 years ago
    '-100 years', // 100 years ago
];

echo '<table>';
foreach ($dates as $date) {
    echo '<tr><th>' . $date . '</th><td>' . ago(new DateTime($date)) . '</td></tr>';
}
echo '</table>';

Yo kullanabilirsiniz:

date("Y-m-d H:i:s",strtotime("1 day ago"));