kingnutter Posted April 24, 2009 Share Posted April 24, 2009 Hi there, I am splitting dates entered into a form into both a text field and Unix timestamp. Users are given the option to enter D:M:Y, M:Y or just the year. When they just enter a year I want the timestamp to default to the first day and month of that year, but using the script below it is defaulting to the current time. The process seems to work fine for generating a Day(01) for M:Y entry so I am thinking it is a problem with my syntax for the default month (January) though I HAVE tried as many variations as I can think of. All help appreciated. This is the part of the form to enter the date. I am changing $freq manually for the moment, but it will ultimately be POSTed from a previous page. <?php // This script makes three pull-down menus // for an HTML form: months, days, years. // Make the months array: $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); // Make the days and years arrays and set frequency $days = range (1, 31); $years = range (2001, 2009); $freq = "yearly"; if ($freq == "daily") { // Make the days pull-down menu: echo '<select name="day">'; foreach ($days as $value) { echo "<option value=\"$value\">$value</option>\n"; } echo '</select>'; } // Make the months pull-down menu: if (($freq == "daily") || ($freq == "monthly")) { echo '<select name="month">'; foreach ($months as $value) { echo "<option value=\"$value\">$value</option>\n"; } echo '</select>'; } // make the year pull-down menu: if (($freq = "daily") || ($freq == "monthly") || ($freq == "yearly")) { echo '<select name="year">'; foreach ($years as $value) { echo "<option value=\"$value\">$value</option>\n"; } echo '</select>'; } ?> And this is the errorList and mysql query. NB: I have put a <?php at the beginning so the code snippet reads better but it actually in another part of the script. <?php // set up error list array $errorList = array(); $moj_title = $_POST[moj_title]; // $moj_date = $_POST[moj_date]; (This should be back into the code now, but I'll leave it thus in case it is part of the problem) $moj_issue = $_POST[moj_issue]; $moj_summary = $_POST[moj_summary]; $moj_genre = $_POST[moj_genre]; // if ($freq == "daily") // { $day = $_POST[day]; // } // else // {$day = 01;} // if (($freq == "monthly") || ($freq == "daily")) // { $month = $_POST[month]; // } // else // {$month = 01;} $year = $_POST[year]; //validate text input fields if (trim($_POST['moj_title']) == '') { $errorList[] = 'Invalid entry: Title'; } // if (trim($_POST['moj_date']) == '') // { // $errorList[] = 'Invalid entry: Date (Remember - testing for text only)'; // } if (trim($_POST['moj_issue']) == '') { $errorList[] = 'Invalid entry: Issue (remember - not validating numeric entry yet)'; } if (trim($_POST['moj_summary']) == '') { $errorList[] = 'Invalid entry: Summary'; } if (trim($_POST['moj_genre']) == '') { $errorList[] = 'Invalid entry: Genre'; } // ultimately the day will be checked against the month i.e. no 30th Feb. Can maybe do this with 'minus' on unixtime if (trim($_POST['day']) == '' && ($freq == "daily")) { $errorList[] = 'Invalid entry: Day of date'; } if (trim($_POST['month']) == '' && (($freq == "daily") || ($freq == "monthly"))) { $errorList[] = 'Invalid entry: Month of date'; } if (trim($_POST['year']) == '' && ((($freq == "daily") || ($freq == "monthly") || ($freq == "yearly")))) { $errorList[] = 'Invalid entry: Year of date'; } // check for errors // if none found if (sizeof($errorList) == 0) { // open database connection $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect to database: Line 131'); // select database mysql_select_db($db) or die ('Unable to connect'); // Does the following text-based date ignore $day for a monthly or might it create problems later? $date = "$day"." "."$month"." "."$year"; // if monthly turn $day into 01 and use $month and $year for $unixdate if ($freq == "monthly") { $day = 1; } // if yearly, turn $day into 01 and $month into 01 and use $year for $unixdate if ($freq == "yearly") { $day = 1; $month = January; } $dateforunix = ("$day"." "."$month"." "."$year"); $unixdate = strtotime("$dateforunix"); // generate and execute query $query = "INSERT INTO mojocd(moj_title, moj_date, moj_issue, moj_summary, moj_genre, unix_timestamp, moj_timestamp) VALUES ('$moj_title', '$date', '$moj_issue', '$moj_summary', '$moj_genre', '$unixdate', NOW())"; $result = mysql_query($query) or die ("Error in Query: $query. " . mysql_error()); // print result echo '<font size=-1>Update successful. <a href=list.php>Go back to the main menu</a>.</font>'; // close database connection mysql_close($connection); } else { // errors found // print as list echo '<font size=-1>The following errors were encountered:'; echo '<br>'; echo '<ul>'; for ($x=0; $x<sizeof($errorList); $x++) { echo "<li>$errorList[$x]"; } echo '</ul></font>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155482-generating-default-months-for-year-only-unix-timestamp/ Share on other sites More sharing options...
Yesideez Posted April 24, 2009 Share Posted April 24, 2009 I think you need mktime() here. http://uk.php.net/mktime Here's a quick function off the top of my head... function makeDT($year,$month=false,$day=false,$hour=false,$minute=false,$second=false) { return mktime(($hour ? $hour : 0),($minute ? $minute : 0),($second ? $second ? 0),($month ? $month : 1),($day ? $day : 1),$year); } That will return a UNIX timestamp depending on what you feed it. Minimum it needs is the year. Whatever is missing in the time gets set to 0. Whatever is missing in the date gets set to 1. Examples: echo makeDT(2009); Will return 1st Jan 2009 midnight echo makeDT(2009,,2,10,30); Will return 2nd Jan 2009, 10:30:00am Hope this is of help and what you're after. Quote Link to comment https://forums.phpfreaks.com/topic/155482-generating-default-months-for-year-only-unix-timestamp/#findComment-818161 Share on other sites More sharing options...
kingnutter Posted April 24, 2009 Author Share Posted April 24, 2009 Sounds good. I'll give it a try. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/155482-generating-default-months-for-year-only-unix-timestamp/#findComment-818164 Share on other sites More sharing options...
kingnutter Posted April 24, 2009 Author Share Posted April 24, 2009 To be honest, mktime seems to be sending me off on a whole new tangent. I feel like I am so close with strtotime. Any other thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/155482-generating-default-months-for-year-only-unix-timestamp/#findComment-818194 Share on other sites More sharing options...
kingnutter Posted April 24, 2009 Author Share Posted April 24, 2009 My gut reaction is it is an error in the syntax of this section from the code above: $dateforunix = ("$day"." "."$month"." "."$year"); $unixdate = strtotime("$dateforunix"); But I can't put my finger on it. Quote Link to comment https://forums.phpfreaks.com/topic/155482-generating-default-months-for-year-only-unix-timestamp/#findComment-818464 Share on other sites More sharing options...
laffin Posted April 24, 2009 Share Posted April 24, 2009 ya removed the validation code which uses a default value Quote Link to comment https://forums.phpfreaks.com/topic/155482-generating-default-months-for-year-only-unix-timestamp/#findComment-818479 Share on other sites More sharing options...
kingnutter Posted April 24, 2009 Author Share Posted April 24, 2009 Sounds promising. But which part of my code is that? I'm just getting up to speed on my terminology. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/155482-generating-default-months-for-year-only-unix-timestamp/#findComment-818596 Share on other sites More sharing options...
laffin Posted April 24, 2009 Share Posted April 24, 2009 U should be giving the variables default values if none are entered example // if ($freq == "daily") // { $day = $_POST[day]; // } // else // {$day = 01;} if day isnt submitted, it ends up being blank so u must check for this circumstance and give it a default value, in our case 1 $day=(isset($_GET['day']) && is_numeric($_GET['day']) && $_GET['day']>0 && $_GET['day']<31)?$_GET['day']:1; if yer able to follow the mess, its just a sequence of if statements if day exist and its numeric and is between 1-31 then use the $_GET information, otherwise set it to 1 Quote Link to comment https://forums.phpfreaks.com/topic/155482-generating-default-months-for-year-only-unix-timestamp/#findComment-818617 Share on other sites More sharing options...
kingnutter Posted April 24, 2009 Author Share Posted April 24, 2009 Thanks for that. However you will see that I decided to move the default values for the variables further on in the code, after the $date value had been assigned. I left the old code in there in case I needed to backtrack. So still where I started if anyone has time to have a read through. Quote Link to comment https://forums.phpfreaks.com/topic/155482-generating-default-months-for-year-only-unix-timestamp/#findComment-818669 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.