PHP Nubsauce Posted August 6, 2008 Share Posted August 6, 2008 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 More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Just add DEFAULT 0000-00-00 to the column in MySQL? Link to comment https://forums.phpfreaks.com/topic/118524-i-need-a-if-statement/#findComment-610177 Share on other sites More sharing options...
PHP Nubsauce Posted August 6, 2008 Author Share Posted August 6, 2008 no that dident work =[ Link to comment https://forums.phpfreaks.com/topic/118524-i-need-a-if-statement/#findComment-610184 Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Oh yeah, by the way, date is a mysql reserved keyword so you shouldn't have column named after it. Link to comment https://forums.phpfreaks.com/topic/118524-i-need-a-if-statement/#findComment-610186 Share on other sites More sharing options...
Naez Posted August 6, 2008 Share Posted August 6, 2008 Just store a timestamp instead. Link to comment https://forums.phpfreaks.com/topic/118524-i-need-a-if-statement/#findComment-610187 Share on other sites More sharing options...
PHP Nubsauce Posted August 6, 2008 Author Share Posted August 6, 2008 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? Link to comment https://forums.phpfreaks.com/topic/118524-i-need-a-if-statement/#findComment-610188 Share on other sites More sharing options...
PHP Nubsauce Posted August 6, 2008 Author Share Posted August 6, 2008 i cant just store a timestamp cus i have other code that needs 0000-00-00 Link to comment https://forums.phpfreaks.com/topic/118524-i-need-a-if-statement/#findComment-610191 Share on other sites More sharing options...
deadonarrival Posted August 6, 2008 Share Posted August 6, 2008 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 Link to comment https://forums.phpfreaks.com/topic/118524-i-need-a-if-statement/#findComment-610197 Share on other sites More sharing options...
PHP Nubsauce Posted August 6, 2008 Author Share Posted August 6, 2008 wouldent that produce todays date? I need it to liturally be 0000-00-00, because the actual field describes "due date" and sometimes there is none Link to comment https://forums.phpfreaks.com/topic/118524-i-need-a-if-statement/#findComment-610201 Share on other sites More sharing options...
deadonarrival Posted August 7, 2008 Share Posted August 7, 2008 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. Link to comment https://forums.phpfreaks.com/topic/118524-i-need-a-if-statement/#findComment-611089 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.