Jump to content

Getting time of day from Unix timestamp - somewhat iffy


deadimp

Recommended Posts

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); }
}

Link to comment
Share on other sites

It's not so much the date format that's the problem, but the date timestamp. These are the two lines of interest, the quick fixes:

//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....

Before I added the random fixes to it, the times would change by increments / decrements every submission, thus why I added the extra one / 16 hours. I'd upload an example, but it's on a newer version of the CMS, and I'm not sure about the compatability.

Link to comment
Share on other sites

That whole thing I said about the example... Scratch that. Ends up, I can just make the fields entirely separate, so I did.

To show you why I find this odd, I changed the code to how it originally was, how I felt it should've worked (those two lines):

//FieldDate::submit()
$out+=$cur; // *

//FieldTime::submit()
$out=strtotime($in)%DAY_LEN; // * Clip to time of day

Here's the link: Example

Just click on the submit button a few times, and notice how the time changes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.