This post is in reference to the Build a CMS in an Afternoon forum question regarding exchanging dates between PHP and MySQL:
In a nutshell, the dates I enter on the editArticle.php form for starting and ending the publication of an article are appearing correctly in the MySQL database I have set up. But when I tested editing an already-existing article, these dates come back to the form as 12-31-1969.
(I also have a creationDate property, which is automatically entered as
There is no problem with this date field going to or coming from MySQL.)
I believe this means that the pubStart and pubEnd dates are passed correctly to the database, but when coming out of the database, they are not properly defined.
Here is the code from Article.php for the relevant date fields:
and here is the code from the editArticle.php form:
What do I need to do to correct this problem? Any help appreciated.
Best,
Shelley
--
What will we do when the power goes out?
In a nutshell, the dates I enter on the editArticle.php form for starting and ending the publication of an article are appearing correctly in the MySQL database I have set up. But when I tested editing an already-existing article, these dates come back to the form as 12-31-1969.
(I also have a creationDate property, which is automatically entered as
value="<?php echo date('Y-m-d') ?>"
There is no problem with this date field going to or coming from MySQL.)
I believe this means that the pubStart and pubEnd dates are passed correctly to the database, but when coming out of the database, they are not properly defined.
Here is the code from Article.php for the relevant date fields:
class Article {
public $creationDate = null;
public $pubStart = null;
public $pubEnd = null;
...
}
public function __construct( $data=array() ) {
if ( isset( $data['creationDate'] ) ) $this->creationDate = (int) $data['creationDate'];
if ( isset( $data['pubStart'] ) ) $this->pubStart = (int) $data['pubStart'];
if ( isset( $data['pubEnd'] ) ) $this->pubEnd = (int) $data['pubEnd'];
...
}
public function storeFormValues ( $params ) {
// Store all the parameters
$this->__construct( $params );
// Parse and store the creationDate data
if ( isset($params['creationDate']) ) {
$creationDate = explode ( '-', $params['creationDate'] );
if ( count($creationDate) == 3 ) {
list ( $y, $m, $d ) = $creationDate;
$this->creationDate = mktime ( 0, 0, 0, $m, $d, $y );
}
}
// Parse and store the pubStart date
if ( isset($params['pubStart']) ) {
$pubStart = explode ( '-', $params['pubStart'] );
if ( count($pubStart) == 3 ) {
list ( $y, $m, $d ) = $pubStart;
$this->pubStart = mktime ( 0, 0, 0, $m, $d, $y );
}
}
// Parse and store the pubEnd date
if ( isset($params['pubEnd']) ) {
$pubEnd = explode ( '-', $params['pubEnd'] );
if ( count($pubEnd) == 3 ) {
list ( $y, $m, $d ) = $pubEnd;
$this->pubEnd = mktime ( 0, 0, 0, $m, $d, $y );
}
}
}
public function insert() {
// Does the Article object already have an ID?
if ( !is_null( $this->id ) ) trigger_error ( "Article::insert(): Attempt to insert an Article object that already has its ID property set (to $this->id).", E_USER_ERROR );
// Insert the Article
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "INSERT INTO articles_2 ( creationDate, pubStart, pubEnd, title, summary, content, author ) VALUES ( FROM_UNIXTIME(:creationDate), FROM_UNIXTIME(:pubStart), FROM_UNIXTIME(:pubEnd), :title, :summary, :content, :author )";
$st = $conn->prepare ( $sql );
$st->bindValue( ":creationDate", $this->creationDate, PDO::PARAM_INT );
$st->bindValue( ":pubStart", $this->pubStart, PDO::PARAM_INT );
$st->bindValue( ":pubEnd", $this->pubEnd, PDO::PARAM_INT );
...
}
public function update() {
// Does the Article object have an ID?
if ( is_null( $this->id ) ) trigger_error ( "Article::update(): Attempt to update an Article object that does not have its ID property set.", E_USER_ERROR );
// Update the Article
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "UPDATE articles_2 SET creationDate=FROM_UNIXTIME(:creationDate), pubStart=FROM_UNIXTIME(:pubStart), pubEnd=FROM_UNIXTIME(:pubEnd), title=:title, summary=:summary, content=:content, author=:author WHERE id = :id";
$st = $conn->prepare ( $sql );
$st->bindValue( ":creationDate", $this->creationDate, PDO::PARAM_INT );
$st->bindValue( ":pubStart", $this->pubStart, PDO::PARAM_INT );
$st->bindValue( ":pubEnd", $this->pubEnd, PDO::PARAM_INT );
...
}
and here is the code from the editArticle.php form:
<li>
<label for="creationDate">Article Creation Date</label> <input type="date" name="creationDate" id="creationDate" placeholder="YYYY-MM-DD" required maxlength="10" value="<?php echo date('Y-m-d') ?>" />
</li>
<li>
<label for="pubStart">Publish Start Date</label> <input type="date" name="pubStart" id="pubStart" placeholder="YYYY-MM-DD" required maxlength="10" value="<?php echo $results['article']->pubStart ? date( "Y-m-d", $results['article']->pubStart ) : "" ?>" />
</li>
<li>
<label for="pubEnd">Publish End Date</label> <input type="date" name="pubEnd" id="pubEnd" placeholder="YYYY-MM-DD" required maxlength="10" value="<?php echo $results['article']->pubEnd ? date( "Y-m-d", $results['article']->pubEnd ) : "" ?>" />
</li>
What do I need to do to correct this problem? Any help appreciated.
Best,
Shelley
--
What will we do when the power goes out?



