Jump to content

[SOLVED] dates and MySql


simon551

Recommended Posts

I think I'm kind of confused on how to deal with nulls in dates.

 

if ( !($_POST['beginningDate']) == '') 
{
	$date= ($_POST['beginningDate']);
	$timestamp = strtotime($date);
	$bdate = date("Y-m-d", $timestamp);
}
else 	{
		$bdate= 'NULL';
	}


$insertSQL="INSERT INTO table (beginningDate) VALUES ($bdate)";

 

This works to take care of the null, but if the date actually has a post value, then the query

rejects the date because it is not surrounded by quotes. What I'm struggling with is that MySql

requires either a NULL or a 'Value' but I don't really know how to accomodate the possibility of

either. Some have suggested that I leave the date out of the query if it is null. Would that mean

I would re-write the query in the if statement? Does anyone have a better way of dealing with this

problem?

 

Link to comment
Share on other sites

if ( !($_POST['beginningDate']) == '') 
{
	$date= ($_POST['beginningDate']);
	$timestamp = strtotime($date);
	$bdate = date("Y-m-d", $timestamp);
}
else 	{
		$bdate= 'NULL';
	}


$insertSQL="INSERT INTO table (beginningDate) VALUES ('$bdate')";

 

 

That will work, just surround it with the quotes. Maybe I am missing something here, but I have no problem with single quotes in a query surrounding a variable.

 

Nate

 

Link to comment
Share on other sites

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.