PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Database » DB_DataObject » Bug #3584

Cast::datetime, Cast::time

Details

Request #3584Cast::datetime, Cast::time
Submitted2005-02-24 17:57 UTC
Fromtherion_5 at hotmail dot com
StatusClosed
PackageDB_DataObject
PHP Version4.3.10
OSAny
Roadmaps(Not assigned)

Comments

[2005-02-24 17:57 UTC] therion_5 at hotmail dot com

Description:
------------
Extend Cast class to support DateTime, Time.

Please consider code below, i have made it by analogy to date functionality.

/**
* Time extension
*/
var $hour;
var $minute;
var $second;

/**
*
* accept
* - noargs (now)
* - yyyy-mm-dd HH:MM:SS
* - array(yyyy,mm,dd,HH,MM,SS)
*/
function datetime()
{
$args = func_get_args();
switch(count($args)) {
case 0: // no args = now!
$datetime = date('Y-m-d G:i:s', mktime());
case 1:
if(isset($datetime) == false)
$datetime = $args[0];

$parts = explode(' ', $datetime);

$bits = explode('-', $parts[0]);
$bits = array_merge($bits, explode(':', $parts[1]));

break;
case 1: // one arg = a string

break;
default: // 2 or more..
$bits = $args;
}

if(count($bits) != 6)
return NULL;

// now take data from bits into object fields
$r = new DB_DataObject_Cast;
$r->type = 'datetime';

list($r->year,$r->month,$r->day, $r->hour, $r->minute, $r->second) = $bits;

return $r;
}

/**
*
* accept
* - noargs (now)
* - yyyy-mm-dd HH:MM:SS
* - array(yyyy,mm,dd,HH,MM,SS)
*/
function time()
{
$args = func_get_args();
switch(count($args)) {
case 0: // no args = now!
$time = date('G:i:s', mktime());
case 1:
if(isset($time) == false)
$time = $args[0];

$bits = explode(':', $time);

break;
case 1: // one arg = a string

break;
default: // 2 or more..
$bits = $args;
}

if(count($bits) != 3)
return NULL;

// now take data from bits into object fields
$r = new DB_DataObject_Cast;
$r->type = 'time';

list($r->hour, $r->minute, $r->second) = $bits;

return $r;
}

function toStringFromDateTime($to,$db) {
// first weed out invalid casts..
// in blobs can only be cast to blobs.!
// perhaps we should support TEXT fields???
//

if (($to !== false) && !($to & DB_DATAOBJECT_DATE && $to & DB_DATAOBJECT_TIME)) {
return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a datetime!'.
' (why not just use native features)');
}

return "'{$this->year}-{$this->month}-{$this->day} {$this->hour}:{$this->minute}:{$this->second}'";
}

function toStringFromTime($to,$db) {
// first weed out invalid casts..
// in blobs can only be cast to blobs.!
// perhaps we should support TEXT fields???
//

if (($to !== false) && !($to & DB_DATAOBJECT_TIME)) {
return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a time!'.
' (why not just use native features)');
}

return "'{$this->hour}:{$this->minute}:{$this->second}'";
}

Reproduce code:
---------------