PHP: convert date in seconds?

I have a date like W. December 15, 2009 . How can I convert it in seconds?

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

+3
source share
3 answers

I assume in seconds you mean a UNIX timestamp .

strtotime () should help.

+25
source

You can use the function strtotimeto convert this date to a timestamp:

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

And to be sure, return it to the date as a string:

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

What gives us:

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

(which proves strtotimeunderstood our date ^^)



[edit 2012-05-19], : , strtotime() - , DateTime, - PHP >= 5.3


- :

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


DateTime::createFromFormat() DateTime , , , , ( PHP >= 5.3).

DateTime::format() - UNIX, .

+15

UNIX- ? :

echo strtotime('Tue Dec 15 2009');
+5

Source: https://habr.com/ru/post/1726177/


All Articles