Jump to content

Question regarding Unix timestamps, php, and mysql


Kevin3374

Recommended Posts

I'm having some trouble inserting timestamps into a mysql table.  I am inserting entries into a table using the following query:

 

$query = <<<SQLQUERY
INSERT INTO Events
  		SET
  		start_date = FROM_UNIXTIME($startDates[$x]),
  		end_date = FROM_UNIXTIME($endDates[$x]),
  		title = "$titles[$x]",
  		location = "$locations[$x]"
SQLQUERY;

 

Note that start_date and end_date were created with type DATE, in mysql. 

 

In another file, I am trying to select and display the records from this table using the following code

 

$query_str = "SELECT UNIX_TIMESTAMP(start_date), UNIX_TIMESTAMP(end_date), title, location FROM Events";
$result = $conn->query($query_str);

//left out a few lines here

while (($row_data = @$result->fetch_assoc()) !== NULL)
	{
		echo "<tr><td>{$row_data['start_date']}";

		if($row_data['end_date'] != NULL)
			echo " - " . date('M d', $row_data['end_date']). "</td>";
		else
			echo "</td>";	

		echo "<td>{$row_data['title']}</td><td>{$row_data['location']}</td></tr>\n";	

	}//end while

 

I get the title and locations back correctly, but the cells for the dates all come back empty.  Anyone have any idea what I'm doing wrong?

try

 

<?php
$query_str = "SELECT UNIX_TIMESTAMP(start_date) as start_date, UNIX_TIMESTAMP(end_date) as end_date, title, location FROM Events";
?>

 

Thanks, that worked perfectly.  Not sure why my book said to do it the way I had it, but your way definitely works.

please click topic solved

 

without the "as" you would of needed to do this

echo "the start date is {$row_data['UNIX_TIMESTAMP(start_date)']}";

 

Okay, I'm sure its somewhere really obvious and I'm just blind.  But I don't see a 'topic solved" link anywhere.  I even did ctrl-f and the only result was in your reply to me.

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.