I've a date like Tue Dec 15 2009. How can I convert it into seconds?
Update: How can I convert a date formatted as above to Unix timestamp?
I assume by seconds you mean a UNIX timestamp.
strtotime() should help.
You can use the strtotime
function to convert that date to a timestamp :
$str = 'Tue Dec 15 2009';
$timestamp = strtotime($str);
And, just to be sure, let's convert it back to a date as a string :
var_dump(date('Y-m-d', $timestamp));
Which gives us :
string '2009-12-15' (length=10)
(Which proves strtotime
did understand our date ^^ )
[edit 2012-05-19] as some other questions might point some readers here: Note that strtotime()
is not the only solution, and that you should be able to work with the DateTime
class, which provides some interesting features -- especially if you are using PHP >= 5.3
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).
And DateTime::format()
will allow you to format that object to almost any kind of date format -- including an UNIX Timestamp, as requested here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With