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
https://forums.phpfreaks.com/topic/61326-solved-dates-and-mysql/
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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.