YoungNate_Black_coder Posted July 13, 2011 Share Posted July 13, 2011 okay so i wanna how would i go about doing this? i want when my pregnant function takes place on my website i want to store a value in the database and when a ceritan time has been met i update the datbase to change a value in the database : like when the form is submitted chang a value to pregnant then store it in a database then when 20 mins pass change that to not pregnant ???? Quote Link to comment https://forums.phpfreaks.com/topic/241913-php-mysql-time-funtion-datetime/ Share on other sites More sharing options...
Philip Posted July 13, 2011 Share Posted July 13, 2011 class Girl { private $_skank; public function __construct( $skank ) { if(!is_bool($skank)) $skank = true; $this->_skank = $skank; } private function knockUp( ) { return (bool) $this->_skank; } public function isPrego() { if(!$this->knockUp()) return false; $this->popOutBaby(); return true; } private function popOutBaby( ) { echo "waaaahhh!"; } } $liz = new Girl(true); if($liz->isPrego()) echo 'gratz, its a boy!'; 'nough said. Quote Link to comment https://forums.phpfreaks.com/topic/241913-php-mysql-time-funtion-datetime/#findComment-1242340 Share on other sites More sharing options...
QuickOldCar Posted July 13, 2011 Share Posted July 13, 2011 class Girl { private $_skank; public function __construct( $skank ) { if(!is_bool($skank)) $skank = true; $this->_skank = $skank; } private function knockUp( ) { return (bool) $this->_skank; } public function isPrego() { if(!$this->knockUp()) return false; $this->popOutBaby(); return true; } private function popOutBaby( ) { echo "waaaahhh!"; } } $liz = new Girl(true); if($liz->isPrego()) echo 'gratz, its a boy!'; 'nough said. hilarious, Quote Link to comment https://forums.phpfreaks.com/topic/241913-php-mysql-time-funtion-datetime/#findComment-1242343 Share on other sites More sharing options...
Psycho Posted July 13, 2011 Share Posted July 13, 2011 You are making this more difficult than it needs to be. You shouldn't need to set a value, then at some set time in the future change it back. That would require you to have some automated process constantly running. You simply do that logic in your code. Here is a rough example of what I am talking about. When the form is submitted enter a timestamp into the database for the start of the event (i.e. pregnancy). You want the gestation period to be 20 minutes, so you simply do a query on that field and determine if 20 minutes have passed from that timestamp or not. If no, then the result is pregnant. If yes, then the result is "not pregnant". You never need to update the value. You can do the calculation in PHP or in MySQL. Here is an example of how you could return a Boolean value from a MySQL query to determine pregnant/not pregnant using just the pregnancy start time. Not 100% sure of the sysntax, but it should be anough to get you started SELECT *, IF(ADDTIME(`pregnancy_start`, '00:20:00') > NOW(), 1, 0) as `pregnant` FROM table WHERE id = '$id' Quote Link to comment https://forums.phpfreaks.com/topic/241913-php-mysql-time-funtion-datetime/#findComment-1242348 Share on other sites More sharing options...
YoungNate_Black_coder Posted July 13, 2011 Author Share Posted July 13, 2011 and what should the timestamp structure be in mysql ?? because right now it is varcher jut because i wanted to be able to say ex. jan, 9 , 2011.... and whats the format for inputing the time now into the database? Quote Link to comment https://forums.phpfreaks.com/topic/241913-php-mysql-time-funtion-datetime/#findComment-1242352 Share on other sites More sharing options...
djlee Posted July 13, 2011 Share Posted July 13, 2011 i personally use epoch time for all timestamps apart from DOB's. then use php's date() to format as required. Integer comparison is quicker than other comparitors, and i find it easier to understand. If you have the timestamp you can in php just do if($timestamp > (time() + 1200)) { /pregnancy ends } However it really just comes down to personal preference since the performance differences are negligable. When inserting into a DATETIME field in mysql, you can just use "NOW()". Quote Link to comment https://forums.phpfreaks.com/topic/241913-php-mysql-time-funtion-datetime/#findComment-1242359 Share on other sites More sharing options...
Psycho Posted July 13, 2011 Share Posted July 13, 2011 and what should the timestamp structure be in mysql ?? because right now it is varcher jut because i wanted to be able to say ex. jan, 9 , 2011.... and whats the format for inputing the time now into the database? varchar? That's just silly. Doing that you now lose all ability to use any of the date functions in MySQL. PHP has the date() function that allows you to format a timestamp in any format you wish. Since you need time information in addition to date information, I would use the MySQL timestamp field type. But, it is not the same as a PHP timestamp so you will need to do a conversion between the two. You can either do the conversion in PHP or MySQL (here is a good article on handling dates between PHP and MySQL. Has some useful information: http://www.richardlord.net/blog/dates-in-php-and-mysql). However, if you don't need to actually use the date for display purposes may be able to do everything in MySQL and not need to worry about the conversion. That assumes that when the user submits the form that you want the current date/time entered and it is not a user specified date/time entered on the form. Step 1: Insert or Update a record, setting the start date/time of the pregnancy to "now" INSERT INTO sameTabel (`id`, `startPregnancy`) VALUES ($idValue, NOW()) If you need to set the pregnancy start date/time to something other than "NOW()" (i.e. user specified) then just take the date in PHP and format it using the PHP date() function to MySQL format: "YYYY-MM-DD HH:MM:SS". $mysqlDateTime = date("Y-m-d H:i:s", strtotime($sameDateValue)); Then just use a query similar to what I showed above to determine whether a record is currently pregnant or not. Now, if you want to present the date from MySQL onto the page in the format "jan, 9 , 2011", just get the value from mysql and convert it using date() echo date("M. j, Y", $dateValueFrom MySQL); Quote Link to comment https://forums.phpfreaks.com/topic/241913-php-mysql-time-funtion-datetime/#findComment-1242396 Share on other sites More sharing options...
YoungNate_Black_coder Posted July 14, 2011 Author Share Posted July 14, 2011 okay so i changed my database structure to timestamp and when the snake gets pregnant i store now(), now in the same insert query how do i add 20 mins to that and check if that 20 mins has been passed on another page? Quote Link to comment https://forums.phpfreaks.com/topic/241913-php-mysql-time-funtion-datetime/#findComment-1242802 Share on other sites More sharing options...
Psycho Posted July 14, 2011 Share Posted July 14, 2011 ...now in the same insert query how do i add 20 mins to that and check if that 20 mins has been passed on another page? You don't need to add 20 minutes and store that value in the INSERT query. As I explained previously, when you need to determine if "pregnant" is true or false you would run a query such as SELECT *, IF(ADDTIME(`pregnancy_start`, '00:20:00') > NOW(), 1, 0) as `pregnant` FROM table WHERE id = '$id' Quote Link to comment https://forums.phpfreaks.com/topic/241913-php-mysql-time-funtion-datetime/#findComment-1242842 Share on other sites More sharing options...
YoungNate_Black_coder Posted July 14, 2011 Author Share Posted July 14, 2011 bu im calling for all rows like : select all from table where sex = 'female' and (if she is not pregnant)???? thiss will return all female snakes that isnt pregnant.... Quote Link to comment https://forums.phpfreaks.com/topic/241913-php-mysql-time-funtion-datetime/#findComment-1242857 Share on other sites More sharing options...
Psycho Posted July 14, 2011 Share Posted July 14, 2011 I already showed the logic to determine if the snake is pregnant or not, just move that logic to the WHERE clause instead of creating a value. Heck you could have simply used that variable in the WHERE clause. SELECT *, FROM table WHERE sex = 'female' AND ADDTIME(`pregnancy_start`, '00:20:00') > NOW() OR SELECT *, IF(ADDTIME(`pregnancy_start`, '00:20:00') > NOW(), 1, 0) as `pregnant` FROM table WHERE sex = 'female' AND `pregnant` = 1 Quote Link to comment https://forums.phpfreaks.com/topic/241913-php-mysql-time-funtion-datetime/#findComment-1242861 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.