• Advertisement
Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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) {
		if (!is_object($time)) $time=new FieldTime("");
		if (!$fmt) {
			if ($time) $fmt.=" ".$time->fmt;
		$this->time=$time; $this->time->name=$this->name."[time]";
	//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() );
		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();
		foreach ($info as $key => $i) {
			$c=new FieldCombo($name."[$key]",$i,$key=="mon"); //Only month is by key
		if ($this->time) $out.=' '.$this->time->render($value);
		return $out;
	function submit($in,&$out) {
		if ($this->time) {
			$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") {
	function render($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:

$obj=(object) array("time"=>time(), "date"=>time());
 new FieldDate("date"),
 new FieldTime("time") );
$form=new Form($obj,$field,$_SERVER["REQUEST_URI"]);
echo "<br>";
I felt like that should have worked, but it didn't. I've managed to remedy this weird little offset thing with these changes:
$out+=($cur+16*60*60)%DAY_LEN; // * Another cheap fix - I'm not sure how I can simplify this

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement