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 git.master, git.master_jit, rfc.property-hooks
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" }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
119.62 ms | 406 KiB | 5 Q