3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface DbAdapterInterface { /** * @param DateTime $date * @return string */ public function convertFromDateTime(DateTime $date); /** * @param DateTime $date * @return array */ public function convertToDateTime(array $row); } class MySqlAdapter implements DbAdapterInterface { public function convertFromDateTime(DateTime $date) { return $date->format('Y-m-d H:i:s'); } public function convertToDateTime(array $row) { return new DateTime($row['date']); } } class OracleAdapter implements DbAdapterInterface { public function convertFromDateTime(DateTime $date) { return $date->getTimestamp(); } public function convertToDateTime(array $row) { return new DateTime('@'.$row['date']); } } class Db { /** @var DbAdapterInterface $adapter */ private $adapter; public function setAdapter(DbAdapterInterface $adapter) { $this->adapter = $adapter; } public function insert($data) { $date = $data['date']; $dateString = $this->adapter->convertFromDateTime($date); // do your insert return $dateString; } public function findById($id) { // fetch row by id here, this is just an example, I'm using the id as the date string $row = ['date' => $id]; $row = $this->adapter->convertToDateTime($row); // do your insert return $row; } } // Example $data = [ 'date' => new DateTime(), ]; $db = new Db(); $db->setAdapter(new MySqlAdapter()); echo $db->insert($data)."\n"; $db->setAdapter(new OracleAdapter()); echo $db->insert($data)."\n"; $time = '1493308146'; var_dump( $db->findById($time)); $db->setAdapter(new MySqlAdapter()); $time = '2014-09-18 22:00:00'; var_dump( $db->findById($time));
Output for 7.0.0 - 7.0.17, 7.0.20, 7.1.0 - 7.1.2, 7.1.4 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.33, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
2017-04-27 17:52:43 1493308363 object(DateTime)#3 (3) { ["date"]=> string(26) "2017-04-27 15:49:06.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } object(DateTime)#4 (3) { ["date"]=> string(26) "2014-09-18 22:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }
Output for 7.3.32 - 7.3.33, 8.0.13
2017-04-27 15:52:43 1493308363 object(DateTime)#3 (3) { ["date"]=> string(26) "2017-04-27 15:49:06.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } object(DateTime)#4 (3) { ["date"]=> string(26) "2014-09-18 22:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
Output for 7.1.3
2017-04-27 17:52:44 1493308364 object(DateTime)#3 (3) { ["date"]=> string(26) "2017-04-27 15:49:06.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } object(DateTime)#4 (3) { ["date"]=> string(26) "2014-09-18 22:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }
Output for 7.0.18
2017-04-27 17:52:42 1493308362 object(DateTime)#3 (3) { ["date"]=> string(26) "2017-04-27 15:49:06.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } object(DateTime)#4 (3) { ["date"]=> string(26) "2014-09-18 22:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }

preferences:
140.03 ms | 411 KiB | 5 Q