In my project (based on php/mysql/jquery ) i need the user to enter time.
But it seems very annoying to enter the time in strict formats like
06:00 PM
or
14:00
So i want to give the user freedom to enter time in a very friendly way like
6 pm, 11am etc
or
14
which will then be converted internally into a strict time format for further processing.
Can anybody suggest me any good plugin . If no plugins are there please suggest me how to approach.
What I tend to do for date and/or time fields is let the user enter it in whatever format they like, then format it back to a "universal" particular format on blur of the field. Date.js is incredibly handy for both the parsing and the formatting.
Something like
$('.timeboxme').blur(function() {
var $el = $(this);
var theDate = Date.parse($el.val());
if(theDate) {
$el.val(theDate.toString("HH:mm"));
} else {
//it didn't appear to be a valid date/time, tell the user
}
});
As a bonus, if you use date.js, you get some fancy tricks you can tell the user about, like "+3 hours" or "last hour" :-)
Datejs is very close to what you are asking for.
here's an interesting jquery plugin i found recently
http://haineault.com/media/jquery/ui-timepickr/page/
If you dont wan't to use a jquery plugin my best recommendation would be to use select boxes
3 selects would do it
[hour][minute][ampm]
or even
[hour][minute]
if you went the 24hr route
to me allowing someone to enter '14' into a text to the user
Related
I would like to open access to a page at specific time frame in php.
Say for E.g 9:00AM to 12:00PM - User will be able view a page after which they would get you are not allowed to view this page at this time.
Is this possible in PHP without accessing database?
If so can somebody guide me?
Thanks!
You can do something like this:
if(date('G')>=9 && date('G')<=11)
{
// show your code/site/content.
}
else
{
// Show the "Come back during opening hours..." sign.
}
The date function by default uses the specific time and using G gives a 0-24 value for the hour of the day (no leading zeroes).
This will use the server time though - which is what I expect you want rather than using the user's timezone if they are not at the same timezone as your server.
If your server is in a different timezone to what you expect your users to be at, use date-default-timezone-set to set it to where you want.
You'd better use .htaccess directives. The idea is described here http://www.blog.highub.com/apache/http-server/htaccess-deny-diractory-access-during-a-specific-time/
For a school project we have to build a web app. I'll be creating something where people can keep track of their classes, their homework, and their free time. A planner/calendar. (I'm making it sound really lame here but hey, I'm tired and English isn't my first language ;) )
I'll be working in CodeIgniter for the PHP logic, combined with the usual.. CSS, jQuery, mySQL. PHP is a requirement for the course; I chose to do this in CI because well.. I wanted to learn the framework. We kind of have to show off what we can do at this point of our 'school career'.
Anyway, I would like to ask for some insights regarding a feature I want to implement. At the top of my page, I would like to show a bar which contains the days of the month. Below the day number, I would be showing how many tasks are added on that day by means of some dots. When the user clicks previous or next, I want to show the previous/next month's days. I also want some sort of slider underneath this box which the user can use to slide left and right, and cycle through the days that way. I hope that made sense?
EDIT 2: I want the slider to be dynamic. If the user slides to the previous or next months, or clicks the buttons, I want it to load the days of the previous/next months and show those. Also, say we're at the 26th of a month, the slider would have to show something like 10-31 of this month AND 1-10 of the next month. I suppose I'll also have to change my month indication (not like in the image here) so a user knows when another month starts (I'll show them the name of the month).
Here's a picture (don't mind the day numbers being messed up, I was lazy doing that correctly in Photoshop. will fix that tomo):
I've been looking at the jQuery UI sliders. I suppose I'd have to grab the number of days from a database or by using PHP? I guess the cal_days_in_month function could come in handy here. When the user clicks on the arrows or slides left or right, I don't want the page to refresh. Should I go with ajax calls there? I'm not quite sure how to implement this, to be honest. The numbers are also links to a calendar type of view which shows underneath this bar.
Could I possibly use the CI Calendar class for this? Or is it more for full-fledged google calendar-type of calendars? I thought this screencast could perhaps be useful?
If possible, could someone please provide some insights on how to start working on this and which plugins/etc I could perhaps use? I'm not sure where to start, to be honest. I'm sure I can work this out somehow, but I guess it'd be nice to get a kickstart by means of some help here. The main problem I'm seeing is the slider/next/previous thing and loading in the previous/next month's days.
Thanks in advance.
EDIT: I realise some people might say/think 'OMG, why don't you just use the skills you have instead of trying something you have to ask us about!'. Well, this is because I actually want to learn something while doing this project. Keep in mind, I'm not asking for lines of code here, I'm just asking for some insight on where to start and what stuff to use; perhaps little snippets that can help me out. Thanks.
UPDATE:
I got a very basic 'day bar' working. Still without a slider, nor do the previous and next buttons work, but hey.. at least it fills it in dynamically. It shows the 5 days previous to the current day, then this month till the end. Whatever is left to fill in gets filled with days of the next month. Quite basic. However, I do have a couple of questions!
Since someone told me yesterday that I was breaking design patterns by doing some stuff the way I was doing it, I'm extremely paranoid about the way I'm working now and I would really like some feedback from 'CodeIgniter pro's'. To fill in the 'day bar', I created a helper with a couple of methods. (One method to dynamically fill that 'month year' thing you see in the picture, another method init() which loads the list of the days, like I explained before). I loaded this helper in the controller and I'm now using the methods in my view:
<ul>
<?php
init($current_day_of_month, $current_month,
$current_year, $days_in_current_month, $show_history);
?>
</ul>
The helper then echoes my day values in my view. Is this good or bad practice? I kept thinking the wrong way when I wanted to start writing the code for this.. I wanted to have a function somewhere in my controller and then call it from the view, but I read that I shouldn't be doing it like that.. that I had to reverse my logic. I find it hard to wrap my head around the fact that I have to do this by sending arrays of data to my view (from my controller), so I opted for creating the helper. Good? Bad? Any tips, resources I should read, screencasts I should watch? Thanks a bunch.
This seems pretty straight forward to me. I don't really have time to write the whole thing now. but heres what steps I would take.
1) create a model which gets all the tasks for a month, and uses that to create an array of {date}=>{num_tasks} e.g. {'1'=>3,'2'=>1, "3"=>0, ...}. //hint: use a regular SQL count OR just loop thru and tally them.
2) create a controller function to return this array as JSON. Something like this:
public function get_month($month, $year) {
$tasks = $this->task_model->get_each_days_taskcount($month, $year);
$json = json_encode ($tasks);
echo $json;
}
3) write a html page which has a javascript function to call this controller function with AJAX. Something like:
function fill_calendar(month, year) {
$.get('some_controller/get_month/' + month + '/' + year, function(data) {
// parse the JSON then
// do something with the data here like $('#calendar').append();
});
}
4) Load this month with something like this:
$(function() {
var d = new Date();
var tmonth = d.getMonth();
var tyear = d.getFullYear();
fill_calendar(tmonth, tyear); // populate with this month
});
5) make the prev and next buttons work with something like
$('#prev_button').click(function() {
fill_calendar(current_month - 1 , tyear);
// you will probly need to make this calculation smarter than just minus 1
});
The key thing if you want your system to be dynamic is to make the data transmission short. So using Ajax, as icchanobot says, send the request for a specific month. Use get:
'some_controller?m=' + month + '&y=' + year
or even:
'some_controller?next' // or previous
The controller has to get data for the correct month, but not send back the whole month - only the data needed for your display, in a format as tight as possible. You could query how many events run on which days of that month:
SELECT day, count(event)
FROM event_table
WHERE DATE BETWEEN 'yyyy-mm-01' AND 'yyyy-mm-31'
GROUP BY day
ORDER BY day;
query needs adapting to your data structure - use a function to get the day from a complete date, and maybe use indexes so that the query returns the data fast.
Then the controller returns a string as short as you can make it, of the relevant data sorted in day order:
1=3,15=1,29=2
That would mean "1st=3 events, 15th=1 event, 29=2 events". If you don't want the number of events then "1,15,2" is enough. Empty days aren't transmitted.
the data is received by an ajax event handler on your web page and you parse it by using split, then populate the slider by using a loop.
Your biggest drag, in a very dynamic application, is if it slows down when you repeatedly ask for the next month and the next.
A few tricks:
Update the display while waiting for data; you send your query, and while it is being processed, you can slide the month into view, with the correct number of days, looking disabled so that the user knows immediately that they will get their data, and that it is in progress. Then when the data comes, populate and highlight. It will feel instant though it isn't.
Avoid processing information the user doesn't want anymore. If somebody clicks "next" three times, they want the data for july, not may, june and july. Don't process what you don't display.
Cache data you've already asked, unless you want the system to return dynamically to the server for the latest state of the calendar. You've asked for the data for May and June, but not displayed it; when the user hits "back", don't ask for that data again.
Good luck!
I really don't see the complication about this.......
If you just use the jQuery UI DataPicker and modify the js file so it stores the days in a slider, then put the data in a jQuery UI Slider and you should have almost what you require.
I am having an add/edit form to update and add to database, and I was not sure what the best way is to input TIME type (HH:MM:SS). Should I use multiple html text inputs for HH, MM, SS?
if so, is there a function that prepares the string for database input?
Basically what I'm trying to input is how many hours, minutes, seconds a specific task took to finish.
Can anyone point me in the right direction here?
I'm designing a website using Codeigniter (PHP).
Thanks
Let me go ahead and clarify what needs to happen a bit more...
The user is required to enter data specific to sports more in particular to a players minutes and seconds played. I'm thinking of maybe simplifying it to only minutes. Perhaps this way input is only 1 thing. Then again my question is, what method would work to convert this "minute number" to the correct MYSQL TIME format?
I wrote a helper to do something similar in an app I'm working on. Mine generates three dropdowns, hh, mm and am/pm, by calling built in form_dropdown helper. Once I get the data from the drop downs, I convert it 24hr format and then I just concatenate the strings into the right format for MySQL. Since it's a helper I can just call it from any view using form_time(). I can post it here if you think it would help to see it.
Dana
just use now() function, if your DB is
MYSQL is you want to save the current
time
e.g:
UPDATE tbl SET timemodified = NOW()
and make sure that timemodified has a
type of "time"
Ow sory miss that point. uhm maybe you need to have a start time in your DB, then after he/.she is finish his/her task. you must query on the DB the start time subtract it to the end time(your current time) then the result would be the time he/she performed the task
this checks for 2 numbers, then a ":" then 2 numbers, then a ":", then finally 2 numbers again:
$cleanTime = preg_match( '/(\d\d)\:(\d\d)\:(\d\d)/', $_POST[ 'NAME_OF_TIME_INPUT' ] );
if( !$cleanTime ){ /* ... error ... */ }
don't be scared of all the slashes, haha (I was at first when I used regexps).
replace NAME_OF_TIME_INPUT with the content of the name attribute on the <input on the <form page
i.e. if <input name="coolinput" /> then use $_POST[ 'coolinput' ]
This is one of the eternal struggles of (web) UI design, how to input time without driving the users nuts. What works for your specific case is something only you can decide, because it depends on the exact format/circumstances you need and your target audience.
As general guidelines I'd say:
Don't do a free-form text field that requires a certain format, e.g. "Enter time (HH:MM:SS)", because it's too easy to mess up and will deny the users input or mess up the time if you do no validation.
Try to avoid [0-23] [0-59] [0-59] dropdowns, since they can be quite a pain (click, scroll, click, click, scroll, click, click, scroll, click).
If ease of use is a high priority, as would be the case for public websites, maybe a Javascript enhanced timepicker is a good idea. Try not to use anything too fancy that nobody gets though (like dragging the hands on a clock).
A free-form, free-format text field might be the best idea. The user can just type in "3pm", "16:34" or "midnight". You may need to provide examples to get users started, otherwise they may feel lost. You can run this through strtotime on your end, but you may need to fill in the blanks and do a lot of validation.
Three short text fields may be a good idea if your audience is very keyboard focused and can be expected to tab through them in rapid order.
As for formatting it for SQL, however you receive the time input from the user, you should assemble it to a UNIX timestamp and format that timestamp for SQL:
date('Y-m-d H:i:s', $timestamp);
I have searched for some alternatives and solutions and I came up with this:
$min = 60;
$time[] = floor($min/60);
$time[] = $min%60;
And I used the following to convert to MySQL TIME format
INSERT INTO table (min) VALUES (MAKETIME($time[0], $time[1], 0))
I have had the same issue with working with the html time input. However, I've managed to work around it with a PHP function.
What the function does is translate the time into a format the MYSQL DATETIME datatype can understand.
Of course you will need to enter the date somehow, but I'll leave that up to you.
functions.php
function convertHtmlTime($date,$time){
$newDate = date($date);
$newTime = date($time);
$datetime = new DateTime($newDate.$newTime);
return date_format($datetime, 'YmdHis');
}
test.php
$date="2007-02-16";
$time="23:59";
echo convertHtmlTime($date,$time);
Results:
20070216235900
I want to show when the comment last posted in PHP. like 5 minutes ago, 2 days ago, 7 weeks ago. How to do this?
You can find plenty of answers with full solutions in different languages, pseudocode, ideas, etc.. here.
I believe there's an example of PHP too.
You can use timeago, a jQuery plugin, to do it via Javascript. It would yield the same result, update without refreshing, and by doing it client side instead of server side, you are not precluded from caching.
http://timeago.yarp.com/
Otherwise, Annurag has a link with some good PHP solutions.
You can do manual calculation in server, to get the time difference, then translate it into human time format.
Or my preference, do it in browser using javascript. Using this approach, the time in page can be updated without refresing the page.
You can use this jQuery EasyDate library to translate a DOM element into humane time format.
You can also read the comments in this post about Pretty Date by John Resig. It contain the code, and improvement by other.
Store the comment posted in the date DB and show the the same in the front end by comparing with current date and time using php function
By default a Zend Framework date validator uses the date format yyyy-MM-dd:
$dateValidator = new Zend_Validate_Date();
But I want to add hour and minute validation. In other words, I want to require the user to enter the hour and minute. But the following does not work:
$dateValidator = new Zend_Validate_Date('yyyy-MM-dd hh:ii');
If I enter 2010-02-01, I get an error saying that the date doesn't fit the format. If I enter 2010-02-01 3, it doesn't complain. What's it's doing is assuming that the user means 2010-02-01 03:00 rather than enforcing that the user enters the date in the given format.
How can I enforce that the date must be entered in the given format?
Please see: http://framework.zend.com/issues/browse/ZF-6369
Basically what it comes down to is that the code underlying the format validation doesn't work correctly. Rather than using strict validation, it will try to coerce the provided date into something that will validate and so you get hanky results.
It does look like the bug has been marked as 'Major' so hopefully we'll see a fix soon.
To add to Noah's answer, Zend_Validate_Date is really quite awful and inflexible; that is if you want to have a more forgiving policy for date entry.
Now, if ZF shipped with a Zend_Filter_Date that would normalize the various trivial (albeit very parseable) formats date selectors / user input supplies, that might be a different story as you could filter the date to a normalized format, then validate it that it is in that format. But it doesn't.
No matter though, there are plenty of sane solutions to this problem. Probably the easiest of which is this:
$validator = new \Zend_Validate_Callback(function($value) {
return (bool)strtotime($value);
});
Personally, I don't care whether a date / datetime comes in as yyyy/MM/dd, Sept 23, 2012, or as "-2 weeks" -- all I really care about is whether or not strtotime is smart enough to parse it.
+1 to Stephen's answer.
I went for a similar solution, since I already knew the format I had to validate:
$validator = new \Zend_Validate_Callback(function($value) {
return (bool) date_create_from_format('Y-m-d H:i', $value);
});