|
Problem This is in response to Manar's post titled "select a week from GtkCalendar".
She would like to be able to select a week from GtkCalendar as shown below.

Solution
- We use almost entirely the same code from How to let user enter date with a popup calendar - Part 2?
- We then use the php function
strtotime to help us calculate the date of the start of week and end of week.
- Note that in this example, the week starts from Monday. If you prefer the week to start from Sunday, you can easily modify the code.
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| <?php $window = new GtkWindow(); $window->set_size_request(400, 150); $window->set_position(Gtk::WIN_POS_CENTER_ALWAYS); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Press F1 to select a week"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 40); $vbox->pack_start($title, 0, 0);
$hbox = new GtkHBox(); $vbox->pack_start($hbox, 0, 0); $hbox->pack_start(new GtkLabel("Select Date: "), 0); $hbox->pack_start($entry = new GtkEntry(), 0);
$hbox = new GtkHBox(); $vbox->pack_start($hbox, 0, 0); $hbox->pack_start(new GtkLabel("Week Start: "), 0); $hbox->pack_start($week_start_label = new GtkLabel(), 0);
$hbox = new GtkHBox(); $vbox->pack_start($hbox, 0, 0); $hbox->pack_start(new GtkLabel("Week End: "), 0); $hbox->pack_start($week_end_label = new GtkLabel(), 0);
$hbox = new GtkHBox(); $vbox->pack_start($hbox, 0, 0); $hbox->pack_start(new GtkLabel("Week Number: "), 0); $hbox->pack_start($week_num = new GtkLabel(), 0);
// let us know when user press F1 key
$entry->connect('key-press-event', 'on_keypress', $entry); $window->show_all(); Gtk::main();
function on_keypress($widget, $event, $entry) { // we only want F1. return all the rest
if ($event->keyval!=Gdk::KEY_F1) return false; $selected_date = get_date();
$w = date('w', strtotime($selected_date)); // note 1
if ($w==0) $n1 = 6; // it's a Sunday
else $n1 = $w - 1; $week_start = date('m/d/Y', strtotime("$selected_date - $n1 days")); // note 2
if ($w==0) $n2 = 0; else $n2 = 7 - $w; $week_end = date('m/d/Y', strtotime("$selected_date + $n2 days")); // note 3
global $week_start_label, $week_end_label, $week_num; $entry->set_text(date('m/d/Y', strtotime($selected_date))); $week_start_label->set_text($week_start . ' (Monday)'); $week_end_label->set_text($week_end . ' (Sunday)'); $week_num->set_text(date('W', strtotime($selected_date))); $entry->grab_focus(); }
function get_date() { $getdate_dialog = new GetDate(); $date = $getdate_dialog->calendar->get_date(); $selected_date = $date[0].'/'.(1+$date[1]).'/'.$date[2]; return $selected_date; }
class GetDate{
var $calendar;
function GetDate() { $dialog = new GtkDialog('Select Week', null, Gtk::DIALOG_MODAL); $dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS); $top_area = $dialog->vbox; $top_area->pack_start($hbox = new GtkHBox()); setlocale(LC_ALL, 'english'); $this->calendar = new GtkCalendar(); $top_area->pack_start($this->calendar, 0, 0); $this->calendar->set_display_options(Gtk::CALENDAR_SHOW_HEADING| Gtk::CALENDAR_SHOW_DAY_NAMES|
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation We make use of the code from How to let user enter date with a popup calendar - Part 2?
What's new here:
- Get the numeric representation of the day of the week. Sunday=0, Monday=1, etc.
- Get the start of week.
- Get the end of week.
Related Links
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |