PHP: saniye içine tarih dönüştürebilirsiniz?

3 Cevap

I Tue Dec 15 2009 gibi bir tarih var. Ben Nasıl saniye çevirebilirim?

Update: How can I convert a date formatted as above to Unix timestamp?

3 Cevap

Ben bir UNIX timestamp Yani saniye varsayalım.

strtotime() yardımcı olmalıdır.

Sen bir zaman damgası için bu tarihi dönüştürmek için strtotime işlevini kullanabilirsiniz:

$str = 'Tue Dec 15 2009';
$timestamp = strtotime($str);

Ve, sadece emin olmak için, en bir dizge olarak tarihe geri dönüştürmek izin:

var_dump(date('Y-m-d', $timestamp));

Bize verir:

string '2009-12-15' (length=10)

(Which proves strtotime) bizim tarih ^ ^ anladın



Eğer> = 5.3 PHP kullanıyorsanız, özellikle - [edit 2012-05-19] as some other questions might point some readers here: bazı ilginç özellikler sunar strtotime() is not the only solution, and that you should be able to work with the DateTime class, Not


In this case, you could use something like the following portion of code :

$str = 'Tue Dec 15 2009';
$format = 'D F d Y';
$dt = DateTime::createFromFormat($format, $str);
$timestamp = $dt->format('U');


DateTime::createFromFormat() allows one to create a DateTime object from almost any date, no matter how it's formated, as you can specify the format you date's in (This method is available with PHP >= 5.3).

Burada istendiği gibi, bir UNIX Timestamp dahil olmak üzere - ve DateTime::format() , tarih formatı hemen hemen her türlü bu nesneyi biçimlendirmek için izin verecektir.

Bir UNIX zaman damgası gibi mi? Deneyin:

echo strtotime('Tue Dec 15 2009');