Jump to content

Recommended Posts

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 ????

Link to comment
https://forums.phpfreaks.com/topic/241913-php-mysql-time-funtion-datetime/
Share on other sites

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.

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,  :D

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'

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()".

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);

...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'

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.