views:

75

answers:

5

Hi

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 :(

+2  A: 

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.

webdestroya
+1. If you've got PHP >= 5.1 and a date format that doesn't work with strtotime, strptime is definitely an option.
Frank Farmer
A: 

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);
alexantd
If you're going to do that you might as well just `mktime(0, 0, 0, $tmp[1], $tmp[0], $tmp[2]);`
Michael Mrozek
+3  A: 

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]);
Mark Baker
This is what I came up with too -- is there really no function that takes a date format string and a textual date string and returns the timestamp? It seems like a weird gap in PHP's date interface
Michael Mrozek
+5  A: 

PHP >= 5.3:

$var = DateTime::createFromFormat('j/n/Y','30/7/2010')->getTimestamp();
Wrikken
cool, that works, thank you
Alex
A: 

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");
}
Paul Dragoonis