{"id":148,"date":"2017-08-07T09:29:52","date_gmt":"2017-08-07T15:29:52","guid":{"rendered":"http:\/\/www.polysyncronism.com\/wordpress\/?p=148"},"modified":"2017-08-07T09:34:05","modified_gmt":"2017-08-07T15:34:05","slug":"fuelphp-framework-jquery-datetimepicker","status":"publish","type":"post","link":"http:\/\/www.polysyncronism.com\/wordpress\/2017\/08\/07\/fuelphp-framework-jquery-datetimepicker\/","title":{"rendered":"FuelPHP framework jQuery DateTimePicker"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.polysyncronism.com\/wordpress\/wp-content\/uploads\/2017\/08\/fuel_logo_light-300x76.png\" alt=\"\" width=\"300\" height=\"76\" class=\"alignleft size-medium wp-image-155\" srcset=\"http:\/\/www.polysyncronism.com\/wordpress\/wp-content\/uploads\/2017\/08\/fuel_logo_light-300x76.png 300w, http:\/\/www.polysyncronism.com\/wordpress\/wp-content\/uploads\/2017\/08\/fuel_logo_light-768x194.png 768w, http:\/\/www.polysyncronism.com\/wordpress\/wp-content\/uploads\/2017\/08\/fuel_logo_light.png 828w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/>I have recently started learning about the FuelPHP framework. I have been working on understanding how to use the framework as intended by the creators, and I think I&#8217;m doing a fair job of that, except possibly with regards to using external jQuery libraries.<\/p>\n<p>I have a simple CRUD application that I built  as a proof of concept for using a third party jQuery DateTimePicker library.<\/p>\n<p>This is the <a href=\"https:\/\/github.com\/xdan\/datetimepicker\" target=\"_blank\">jQuery DateTimePicker library<\/a> that I found and used.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.polysyncronism.com\/wordpress\/wp-content\/uploads\/2017\/08\/dtpsample_folders-300x164.jpg\" alt=\"\" width=\"300\" height=\"164\" class=\"alignnone size-medium wp-image-149\" srcset=\"http:\/\/www.polysyncronism.com\/wordpress\/wp-content\/uploads\/2017\/08\/dtpsample_folders-300x164.jpg 300w, http:\/\/www.polysyncronism.com\/wordpress\/wp-content\/uploads\/2017\/08\/dtpsample_folders.jpg 760w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><\/p>\n<p>This is not including the public asset folder that contains the actual Javascript and CSS.<\/p>\n<p><strong>Model<\/strong><br \/>\n<strong>fuel\/app\/classes\/model\/dtpsample.php<\/strong><\/p>\n<pre class=\"lang:php decode:true \" title=\"Model - fuel\/app\/classes\/model\/dtpsample.php\" >&lt;?php\r\nclass Model_Dtpsample extends Model_Crud\r\n{\r\n\tprotected static $_table_name = 'dtpsample';\r\n\t\r\n\tpublic static function validate($factory)\r\n\t{\r\n\t\t$val = Validation::forge($factory);\r\n\t\t$val-&gt;add_field('name', 'Name', 'required|max_length[255]');\r\n\t\t$val-&gt;add_field('event_date', 'Event Date', 'required');\r\n\t\t\r\n\r\n\t\treturn $val;\r\n\t}\r\n\r\n}<\/pre>\n<p><strong>View Template<\/strong><br \/>\n<strong>fuel\/app\/views\/template_dtpsample.php<\/strong><\/p>\n<p>I want to specifically point out this piece of code. The main thing to notice is Asset::js and the JavaScript code.<\/p>\n<pre class=\"lang:xhtml decode:true \" title=\"View Template - fuel\/app\/views\/template_dtpsample.php\" >&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n\t&lt;meta charset=\"utf-8\"&gt;\r\n\t&lt;title&gt;&lt;?php echo $title; ?&gt;&lt;\/title&gt;\r\n\t&lt;?php echo Asset::css('bootstrap.css'); ?&gt;\r\n\t&lt;?php echo Asset::css('jquery.datetimepicker.css'); ?&gt;\r\n\t\r\n\t\r\n\t&lt;style&gt;\r\n\t\tbody { margin: 40px; }\r\n\r\n\t&lt;\/style&gt;\r\n\t\r\n\t&lt;?php \r\n\t     echo Asset::js(array(\r\n\t\t'\/\/cdnjs.cloudflare.com\/ajax\/libs\/jquery\/1.8.3\/jquery.min.js',\r\n\t\t'bootstrap.js',\r\n\t\t'jquery.datetimepicker.js',\r\n\t\t'moment.js',\r\n\t\t'moment-timezone.js'\r\n\t)); \r\n\t?&gt;\r\n\t\r\n\t\r\n\t\r\n\t&lt;script type=\"text\/javascript\"&gt;\r\n  $(function() {\r\n      \r\n    $(\"#form_name\").focus();  \r\n\r\n    $('#form_event_date').datetimepicker({\r\n        formatTime:'g:i A', \r\n        \/\/format:'Y-m-d h:i A',\r\n        format:'unixtime',\r\n        step:30,\r\n        inline:true,\r\n        onChangeDateTime:function(dp,$input){\r\n    \/\/$(\"#form_event_date\").val($input.val());\r\n    \r\n    var strDate = moment.unix($input.val()).format(\"YYYY-MM-DD h:mm a\");\r\n    \/\/var dtDate = new Date($input.val() * 1000).format('Y-m-d h:i:s a')\r\n    $(\"#dtResult\").val(strDate);\r\n\r\n  }\r\n    });\r\n    \r\n    \r\n  \r\n\r\n  });\r\n  \r\n  \r\n  \r\n&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\t&lt;div class=\"container\"&gt;\r\n\t\t&lt;div class=\"col-md-12\"&gt;\r\n\t\t\t&lt;h1&gt;&lt;?php echo $title; ?&gt;&lt;\/h1&gt;\r\n\t\t\t&lt;hr&gt;\r\n&lt;?php if (Session::get_flash('success')): ?&gt;\r\n\t\t\t&lt;div class=\"alert alert-success\"&gt;\r\n\t\t\t\t&lt;strong&gt;Success&lt;\/strong&gt;\r\n\t\t\t\t&lt;p&gt;\r\n\t\t\t\t&lt;?php echo implode('&lt;\/p&gt;&lt;p&gt;', e((array) Session::get_flash('success'))); ?&gt;\r\n\t\t\t\t&lt;\/p&gt;\r\n\t\t\t&lt;\/div&gt;\r\n&lt;?php endif; ?&gt;\r\n&lt;?php if (Session::get_flash('error')): ?&gt;\r\n\t\t\t&lt;div class=\"alert alert-danger\"&gt;\r\n\t\t\t\t&lt;strong&gt;Error&lt;\/strong&gt;\r\n\t\t\t\t&lt;p&gt;\r\n\t\t\t\t&lt;?php echo implode('&lt;\/p&gt;&lt;p&gt;', e((array) Session::get_flash('error'))); ?&gt;\r\n\t\t\t\t&lt;\/p&gt;\r\n\t\t\t&lt;\/div&gt;\r\n&lt;?php endif; ?&gt;\r\n\t\t&lt;\/div&gt;\r\n\t\t&lt;div class=\"col-md-12\"&gt;\r\n&lt;?php echo $content; ?&gt;\r\n\t\t&lt;\/div&gt;\r\n\t\t&lt;footer&gt;\r\n\t\t\t&lt;p class=\"pull-right\"&gt;Page rendered in {exec_time}s using {mem_usage}mb of memory.&lt;\/p&gt;\r\n\t\t\t&lt;p&gt;\r\n\t\t\t\t&lt;a href=\"http:\/\/fuelphp.com\"&gt;FuelPHP&lt;\/a&gt; is released under the MIT license.&lt;br&gt;\r\n\t\t\t\t&lt;small&gt;Version: &lt;?php echo e(Fuel::VERSION); ?&gt;&lt;\/small&gt;\r\n\t\t\t&lt;\/p&gt;\r\n\t\t&lt;\/footer&gt;\r\n\t&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>Screenshot of the DateTimeStamp picker on the page.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.polysyncronism.com\/wordpress\/wp-content\/uploads\/2017\/08\/dtpsample_Create-300x258.jpg\" alt=\"\" width=\"300\" height=\"258\" class=\"alignnone size-medium wp-image-150\" srcset=\"http:\/\/www.polysyncronism.com\/wordpress\/wp-content\/uploads\/2017\/08\/dtpsample_Create-300x258.jpg 300w, http:\/\/www.polysyncronism.com\/wordpress\/wp-content\/uploads\/2017\/08\/dtpsample_Create.jpg 743w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><\/p>\n<p><strong>Views<\/strong><br \/>\n<strong>fuel\/app\/views\/dtpsample\/_form.php<\/strong><\/p>\n<pre class=\"lang:php decode:true \" title=\"Views - fuel\/app\/views\/dtpsample\/_form.php\" >&lt;?php echo Form::open(array(\"class\"=&gt;\"form-horizontal\")); ?&gt;\r\n\r\n\t&lt;fieldset&gt;\r\n\t\t&lt;div class=\"form-group\"&gt;\r\n\t\t\t&lt;?php echo Form::label('Name', 'name', array('class'=&gt;'control-label')); ?&gt;\r\n\r\n\t\t\t\t&lt;?php echo Form::input('name', Input::post('name', isset($dtpsample) ? $dtpsample-&gt;name : ''), array('class' =&gt; 'col-md-4 form-control', 'placeholder'=&gt;'Name')); ?&gt;\r\n\t\t&lt;\/div&gt;\r\n\t\t&lt;div class=\"form-group\"&gt;\r\n\t\t    &lt;?php echo Form::label('DateTime', 'event_date', array('class'=&gt;'control-label')); ?&gt;&lt;br&gt;\r\n\t\t    &lt;input id=\"dtResult\" size=\"20\" type = \"text\" readonly\/&gt;&lt;br&gt;   \r\n\t\t    &lt;?php echo Form::input('event_date', Input::post('event_date', isset($dtpsample) ? $dtpsample-&gt;event_date : ''), array('class' =&gt; 'col-md-4 form-control', 'placeholder'=&gt;'DateTime')); ?&gt;\r\n\t\t&lt;\/div&gt;\r\n\t\t\r\n\t\t&lt;div class=\"form-group\"&gt;\r\n\t\t\t&lt;label class='control-label'&gt;&amp;nbsp;&lt;\/label&gt;\r\n\t\t\t&lt;?php echo Form::submit('submit', 'Save', array('class' =&gt; 'btn btn-primary')); ?&gt;\t\t&lt;\/div&gt;\r\n\t&lt;\/fieldset&gt;\r\n&lt;?php echo Form::close(); ?&gt;<\/pre>\n<pre class=\"lang:php decode:true \" title=\"Views - fuel\/app\/views\/dtpsample\/create.php\" >&lt;h2&gt;New dtpsample&lt;\/h2&gt;\r\n&lt;br&gt;\r\n\r\n&lt;?php echo render('dtpsample\/_form'); ?&gt;\r\n\r\n\r\n&lt;p&gt;&lt;?php echo Html::anchor('dtpsample', 'Back'); ?&gt;&lt;\/p&gt;<\/pre>\n<pre class=\"lang:php decode:true \" title=\"Views - fuel\/app\/views\/dtpsample\/edit.php\" >&lt;h2&gt;Editing dtsample&lt;\/h2&gt;\r\n&lt;br&gt;\r\n\r\n&lt;?php echo render('dtpsample\/_form'); ?&gt;\r\n&lt;p&gt;\r\n\t&lt;?php echo Html::anchor('dtpsample\/view\/'.$dtpsample-&gt;id, 'View'); ?&gt; |\r\n\t&lt;?php echo Html::anchor('dtpsample', 'Back'); ?&gt;&lt;\/p&gt;<\/pre>\n<pre class=\"lang:php decode:true \" title=\"Views - fuel\/app\/views\/dtpsample\/index.php\" >&lt;h2&gt;dtpsample&lt;\/h2&gt;\r\n&lt;br&gt;\r\n&lt;?php if ($dtpsamples): ?&gt;\r\n&lt;table class=\"table table-striped\"&gt;\r\n\t&lt;thead&gt;\r\n\t\t&lt;tr&gt;\r\n\t\t\t&lt;th&gt;Name&lt;\/th&gt;\r\n\t\t\t&lt;th&gt;Event Date&lt;\/th&gt;\r\n\t\t\t&lt;th&gt;&lt;\/th&gt;\r\n\t\t&lt;\/tr&gt;\r\n\t&lt;\/thead&gt;\r\n\t&lt;tbody&gt;\r\n\t    \r\n&lt;?php date_default_timezone_set('America\/Chicago'); ?&gt;\t    \r\n&lt;?php foreach ($dtpsamples as $item): ?&gt;\t\t&lt;tr&gt;\r\n\r\n\t\t\t&lt;td&gt;&lt;?php echo $item-&gt;name; ?&gt;&lt;\/td&gt;\r\n\t\t\t&lt;td&gt;&lt;?php echo date('Y-m-d g:i:s A', $item-&gt;event_date); ?&gt;&lt;\/td&gt;\r\n\t\t\t\r\n\t\t\t&lt;td&gt;\r\n\t\t\t\t&lt;?php echo Html::anchor('dtpsample\/view\/'.$item-&gt;id, 'View'); ?&gt; |\r\n\t\t\t\t&lt;?php echo Html::anchor('dtpsample\/edit\/'.$item-&gt;id, 'Edit'); ?&gt; |\r\n\t\t\t\t&lt;?php echo Html::anchor('dtpsample\/delete\/'.$item-&gt;id, 'Delete', array('onclick' =&gt; \"return confirm('Are you sure?')\")); ?&gt;\r\n\r\n\t\t\t&lt;\/td&gt;\r\n\t\t&lt;\/tr&gt;\r\n&lt;?php endforeach; ?&gt;\t&lt;\/tbody&gt;\r\n&lt;\/table&gt;\r\n\r\n&lt;?php else: ?&gt;\r\n&lt;p&gt;No dtsamples.&lt;\/p&gt;\r\n\r\n&lt;?php endif; ?&gt;&lt;p&gt;\r\n\t&lt;?php echo Html::anchor('dtpsample\/create', 'Add new dtsample', array('class' =&gt; 'btn btn-success')); ?&gt;\r\n\r\n&lt;\/p&gt;\r\n\r\n<\/pre>\n<p><strong>Controller<\/strong><br \/>\n<strong>fuel\/app\/classes\/controller\/dtpsample.php<\/strong><\/p>\n<pre class=\"lang:php decode:true \" title=\"Controller - fuel\/app\/classes\/controller\/dtpsample.php\" >&lt;?php\r\nclass Controller_Dtpsample extends Controller_Template\r\n{\r\n\r\n    public $template = 'template_dtpsample';\r\n\r\n\tpublic function action_index()\r\n\t{\r\n\t\t$data['dtpsamples'] = Model_Dtpsample::find_all();\r\n\t\t$this-&gt;template-&gt;title = \"DateTimePicker Sample\";\r\n\t\t$this-&gt;template-&gt;content = View::forge('dtpsample\/index', $data);\r\n\r\n\t}\r\n\r\n\tpublic function action_view($id = null)\r\n\t{\r\n\t\tis_null($id) and Response::redirect('dtpsample');\r\n\r\n\t\t$data['dtpsample'] = Model_Dtpsample::find_by_pk($id);\r\n\r\n\t\t$this-&gt;template-&gt;title = \"DateTimePicker Sample\";\r\n\t\t$this-&gt;template-&gt;content = View::forge('dtpsample\/view', $data);\r\n\r\n\t}\r\n\r\n\tpublic function action_create()\r\n\t{\r\n\t\tif (Input::method() == 'POST')\r\n\t\t{\r\n\t\t\t$val = Model_Dtpsample::validate('create');\r\n\r\n\t\t\tif ($val-&gt;run())\r\n\t\t\t{\r\n\t\t\t\t$dtpsample = Model_Dtpsample::forge(array(\r\n\t\t\t\t\t'name' =&gt; Input::post('name'),\r\n\t\t\t\t\t'event_date' =&gt; Input::post('event_date')\r\n\t\t\t\t));\r\n\r\n\t\t\t\tif ($dtpsample and $dtpsample-&gt;save())\r\n\t\t\t\t{\r\n\t\t\t\t\tSession::set_flash('success', 'Added dtpsample #'.$dtpsample-&gt;id.'.');\r\n\t\t\t\t\tResponse::redirect('dtpsample');\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\tSession::set_flash('error', 'Could not save dtpsample.');\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tSession::set_flash('error', $val-&gt;error());\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t$this-&gt;template-&gt;title = \"DateTimePicker Sample\";\r\n\t\t$this-&gt;template-&gt;content = View::forge('dtpsample\/create');\r\n\r\n\t}\r\n\r\n\tpublic function action_edit($id = null)\r\n\t{\r\n\t\tis_null($id) and Response::redirect('dtpsample');\r\n\r\n\t\t$dtpsample = Model_Dtpsample::find_one_by_id($id);\r\n\r\n\t\tif (Input::method() == 'POST')\r\n\t\t{\r\n\t\t\t$val = Model_Dtpsample::validate('edit');\r\n\r\n\t\t\tif ($val-&gt;run())\r\n\t\t\t{\r\n\t\t\t\t$dtpsample-&gt;name = Input::post('name');\r\n                $dtpsample-&gt;event_date = Input::post('event_date');\r\n\r\n\t\t\t\tif ($dtpsample-&gt;save())\r\n\t\t\t\t{\r\n\t\t\t\t\tSession::set_flash('success', 'Updated dtpsample #'.$id);\r\n\t\t\t\t\tResponse::redirect('dtpsample');\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\tSession::set_flash('error', 'Nothing updated.');\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tSession::set_flash('error', $val-&gt;error());\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t$this-&gt;template-&gt;set_global('dtpsample', $dtpsample, false);\r\n\t\t$this-&gt;template-&gt;title = \"DateTimePicker Sample\";\r\n\t\t$this-&gt;template-&gt;content = View::forge('dtpsample\/edit');\r\n\r\n\t}\r\n\r\n\tpublic function action_delete($id = null)\r\n\t{\r\n\t\tif ($dtpsample = Model_Dtpsample::find_one_by_id($id))\r\n\t\t{\r\n\t\t\t$dtpsample-&gt;delete();\r\n\r\n\t\t\tSession::set_flash('success', 'Deleted dtpsample #'.$id);\r\n\t\t}\r\n\r\n\t\telse\r\n\t\t{\r\n\t\t\tSession::set_flash('error', 'Could not delete dtpsample #'.$id);\r\n\t\t}\r\n\r\n\t\tResponse::redirect('dtpsample');\r\n\r\n\t}\r\n\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have recently started learning about the FuelPHP framework. I have been working on understanding how to use the framework as intended by the creators, and I think I&#8217;m doing a fair job of that, except possibly with regards to using external jQuery libraries. I have a simple CRUD application that I built as a &hellip; <a href=\"http:\/\/www.polysyncronism.com\/wordpress\/2017\/08\/07\/fuelphp-framework-jquery-datetimepicker\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;FuelPHP framework jQuery DateTimePicker&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,12,21,14],"tags":[],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-code","category-javascript","category-jquery","category-php"],"_links":{"self":[{"href":"http:\/\/www.polysyncronism.com\/wordpress\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.polysyncronism.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.polysyncronism.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.polysyncronism.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.polysyncronism.com\/wordpress\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":7,"href":"http:\/\/www.polysyncronism.com\/wordpress\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":158,"href":"http:\/\/www.polysyncronism.com\/wordpress\/wp-json\/wp\/v2\/posts\/148\/revisions\/158"}],"wp:attachment":[{"href":"http:\/\/www.polysyncronism.com\/wordpress\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.polysyncronism.com\/wordpress\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.polysyncronism.com\/wordpress\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}