Petah
Petah

Reputation: 46050

PHP date/time to timestamp (custom format)

Any one got a function to like strtotime() but the takes the format as input?

For example I need to convert yyyymmdd to a timestamp, or perhaps yyyyddmm. So I would like to specify the format used. Also Im on Windows so strptime() isn't an option.

Upvotes: 5

Views: 2950

Answers (2)

fire
fire

Reputation: 21531

Very easy to write a function, just take a look at mktime...

// Assumes yyyy-mm-dd
function fromdate($date) {
    $d = explode("-", $date);
    return mktime(0, 0, 0, $d[1], $d[2], $d[0]);
}

Upvotes: -1

Pekka
Pekka

Reputation: 449415

PHP 5's DateTime class has the createFromFormat method which does what you need.

However, it requires PHP 5.3 so it's not always an option (yet).

Upvotes: 3

Related Questions