1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-22 01:48:05 +01:00
php-dibi/examples/date.type.demo.php
2007-03-26 06:22:53 +00:00

78 lines
1.5 KiB
PHP

<?php
require_once '../dibi/dibi.php';
// required since PHP 5.1.0
if (function_exists('date_default_timezone_set'))
date_default_timezone_set('Europe/Prague');
/**
* Pseudotype for UNIX timestamp representation
*/
class TDateTime implements IDibiVariable
{
/**
* Unix timestamp
* @var int
*/
protected $time;
public function __construct($time = NULL)
{
if ($time === NULL)
$this->time = time(); // current time
elseif (is_string($time))
$this->time = strtotime($time); // try convert to timestamp
else
$this->time = (int) $time;
}
/**
* Format for SQL
*
* @param object destination DibiDriver
* @param string optional modifier
* @return string
*/
public function toSQL($driver, $modifier = NULL)
{
return date(
$driver->formats['datetime'], // format according to driver's spec.
$this->time
);
}
}
// CHANGE TO REAL PARAMETERS!
dibi::connect(array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
'password' => 'xxx',
'database' => 'dibi',
'charset' => 'utf8',
));
// generate and dump SQL
dibi::test("
INSERT INTO [mytable]", array(
'A' => 12,
'B' => NULL,
'C' => new TDateTime(31542), // using out class
'D' => 'any string',
));