verdrm Posted November 7, 2009 Share Posted November 7, 2009 What is the best way with PHP and MySQL to handle time? In example, I have a form where the user can type in a time (12:00, 1:15) and select AM/PM from a pull down menu. The problem is I need it to display in order (12 AM is before 12 PM). How can I solve that with PHP / MySQL? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/180702-php-time/ Share on other sites More sharing options...
verdrm Posted November 8, 2009 Author Share Posted November 8, 2009 Can someone point me in the right direction? Quote Link to comment https://forums.phpfreaks.com/topic/180702-php-time/#findComment-953434 Share on other sites More sharing options...
kenrbnsn Posted November 8, 2009 Share Posted November 8, 2009 If you post your code that's giving you trouble, we can help you. If you don't post anything, we would just be guessing at your problem. Ken Quote Link to comment https://forums.phpfreaks.com/topic/180702-php-time/#findComment-953471 Share on other sites More sharing options...
verdrm Posted November 8, 2009 Author Share Posted November 8, 2009 I don't actually have the code yet. Concept only: I have a form that has two fields: - Time (ex: user input would be 9:15) - AM / PM selection The problem I anticipate is how PHP will display the results correctly since it cannot differentiate AM / PM if I just store the result in MySQL as, for example, "9:15 AM" So my question is, how do I take user inputted times and AM / PM and store / retrieve properly? Quote Link to comment https://forums.phpfreaks.com/topic/180702-php-time/#findComment-953485 Share on other sites More sharing options...
mikesta707 Posted November 8, 2009 Share Posted November 8, 2009 have you tried strtotime() Quote Link to comment https://forums.phpfreaks.com/topic/180702-php-time/#findComment-953491 Share on other sites More sharing options...
verdrm Posted November 8, 2009 Author Share Posted November 8, 2009 How would that work? Can you provide some sample code? Say my form has an input field "time". That would yield, in example "9:15" Then the user has to choose AM / PM. So MySQL would have "9:15 AM" Can I use strtotime() for that? Quote Link to comment https://forums.phpfreaks.com/topic/180702-php-time/#findComment-953501 Share on other sites More sharing options...
mikesta707 Posted November 8, 2009 Share Posted November 8, 2009 if you use a timestamp format for your date column, something like $time = "9:15 AM"; $timestamp = strtotime($time); //insert into database might work. Untested though, so it may need some tweaking Quote Link to comment https://forums.phpfreaks.com/topic/180702-php-time/#findComment-953506 Share on other sites More sharing options...
verdrm Posted November 8, 2009 Author Share Posted November 8, 2009 That did not work. Has anyone stored user inputted times before with AM/PM? Quote Link to comment https://forums.phpfreaks.com/topic/180702-php-time/#findComment-953785 Share on other sites More sharing options...
ashton321 Posted November 8, 2009 Share Posted November 8, 2009 When i did it i did if($daynight = 'PM') { $hour = $hour + 12; } then stored it in the database that way it would be out of 24 hrs so you could then use a MYSQL TIME or DATETIME field Quote Link to comment https://forums.phpfreaks.com/topic/180702-php-time/#findComment-953793 Share on other sites More sharing options...
verdrm Posted November 8, 2009 Author Share Posted November 8, 2009 OK, I'll try that. Quote Link to comment https://forums.phpfreaks.com/topic/180702-php-time/#findComment-953794 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.