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));

preferences:
25.31 ms | 404 KiB | 5 Q