I'm working on my CMS, Thacmus, and I've run into one helluva snag (well, nothing big). I'm working on adding the time of day to my date field, since I've procrastinated on that, but I'm having trouble trying to get the time of day from the timestamp without it interfering with the day. I've managed to supress this with the two asterik'd
Here's the code for the two fields, date, and time:
//Date selector
class FieldDate extends Field {
var $fmt, //Format for column display
$time; //Add time field - If it's an object, it'll add that in (but it'll overwrite the name)
function FieldDate($name,$label="",$fmt="",$time=true) {
$this->Field($name,$label);
if (!is_object($time)) $time=new FieldTime("");
if (!$fmt) {
$fmt="m/d/y";
if ($time) $fmt.=" ".$time->fmt;
}
$this->time=$time; $this->time->name=$this->name."[time]";
$this->fmt=$fmt;
}
//Add option (checkbox) for a null date?
function render($value) {
$date=getdate(time()); //Get current date
static $info=array(
"mon" => array(1 => "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"),
"mday" => array(),
"year" => array() );
//Init
if (!count($info["mday"])) {
for ($i=-2;$i<=2;$i++) $info["year"][]=$date["year"]+$i; //2 above, 2 below
for ($i=1;$i<=31;$i++) $info["mday"][]=$i;
}
if (!$value) $value=time();
$date=getdate($value);
$name=$this->name;
$out="";
foreach ($info as $key => $i) {
$c=new FieldCombo($name."[$key]",$i,$key=="mon"); //Only month is by key
$out.=$c->render($date[$key]);
}
if ($this->time) $out.=' '.$this->time->render($value);
return $out;
}
function submit($in,&$out) {
$out=mktime(0,0,0,$in["mon"],$in["mday"],$in["year"]);
if ($this->time) {
$cur=0;
$this->time->submit($in["time"],$cur);
$out+=($cur+16*60*60)%DAY_LEN; // * Another cheap fix - I'm not sure how I can simplify this
}
}
function disp($value) { return date($this->fmt,$value); }
function colWidth() { return 80+($this->time ? 80 : 0); }
}
//Time input
class FieldTime extends FieldText {
var $fmt;
function FieldTime($name,$label="",$fmt="h:i a") {
$this->FieldText($name,$label,15);
$this->fmt=$fmt;
}
function render($value) {
$value=date($this->fmt,$value);
return FieldText::render($value);
}
function submit($in,&$out) {
$out=strtotime($in)%DAY_LEN+60*60; // * Clip to time of day - Adding the extra hour is a cheap fix....
}
function disp($value) { return date($this->fmt,$value); }
}