'Convert timestamp with dots in PHP ("yyyy.mm.dd hh:nn:ss.zzz")
I'm trying to convert strings with hungarian datetime format, but no success because of the dot-separators:
<?php
$dtime = DateTime::createFromFormat("YY'.'MM'.'DD HH:MM:II frac", "2020.07.22 22:41:36.258");
$timestamp = $dtime->getTimestamp();
echo("Result: " . $timestamp . "<br>");
?>
Isn't it possible without "string-replace" like this:strtotime(preg_replace("/([0-9]{4})\.([0-9]{2})\.([0-9]{2})/",'${1}-${2}-${3}',$xml->delivery_time)) ?
(I'm new to PHP 5 and I'm shocked it can not simply convert a common date format. Searched 200+ results, wasted 4+ hours ... no success.)
Solution 1:[1]
The correct format is stored in the $format variable:
(Note: the v (millisec) modifier has only been added since v7.3)
<?php
$format = 'Y.m.d H:i:s.v';
$dtime = DateTime::createFromFormat($format, "2020.07.22 22:41:36.258");
$timestamp = $dtime->getTimestamp();
echo("Result: " . $timestamp . "<br>");
?>
Result: 1595457696
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
