PHP strtotime () function that accepts the format?

strtotime() works fine if you can provide it with a date format that it understands and can convert, but, for example, you give it a date in the UK, it cannot give the correct unix timestamp.

Is there any PHP function, official or unofficial, that can accept a format variable that tells the function in which format the date and time are transmitted?

The closest I came to is a mixture of date_parse_from_format() and mktime()

 // Example usage of the function I'm after //Like the date() function but in reverse $timestamp = strtotimeformat("03/05/2011 16:33:00", "d/m/YH:i:s"); 
+6
source share
3 answers

If you have PHP 5.3:

 $date = DateTime::createFromFormat('d/m/YH:i:s', '03/05/2011 16:33:00'); echo $date->getTimestamp(); 
+21
source

You are looking for strptime , I think. you can use it to parse the date and then use mktime if you need a UNIX timestamp.

 function strotimeformat($date, $format) { $d = strptime($date, $format); return mktime($d['tm_hour'], $d['tm_min'], $d['tm_sec'], $d['tm_mon'], $d['tm_mday'], $d['tm_year']); } 

This will work with PHP 5.1 onwards.

+2
source

strtotime is set to US date / time when using / as a delimiter. To make him think that this is the date / time of the euro, use - or . as a date separator. You can change / to - or . using simple str_replace()

0
source

Source: https://habr.com/ru/post/887208/


All Articles