Php get timestamp from string when i know format

Is there a php function that does this:

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

strtotime () will use the US version for '10 / 10/2012 ', which is m / d / Y, but I have the first day.

In a specific case, I can make my own parser, but the date format may change depending on the local settings of visitors. 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

+3
source share
1 answer

Yes, he is called DateTime::createFromFormat().

You would use it like:

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

Just make sure your time zone line is in the list of supported time zones .

+8
source

All Articles