Scooby08 Posted August 24, 2008 Share Posted August 24, 2008 If I had an array like this: Array ( [0] => Sunday, August 24, 2008 [1] => 2:34 AM [2] => field [3] => field [4] => field [5] => field [6] => field ) Is there a way that I can apply the code below to the date and time fields? $date = date('Y-m-d', strtotime ($date)); $time = date('H:i:s', strtotime ($time)); I'm trying to format those fields for the database like so: `date` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', `time` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', Link to comment https://forums.phpfreaks.com/topic/121074-format-a-date-in-an-array/ Share on other sites More sharing options...
JasonLewis Posted August 24, 2008 Share Posted August 24, 2008 $date = date("Y-m-d", strtotime($array[0])); //0 index is the date $time = date("H:i:s", strtotime($array[1])); //1 index is the time You can just use MySQL's now() on the insert. mysql_query("INSERT INTO `table` (`date`) VALUES (now())") or die("Error: ".mysql_error()); Link to comment https://forums.phpfreaks.com/topic/121074-format-a-date-in-an-array/#findComment-624157 Share on other sites More sharing options...
Scooby08 Posted August 24, 2008 Author Share Posted August 24, 2008 Ok so now how would I get that date and time into this array to work with this code? <?php // $_POST['weddings'] is the array from my first post foreach ($_POST['weddings'] as $wedding_field_data => $wedding_data) { $wedding_info .= "'".$wedding_data."',"; } $query_weddings = "INSERT INTO dw_weddings (".substr($wedding_columns,'',-1).") "; $query_weddings .= "VALUES ('','$customer_id',".substr($wedding_info,'',-1).") "; ?> Is it possible?? Link to comment https://forums.phpfreaks.com/topic/121074-format-a-date-in-an-array/#findComment-624163 Share on other sites More sharing options...
Barand Posted August 24, 2008 Share Posted August 24, 2008 Why have a date and time field (both of type DATETIME) when you can store both in a single DATETIME column? Link to comment https://forums.phpfreaks.com/topic/121074-format-a-date-in-an-array/#findComment-624187 Share on other sites More sharing options...
Scooby08 Posted August 24, 2008 Author Share Posted August 24, 2008 Well I originally had it like so: <?php $date = $_POST['wedding_date']; $time = $_POST['wedding_time']; $wedding_date_time = date('Y-m-d H:i:s', strtotime ($date.' '.$time)); ?> but I just don't know how to get this to work into the array and thought it might be easier having them separately.. I will use any way that this can work.. The overall process of what I am trying to do might be able to be done differently??? I'm not sure, but this is the only way that I can think to get what I need done to work as of now: <?php if (isset($_POST['submit'])) { $query_weddings_columns = "SHOW COLUMNS FROM dw_weddings "; $result_weddings_columns = mysql_query($query_weddings_columns) or die(mysql_error()); while ($row_weddings_columns = mysql_fetch_object($result_weddings_columns)) { $wedding_columns .= $row_weddings_columns->Field.","; } foreach ($_POST['weddings'] as $wedding_field_data => $wedding_data) { $wedding_info .= "'".$wedding_data."',"; } $query_weddings = "INSERT INTO dw_weddings (".substr($wedding_columns,'',-1).") "; $query_weddings .= "VALUES ('','$customer_id',".substr($wedding_info,'',-1).") "; mysql_query($query_weddings) or die(mysql_error()); } ?> In the end I'm trying to have all my fields on the page totally editable and dynamic, so that this will work no matter how many fields there are and no matter what their names are... Link to comment https://forums.phpfreaks.com/topic/121074-format-a-date-in-an-array/#findComment-624282 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.