Skacc
Skacc

Reputation: 1778

php get timestamp from string when i know the format

Is there a php func that does this:

$timestamp = get_timestamp_from_str('d/m/Y H:i', '10/10/2012 10:10');

strtotime() will use the USA version for '10/10/2012' wich is m/d/Y, but i have the day first.

For a particular case i can make my own parser, but the date format can change depending on the visitors local settings. However, i will always know the current format.

I had to insert this last paragraph so that this question is long and good enough for this portals question quality filter

Thank you

Upvotes: 5

Views: 6942

Answers (1)

nickb
nickb

Reputation: 59709

Yes, it's called DateTime::createFromFormat().

You would use it as:

$datetime = DateTime::createFromFormat( 'd/m/Y H:i', '10/10/2012 10:10', new DateTimeZone('Something'));
$timestamp = $datetime->getTimestamp();

Just make sure your timezone string is in the list of supported timezones.

Upvotes: 10

Related Questions