PHP: strtotime daha iyi tarih çözümleyici

5 Cevap

Belirli bir biçimde bir dize ayrıştırmak çalışıyorum ve ben gerçekten bunu yapmak için iyi bir işlevi bulamıyorum keşfetmek için şaşırdım.

The only one I found is strtotime and it doesn't fit as it guesses the date format. I really don't trust the "guess" part. Moreover my string input is in a french format (dd/mm/aaaa) which it seems that it's not well understood (it parses american formats like mm/dd/aaaa).

What I'm looking for is a function that take in input a date string and a format to parse. I could do it myself with a regexp but I can't believe that it doesn't already exist.

Buldum:

  • DateTime::createFromFormat(). Ama bu sadece PHP 5.3 ile çalışmak ve ben PHP sürümü (5.2) yükseltme gücü yok
  • strptime(). Bu yöntem, ne istediğiniz ama windows platformunda uygulanmadı (arada:? WTF)

Herhangi bir öneri?

5 Cevap

Ne yazık ki, bu tür ayrıştırma bölü çizgilerinde dize patlayan ve daha sonra gün ve ay geçiş yaparak, manually daha bitti gibi görünüyor.

Eğer bir tarih zaman biçimini belirlemenize olanak tanır ki, Zend_Date göz atın. Yanı sıra, çok yapabilirsiniz specify your own constants for many common formats dahil.

$date = new Zend_Date();
$date->set('27/08/2009','DD/MM/YYYY');

Strtotime üzerinde php.net itibaren aşağıdaki yorum yardımcı olabilir:

Fails for non-US dates where the ordering is uncertain, such as 01/02/2003 - parses this as Feb 1st, rather than Jan 2nd.

If you are parsing dates for a non-US locale, you can flip these elements of your date:

<?php 
$y = $_POST['date']; 
if (preg_match('/^\s*(\d\d?)[^\w](\d\d?)[^\w](\d{1,4}\s*$)/', $y, $match)) { 
  $y = $match[2] . '/' . $match[1] . '/' . $match[3]; 
} 
echo date('d # m # Y', strtotime($y)); 
?>

WARNING: Above only works for dates, and breaks for times: 12:30:01 will be converted to 30/12/01.

Ben bir sınıf kendim yazdım, seni http://sourceforge.net/projects/phpdbedittk içinde gadmdatecommand.php Tamam bir sürümünü bulabilirsiniz düşünüyorum

Sadece '/' ile patlayacak ve numarasını, onun oldukça basit değil takas için burada yorumlar gelince. Eğer bir giriş kutusuna tarihleri ​​girmek için teklif ederseniz, alabilirsiniz - kullanıcı yerellik ve uygulamaya bağlı

1/7/2010 1.7.2010 1-7-2010 15 Jul 1 Jul 2010 1/6/8

and many more variations. I've solved this problem (at least for me successfully) by creating dateformats, each of which have a) a regex that matches the format b) an array mapper that matches regex brackets into date pieces (day, month, minute, am/pm) c) an output format for date()

HTH

Eğer tarih formatı giriş İngilizce-biçimlendirilmiş olacak biliyorum, o zaman bir daha standart tarih formatı içine işleyebilir. 24/7/2007 için basit bir ayrıştırma 2007-07-24 saçmadır. Eğik çizgi ile patlayabilir ve doğru yerde parçaları yerleştirdim. I strtotime 2007-07-24 doğru ayrıştırma edecek bir gerçeği biliyorum.