TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-22-2008, 10:54 AM   #1 (permalink)
The Wanderer
 
Join Date: Aug 2008
Posts: 11
Thanks: 4
Theo is on a distinguished road
Default Adding Timestamps in SQL?

Okay, this should be stupidly easy, but I can't figure it out :(

I have this table in my database:

PHP Code:
CREATE TABLE IF NOT EXISTS `articles` (
  `
idint(10unsigned NOT NULL auto_increment,
  `
release_datetimestamp NOT NULL default CURRENT_TIMESTAMP,
  `
titlevarchar(100NOT NULL,
  `
contentlongtext NOT NULL,
  `
linkvarchar(100NOT NULL,
  `
author_idint(10unsigned NOT NULL,
  
PRIMARY KEY  (`id`),
  
KEY `release_date` (`release_date`,`author_id`),
  
FULLTEXT KEY `title` (`title`,`content`)
ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=
I'm trying to set the release date when I post a new article like this (using PDO to access a MySQL database):

PHP Code:
//    Construct our timestamp.    
$theTimestamp mktime000$_POST['month'], $_POST['day'], $_POST['year'] );
$theStatement $theDatabase->prepare( <<<_SQL_
    INSERT INTO `articles` 
    ( `release_date`, `title`, `content`, `author_id` )
    VALUES ( ?, ?, ?, ? )
_SQL_
    );
            
$theStatement->execute( array($theTimestamp$_POST['title'], $_POST['content'], $theSession['user_id'] ) ); 
Whenever I run this piece of code the timestamp ends up being stored in my table as 00000000000... (i.e. Wed 31st Dec 1969 ).

I have a section of code that previews the article and constructs the timestamp in exactly the same way and that works fine. Shouldn't this just work? What am I missing here?
Theo is offline  
Reply With Quote
Old 08-22-2008, 12:28 PM   #2 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

It looks like a data type issue. Take a look at the docs for MySQL's TIMESTAMP datatype.

Quote:
TIMESTAMP columns are displayed in the same format as DATETIME columns. In other words, the display width is fixed at 19 characters, and the format is 'YYYY-MM-DD HH:MM:SS'
I typically store unix timestamps as an INT.
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
The Following User Says Thank You to buggabill For This Useful Post:
Theo (08-22-2008)
Old 08-23-2008, 04:27 AM   #3 (permalink)
The Wanderer
 
Join Date: Aug 2008
Posts: 11
Thanks: 4
Theo is on a distinguished road
Default

Thanks, I've changed the data type of 'release_date' and it's all working as designed now :)

I was assuming the TIMESTAMP data type meant epoch timestamp but apparently not...
Theo is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:56 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design