Source for file DateTime.php
Documentation is available at DateTime.php
* A Structures_Form element for a set of date and time dropdown boxes.
* This element is a composite element made of several Gtk2TextSelect elements.
* This class implement Structures_Form_ElementInterface.
* @package Structures_Form_Gtk2
* @copyright Copyright 2006 Scott Mattocks
* Constants for the element segments.
* The Structures_Form object.
* The increments to display for minutes.
* @param object $form The Structures_Form.
* @param string $label The string to be used as the label.
* @param string $date The default date (optional).
* @param array $order The elements to display and the order to
* display them. (optional).
* @param integer $yearStart The first year for the year element
* @param integer $duration The number of years to show (optional).
* @param array $size The height and width of the entry (optional).
* @param object $style The GtkStyle to apply to the elements
public function __construct(Structures_Form $form, $label, $date = null ,
$order = null , $yearStart = null ,
$duration = null , $minuteIncrements = null ,
$size = null , $style = null )
// Call the parent constructor.
// Set the size if it was given.
$this->set_size_request ($size[0 ], $size[1 ]);
// Set the style if it was given.
if ($style instanceof GtkStyle ) {
$this->set_style ($style);
// Set the display elements and order.
$order = array (self ::ELEMENT_MONTH ,
// Create the individual elements.
$duration = self ::DEFAULT_DURATION;
$minuteIncrements = self ::DEFAULT_INCREMENTS;
// Set the date. Default to now.
// Pack the elements in the order they should appear.
foreach ($order as $element) {
$this->pack_start ($this->$element, false , false , 3 );
* Creates the individual date elements.
* This element is made up of month, day and year elements.
* @param integer $yearStart The first year to show.
* @param integer $duration The number of years to show.
* @param boolean $twelveHour true to use 12 hour format.
protected function createElements($yearStart, $duration, $twelveHour = false )
require_once 'Structures/Form/Element/Gtk2/TextSelect.php';
// Create the month element.
$this->month = new Structures_Form_Element_Gtk2_TextSelect ($this->form,
for ($i = 1; $i <= 12; ++ $i) {
// Use date to get local month names.
// Create the day element.
$this->day = new Structures_Form_Element_Gtk2_TextSelect ($this->form,
for ($i = 1; $i <= 31; ++ $i) {
$this->day->addOption ($i, $i);
// Create the year element.
$this->year = new Structures_Form_Element_Gtk2_TextSelect ($this->form,
for ($i = 0; $i < $duration; ++ $i) {
$this->year->addOption ($yearStart + $i, $yearStart + $i);
// Create the hour element.
$this->hour = new Structures_Form_Element_Gtk2_TextSelect ($this->form,
for ($i = $start; $i <= $limit; ++ $i) {
// Figure out what to show.
$this->hour->addOption (str_pad($i, 2 , '0', STR_PAD_LEFT ), $i);
// Create the minute element.
$this->minute = new Structures_Form_Element_Gtk2_TextSelect ($this->form,
$mins = str_pad($i, 2 , '0', STR_PAD_LEFT );
$this->minute->addOption ($mins, $mins);
// Create the seconds element.
$this->second = new Structures_Form_Element_Gtk2_TextSelect ($this->form,
for ($i = 0; $i <= 59; ++ $i) {
// Create the AMPM element.
$this->ampm = new Structures_Form_Element_Gtk2_TextSelect ($this->form,
$this->ampm->addOption (0 , 'AM');
$this->ampm->addOption (1 , 'PM');
* Sets an element's value.
* This method should set the value of the widget not just set some data
* that is retrieved later. If the widget is a GtkEntry, this method should
* call set_text(). If the widget is a GtkComboBox, this method should set
* To set the value of this element, we must set the value of the three
* composite elements. To do this, we first turn the timestamp into a date
* string and then explode on '-'. This is faster than calling date() three
* @param string $value The text to put in the entry.
* @return boolean true if the value was changed.
// Make sure the value is a number
// Turn the value into a string representation.
$date = date('Y-m-d', $value);
// Now break the date string up into pieces.
list ($year, $month, $day) = explode('-', $date);
// Finally set the three elements.
$success = $success && $this->month->setValue ($month);
$success = $success && $this->day->setValue ($day);
$success = $success && $this->year->setValue ($year);
// Turn the value into a string representation for the time part.
$time = date('H-i-s', $value);
// Now break the date string up into pieces.
list ($hour, $minute, $second) = explode('-', $time);
// Finally set the four time elements.
$success = $success && $this->hour->setValue ($hour);
$success = $success && $this->minute->setValue ($minute);
$success = $success && $this->second->setValue ($second);
$success = $success && $this->ampm->setValue ((int) ($hour > 11 ));
* Returns element's value.
* This method should return the widget's value not just some data from the
* widget (i.e. set with set_data()). For example if the widget is a
* GtkEntry, this method should call get_text(). If the widget is a
* GtkComboBox, this method should return the value of the column
* identified when the element was constructed for the given row.
$this->month->getValue (),
* Clears the current value of the element.
* This method should clear the current value if possible. For example, if
* the widget is a GtkEntry, this method should pass null to set_text(). If
* the value could not be cleared for some reason (the item is frozen or it
* is not possible to clear the value (selection type = browse)) this
* method should return false.
* It may not be possible to clear this element type. If the children
* cannot be cleared, this element cannot be cleared.
* @return boolean true if the value was cleared.
return ($this->month->clearValue () &&
$this->day->clearValue () &&
$this->year->clearValue () &&
$this->hour->clearValue () &&
$this->minute->clearValue () &&
* Returns the element type.
* This method must return a string identifying the element type, such as
* text, password, submit, etc.
* @return string The element type.
* This method exists to maintain consistency in the interface. It should
* simply call set_name which is a GtkWidget method and should be avialable
* Returns the element's name.
* This method exists to maintain consistency in the interface. It should
* simply call get_name which is a GtkWidget method and should be available
return $this->get_name ();
* Freezes the element so that its value may not be changed.
* Again this method exists only to maintain consistency in the interface.
* It should just pass false to set_sensitive().
* To make life easier down the road this method should also call
* set_data('frozen', true);
// Make the widget insensitive.
$this->set_sensitive (false );
// Set a data value for ease of programming later.
$this->set_data ('frozen', true );
* Unfreezes the element so that its value may not be changed.
* Again this method exists only to maintain consistency in the interface.
* It should just pass true to set_sensitive().
* To make life easier down the road this method should also call
* set_data('frozen', false);
// Make the widget sensitive.
$this->set_sensitive (true );
// Set a data value for ease of programming later.
$this->set_data ('frozen', false );
* Returns whether or not the element is currently frozen.
* This method should just return the value from get_data('frozen')
return (bool) $this->get_data ('frozen');
* Sets the GtkLabel that identifies the element.
$this->set_data ('label', $label);
* Returns the GtkLabel that identifies the element.
return $this->get_data ('label');
* Adds an event handler for the element.
* @param string $eventName The name of the event.
* @param mixed $callback The callback to call when the event occurs.
* @return array An array of identifiers, one for each piece.
return array ($this->month->addEventHandler ($eventName, $callback),
$this->day->addEventHandler ($eventName, $callback),
$this->year->addEventHandler ($eventName, $callback),
$this->hour->addEventHandler ($eventName, $callback),
$this->minute->addEventHandler ($eventName, $callback),
$this->second->addEventHandler ($eventName, $callback)
Documentation generated on Mon, 11 Mar 2019 14:44:12 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|