# [web] [php] Unix timestamps, time of day

This topic is 3987 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've ran into a little snag while developing my CMS, which I was able to remedy with two quick, ugly fixes, but I would like to know what I was doing wrong. The problem was that I'm trying to combine FieldTime with FieldDate, but trying to make sure that FieldTime only gives the time of day by using modulus (%), and not just a normal timestamp, has proven to be a hassle. And even without FieldDate, FieldTime is slightly offset after each submission. Here's the code for the two fields in question, FieldDate and FieldTime, and the specific lines are asterik'd:
//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; // *
}
}
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; // * Clip to time of day
}
function disp($value) { return date($this->fmt,$value); } } The code in action: Example Example code: define('ICHECK',true); define('DAY_LEN',24*60*60); include("field.php");$obj=(object) array("time"=>time(), "date"=>time());
$field=array( new FieldDate("date"), new FieldTime("time") );$form=new Form($obj,$field,$_SERVER["REQUEST_URI"]);$form->exec();
echo "<br>";
print_r($obj); I felt like that should have worked, but it didn't. I've managed to remedy this weird little offset thing with these changes: //FieldDate::submit()$out+=($cur+16*60*60)%DAY_LEN; // * Another cheap fix - I'm not sure how I can simplify this //FieldTime::submit()$out=strtotime(\$in)%DAY_LEN+60*60; // * Clip to time of day - Adding the extra hour is a cheap fix....

However, I feel that there is something I'm missing, something I could simplify and make it work. I'd explain more, but I'm in kind of a hurry.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631425
• Total Posts
3000015
×