将时间字符串转换为UNIX时间戳

将时间字符串转换为UNIX时间戳

问题描述:

How can I convert a time string like this:

30/7/2010

to a UNIX timestamp?

I tried strtotime() but I get a empty string :(

如何转换时间字符串,如下所示: p>

30/7/2010 p> blockquote>

到UNIX时间戳? p>

我试过 strtotime() code>但是我得到一个空字符串:( p> div>

PHP >= 5.3:

$var = DateTime::createFromFormat('j/n/Y','30/7/2010')->getTimestamp();

You probably want to use http://us3.php.net/manual/en/function.strptime.php (strptime) since your date is in a different format than what PHP might be expecting.

You could also convert it to a format that strtotime() can accept, like Y/M/D:

$tmp = explode($date_str);
$converted = implode("/", $tmp[2], $tmp[1], $tmp[0]);
$timestamp = strtotime($converted);

You're using UK date format.

Quick and dirty method:

$dateValues = explode('/','30/7/2010');
$date = mktime(0,0,0,$dateValues[1],$dateValues[0],$dateValues[2]);

The PHP 5.3 answer was great

DateTime::createFromFormat('j/n/Y','30/7/2010')->getTimestamp();

Here is a < 5.3.0 solution

$timestamp = getUKTimestamp('30/7/2010');

function getUKTimestamp($sDate) {
    list($day, $month, $year) = explode('/', $sDate);
    return strtotime("$month/$day/$year");
}