Jump to content

Format a date in an array..


Scooby08

Recommended Posts

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

$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());

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??

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

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.