html - PHP not storing the date in MySQL table column -
i'm having problem storing date correctly in mysql database table under column name 'publicationdate' via html form.
// contruct of data public function __construct($data=array()) { if (isset($data['id'])) $this->id = (int) $data['id']; if (isset($data['publicationdate'])) $this->publicationdate = (int) $data['publicationdate']; if (isset($data['content'])) $this->content = $data['content']; } // values post/store webpage forms public function storeformvalues ($params) { $this->__construct($params); if (isset($params['publicationdate'])) { $publicationdate = explode ('-', $params['publicationdate']); if (count($publicationdate) == 3) { list ($y, $m, $d) = $publicationdate; $this->publicationdate = mktime (0, 0, 0, $m, $d, $y); } } } the sql column 'publicationdate' datatype 'timestamp()' 'not null' default 'current_timestamp'. date stored & formatted '0000-00-00 00:00:00'.
the html form input 'publicationdate' edit form hidden , not edited as:
<input type="hidden" name="publicationdate" id="publicationdate" value="<?php if ($results['article']->id ==true) { echo date("y-m-d g:i:s", $results['article']->publicationdate);} else { echo date("y-m-d g:i:s");}?>"/> it'll store date - '0000-00-00 00:00:00' 'yyyy-mm-dd hh:mm:ss' year, month, & day show correctly. hour, minutes, & seconds appear in mysql zeros (00:00:00). problem when display/listing data 'desc' of course recent being @ top of list.
since want mysql date format 'yyyy-mm-dd hh:mm:ss' need change php date format.
change date("y-m-d g:i:s" date("y-m-d h:i:s" in both instances.
the g format displays hour in 12-hour format of hour without leading zeros while h format displays hour in 24-hour format leading zeros.
also when construct $publicationdate create with hour=0, minute=0, second=0.
if want populate $publicationdate time of form's submission use this:
$publicationdate = mktime(date('h'), date('i'), date('s'), $m, $d, $y);
this take current hour, minute , second , submitted month, day , year user.
you should assign actual times in `storeformvalues function.
see here manual mktime
Comments
Post a Comment