Jump to content

i need a if statement


PHP Nubsauce

Recommended Posts

I have a DATE field in my database - and im using a datepicker to add dates. They are obviously added as "0000-00-00" format. There is a blank field next to the date picker, and when you select a date in the datepicker, it add its to the field, and then you hit submit. If you don't add anything, it leaves the field blank.

 

The problem is that if the field is left blank, because theres no specified date to be entered, it just adds a random date, which I can not have. So I need a if statement that says if the field is blank please insert 0000-00-00. I'm no good with dates, so please see if you can help me with this.

 

Does the fix go on the insert into page? Heres how I'm doing it currently -

 

  $date = date('Y-m-d',strtotime($_POST['chooseDate']));
$news = mysql_escape_string($_POST['news']);
$title = mysql_escape_string($_POST['title']);
$addedby = mysql_escape_string($_POST['addedby']);
$sql = "INSERT INTO ".$mysql_pretext."_news " .
"SET title = '$title', news = '$news', added_by = '$addedby', date = '$date'";
if (mysql_query($sql)) {
	  ?>

Link to comment
https://forums.phpfreaks.com/topic/118524-i-need-a-if-statement/
Share on other sites

ok, well thats good advice for the future, but for now the field has to stay as named date. Its working fine other then if the field is left blank.

 

All I need is a simple "if date='' " statement, but I'm not good with dates, whether i need to break it down to y-m-d or not.

 

Any suggestions?

Store the timestamp, then if you need it as yyyy-mm-dd just use date("Y-m-d",$timestamp);

 

Timestamps are easiest to do sums/comparisons with, and it's easy enough to use the date function to convert it into a datetime.

 

I prefer to use

date("Y-m-d H:i:s",$timestamp);

 

Which produces

2008-08-06 23:25:10 or similar.

 

Note that if you have users in both america and england (or other countries with different formats) it often makes sense to change the month to a string. So 06 Aug 2008. It's just a bit easier to work out, and avoids confusion in the first 12 days of a month :P

No, you can store any timestamp, and just convert it to the date.

 

If you need the due date to be in the format 0000-00-00 just use the date("Y-m-d",$timestamp) where $timestamp is the value from the database.

 

If you need to change the value in the database by using a string, just use $timestamp = strtotime("0000-00-00");

Which will return the timestamp for a given date. You can also enter things like 01 Jan 2008 etc. But if you specify 0000-00-00 it has to be in the format YYYY-MM-DD.

 

:)

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.