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

search for in the

DatePeriod> <DateInterval::createFromDateString
[edit] Last updated: Fri, 23 Mar 2012

view this page in

DateInterval::format

(PHP 5 >= 5.3.0)

DateInterval::formatZaman aralığını biçimler

Açıklama

public string DateInterval::format ( string $biçem )

Zaman aralığını biçimlendirir.

Değiştirgeler

biçem

biçem dizgesinde tanınan karakterler aşağıda listelenmiştir. Her biçem karakterinin başına bir yüzde (%) işareti konmalıdır.
Karakter Açıklama Örnek değerler
% % iminin kendisi %
Y Başına 0 getirilerek iki haneli yıl 01, 03
y 0 ile öncelenmeksizin yıl 1, 3
M Başına 0 getirilerek iki haneli ay 01, 03, 12
m 0 ile öncelenmeksizin ay 1, 3, 12
D Başına 0 getirilerek iki haneli ayın günü 01, 03, 31
d 0 ile öncelenmeksizin ayın günü 1, 3, 31
a Toplam gün sayısı 4, 18, 8123
H Başına 0 getirilerek iki haneli saat 01, 03, 23
h 0 ile öncelenmeksizin saat 1, 3, 23
I Başına 0 getirilerek iki haneli dakika 01, 03, 59
i 0 ile öncelenmeksizin dakika 1, 3, 59
S Başına 0 getirilerek iki haneli saniye 01, 03, 57
s 0 ile öncelenmeksizin saniye 1, 3, 57
R Negatifse "-", pozitifse "+" imi -, +
r Negatifse "-", pozitifse boş dizge -,

Dönen Değerler

Biçimlendirilmiş zaman aralığını bir dizge olarak döndürür.

Notlar

Bilginize:

DateInterval::format() yöntemi ne zaman dizgelerinde ne de tarih bölümlerinde tehir edilen kısımları yeniden hesaplar. Bunun sebebi "32 gün" gibi büyük değerlerin olanaksız oluşudur. Olanaklı olsaydı böyle bir değer "1 ay 4 gün" ile "1 ay 1 gün" arasında bir değer olarak yorumlanabilirdi.

Örnekler

Örnek 1 - DateInterval örneği

<?php

$interval 
= new DateInterval('P2Y4DT6H8M');
echo 
$interval->format('%d gün');

?>

Yukarıdaki örneğin çıktısı:

4 gün

Örnek 2 - DateInterval ve büyük değerler

<?php

$interval 
= new DateInterval('P32D');
echo 
$interval->format('%d gün');

?>

Yukarıdaki örneğin çıktısı:

32 gün

Örnek 3 - DateInterval ve DateTime::diff() ile %a ve %d değiştiricileri

<?php

$january 
= new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval $february->diff($january);

// %a toplam gün sayısını çıktılar.
echo $interval->format('toplam %a gün')."\n";

// %d ise aya dahil olmayan gün sayısını çıktılar.
echo $interval->format('%m ay, %d gün');

?>

Yukarıdaki örneğin çıktısı:

toplam 31 gün
1 ay, 0 gün



DatePeriod> <DateInterval::createFromDateString
[edit] Last updated: Fri, 23 Mar 2012
 
add a note add a note User Contributed Notes DateInterval::format
balaclark at gmail dot com 02-Jun-2011 03:58
Be aware that your default timezone can sometimes alter the result of a diff so that the returned months/days are incorrect.

There is a bug report at: http://bugs.php.net/bug.php?id=52480
pekka at gmx dot de 13-Feb-2011 03:20
Note that `%a` is broken on Windows on VC6 builds. http://bugs.php.net/bug.php?id=51184
kuzb 04-Feb-2011 05:15
Quick class to allow you to input a time in any unit, and have it recalculate in to different denominations (for example, seconds to hours, minutes and seconds):

<?php
   
class DateIntervalEnhanced extends DateInterval
   
{

     
/* Keep in mind that a year is seen in this class as 365 days, and a month is seen as 30 days.        
         It is not possible to calculate how many days are in a given year or month without a point of 
         reference in time.*/
     
public function to_seconds()
      {
        return (
$this->y * 365 * 24 * 60 * 60) +
               (
$this->m * 30 * 24 * 60 * 60) +
               (
$this->d * 24 * 60 * 60) +
               (
$this->h * 60 *60) +
              
$this->s;
      }
     
      public function
recalculate()
      {
       
$seconds = $this->to_seconds();
       
$this->y = floor($seconds/60/60/24/365);
       
$seconds -= $this->y * 31536000;
       
$this->m = floor($seconds/60/60/24/30);
       
$seconds -= $this->m * 2592000;
       
$this->d = floor($seconds/60/60/24);
       
$seconds -= $this->d * 86400;
       
$this->h = floor($seconds/60/60);
       
$seconds -= $this->h * 3600;
       
$this->i = floor($seconds/60);
       
$seconds -= $this->i * 60;
       
$this->s = $seconds;
      }
    }

   
// Example usage
   
$di = new DateIntervalEnhanced('PT3600S');
   
$di->recalculate();
   
// outputs 1:0:0 instead of 0:0:3600 now!
   
echo $di->format('%H:%i:%s');
?>
baptiste dot place at utopiaweb dot fr 15-Mar-2010 07:47
With php 5.3, DateTime is sweet !
Here is one quick example :

<?php
/**
 * A sweet interval formatting, will use the two biggest interval parts.
 * On small intervals, you get minutes and seconds.
 * On big intervals, you get months and days.
 * Only the two biggest parts are used.
 *
 * @param DateTime $start
 * @param DateTime|null $end
 * @return string
 */
public function formatDateDiff($start, $end=null) {
    if(!(
$start instanceof DateTime)) {
       
$start = new DateTime($start);
    }
   
    if(
$end === null) {
       
$end = new DateTime();
    }
   
    if(!(
$end instanceof DateTime)) {
       
$end = new DateTime($start);
    }
   
   
$interval = $end->diff($start);
   
$doPlural = function($nb,$str){return $nb>1?$str.'s':$str;}; // adds plurals
   
   
$format = array();
    if(
$interval->y !== 0) {
       
$format[] = "%y ".$doPlural($interval->y, "year");
    }
    if(
$interval->m !== 0) {
       
$format[] = "%m ".$doPlural($interval->m, "month");
    }
    if(
$interval->d !== 0) {
       
$format[] = "%d ".$doPlural($interval->d, "day");
    }
    if(
$interval->h !== 0) {
       
$format[] = "%h ".$doPlural($interval->h, "hour");
    }
    if(
$interval->i !== 0) {
       
$format[] = "%i ".$doPlural($interval->i, "minute");
    }
    if(
$interval->s !== 0) {
        if(!
count($format)) {
            return
"less than a minute ago";
        } else {
           
$format[] = "%s ".$doPlural($interval->s, "second");
        }
    }
   
   
// We use the two biggest parts
   
if(count($format) > 1) {
       
$format = array_shift($format)." and ".array_shift($format);
    } else {
       
$format = array_pop($format);
    }
   
   
// Prepend 'since ' or whatever you like
   
return $interval->format($format);
}
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites