I'm back again with more PHP problems, but this time with the DateTime class.
This is a section of my actual code:
//setup $dbc and $dbcOptions
function test($database, $databaseoptions, $termcode, $department)
{
$tsql = "SELECT StartDate, StopDate
FROM CourseRecords
WHERE TermCode = '" .$termcode. "' AND Department = '" .$department. "';";
$params = array();
$stmt = sqlsrv_query($database, $tsql, $params, $databaseoptions); //---Query ALL the things!!!
if( $stmt === false ) {
echo 'This page is broken<br />';
die( print_r( sqlsrv_errors(), true));
}
return $stmt;
}
$results = test($dbc, $dbcOptions, "142s", "CSC");
$rows = sqlsrv_num_rows($results);
echo "rows = " .$rows;
for ($i = 0; $i < $rows; $i++)
{
if(sqlsrv_fetch($results) !== false)
{
$startdate = date_format(sqlsrv_get_field($results, 0), "Y-m-d"); //line 72
$stopdate = date_format(sqlsrv_get_field($results, 1), "Y-m-d"); //line 73
}
}
I get the following errors in my php-errors.log file:
[13-Nov-2014 17:05:19 UTC] PHP Warning: date_create(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for '-6.0/no DST' instead in E:\inetpub\wwwroot\directory\searchtest.php on line 72
[13-Nov-2014 17:05:19 UTC] PHP Warning: date_create(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for '-6.0/no DST' instead in E:\inetpub\wwwroot\directory\searchtest.php on line 73
Creating $startdate and $stopdate as DateTime objects produces the same error when I do this:
$timezone = new DateTimeZone("America/Chicago");
$startdate = new DateTime("2012-12-12", $timezone);
$stopdate = new DateTime("2012-12-12", $timezone);
// get $results and find $rows and junk
$startdate = sqlsrv_get_field($results, 0);
$stopdate = sqlsrv_get_field($results, 1);
which by all means should work because you guys have helped me with previous problems.
So my question is: Why does this fail with a 500 error and what can I do to correct it?
Converting my comment into answer:
Try adding date_default_timezone_set('America/Chicago'); at the beginning of your script.
Related
I am unable to export my mysql table to XML format using PHP, I receive the following error when I run the below script. I have removed the connection bit, I have gone through already errors but was not able resolve this issue.
Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given,
Script:
<?php
$query = "SELECT id, name, plate, datetime , time , image_name FROM EarlsdonMSIN_anpr_vega";
$camArray = array();
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
array_push($camArray, $row);
}
if(count($camArray)){
createXMLfile($camArray);
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
function createXMLfile($camArray){
$filePath = '/cam.xml';
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('cam');
for($i=0; $i<count($camArray); $i++){
$camId = $camArray[$i]['id'];
$camName = $camArray[$i]['name'];
$camplate = $camArray[$i]['plate'];
$camdatetime = $camArray[$i]['datetime'];
$camtime = $camArray[$i]['time'];
$camimagename = $camArray[$i]['image_name'];
$cam = $dom->createElement('cam');
$cam->setAttribute('id', $camId);
$name = $dom->createElement('name', camName);
$cam->appendChild($name);
$plate = $dom->createElement('plate', $camplate);
$cam->appendChild($plate);
$datetime = $dom->createElement('datetime', $camdatetime);
$cam->appendChild($datetime);
$time = $dom->createElement('time', $camtime);
$cam->appendChild($time);
$imagename = $dom->createElement('image_name', $camimagename); $cam->appendChild($image_name);
$root->appendChild($cam);
}
$dom->appendChild($root);
$dom->save($filePath);
}
Help me, i wanna put the timestone to this format Hour:minutes:secondes
$services = $pdo ->prepare("SELECT * FROM `servicos` WHERE `DE` = :u");
$services->bindValue(":u", "1");
$services->execute();
echo $numServico = $services->rowCount(); // Contagem de serviços
$l = $services->fetchAll(PDO::FETCH_OBJ);
//$json = json_encode($l);
echo '{"data":[';
//echo $json;
foreach ($l as $listar) {
$inicioHora = new DateTime("listar->REGISTRO");
$inicioHora = $inicioHora->format('H:i:s');
}
But i receive this
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (listar->REGISTRO) at position 0 (l): The timezone could not be found in the database' on line 26
if the field $listar->REGISTRO is a timestamp, then you only need to use the date() function http://php.net/manual/en/function.date.php
$time = date('H:i:s', $listar->REGISTRO);
Replace this:
$inicioHora = new DateTime("listar->REGISTRO");
$inicioHora = $inicioHora->format('H:i:s');
With this:
$inicioHora = date('H:i:s', strtotime($listar->REGISTRO));
This code throw Exception:
public function actionSetdubl() {
$dubls = Yii::$app->request->post('dubl');
$parent = Yii::$app->request->post('parent');
$parentInfo = JurForm::find()->where(['PKJUR' => $parent])->asArray()->all()[0];
for ($i = 0; $i < sizeof($dubls); ++$i) {
$val = $dubls[$i];
$jur = JurForm::findOne($val);
$jur->CFLDUBL = 'Yes';
$jur->DUBLMDM_ID = $parentInfo['MDM_ID'];
$jur->DCHANGEDATE = date('Y-m-d H:i:s');
$jur->save();
}
return Yii::$app->getResponse()->redirect('/index.php?r=jur/analysis');
}
on the line with code $jur = JurForm::findOne($val);.
Exception:
Setting unknown property: app\models\JurForm::PKJUR.
DB: Oracle.
ActiveRecord2 has a hard time automatically mapping table names that start with a capital letter.
So for these columns you have to go into your model class and formally declare them:
public $PKJUR;
maybe better?
$parentInfo = JurForm::find()->where(['PKJUR' => $parent])->asArray()->one()
also i think PKJUR is not defined in DB.
we are using PostGreSql database, when we run following code unit, it shows error of.
Severity: Warning
Message: Illegal string offset 'server'
Filename: postgre/postgre_driver.php
Message: Cannot modify header information - headers already sent by (output started at system/core/Exceptions.php:185)
Model Code:
public function tracks_add( $id ) {
$cnt = 0;
$date = date('Y-m-d H:i:s');
$s_title = $this->input->post('s_title');
$s_singer = $this->input->post('s_singer');
$s_url = $this->input->post('s_url');
foreach ($s_title as $s_title) {
$this->db->set( array('a_id'=> $id, 's_title' => $s_title, 's_singer' => $s_singer[$cnt], 's_url' => $s_url[$cnt], 'date'=> $date) );
$this->db->insert('soundtracks');
$cnt++;
}
}
You can find this issue here :
Postgres db driver insert issue
Change line 331 to
$v = pg_version($this->conn_id); $v = isset($v['server']) ? $v['server'] : 0; // 'server' key
For some reason this works flawlesly on my out of the box WAMP yet when uploaded to web server it drops the following error:
Severity: Warning
Message: DOMDocument::load(): I/O warning : failed to load external
entity
"/mounted-storage/home155/sub004/sc81961-YHHB/* * * * * *.com/dev/
2012-10-04 03:15:03
4 2012-10-04 03:25:16
2012-09-17 19:25:11
3
0
1
1
2012-10-04 04:08:18 "
Filename: models/fuel_model.php
Line Number: 74
It's pointing to the applications root folder.
Code:
function get_pos($id)
{
$get = $this->db->query(
"SELECT * FROM holding WHERE id=1"
);
$stream = $get->row_array();
$addr = 'http://api.eveonline.com/corp/StarbaseDetail.xml.aspx?keyID='.$stream['apiid'].
'&vCode='.$stream['apikey'] . '&itemID=' . $id;
$link = new DOMDocument();
$get_cache = $this->db->query(
"SELECT * FROM cache_poses WHERE posid=".$id
);
$cache = $get_cache->row_array();
$posted = strtotime(date("Y-m-d H:i:s"));
if(!empty($cache))
{
$posted = strtotime($cache['cachedon']);
}
$current = strtotime(date("Y-m-d H:i:s"));
$mins = $posted - $current;
$expiry = $mins % 60;
if(empty($cache))
{
$link->load($addr);
$this->db->query("INSERT INTO cache_poses (data, posid) VALUES (" . $this->db->escape($link->saveXML()) . "," . $id .")");
} elseif(!empty($cache) && ($expiry >= 360)) {
$this->db->query("DELETE FROM cache_poses WHERE posid=".$id);
$link->load($addr);
$this->db->query("INSERT INTO cache_poses (data, posid) VALUES (" . $this->db->escape($link->saveXML()) . "," . $id . ")");
} elseif(!empty($cache) && ($expiry < 360)) {
$link->load($cache['data']);
}
$supply = $link->getElementsByTagName('row');
$supplies = array();
foreach ($supply as $fuel) {
$block = $fuel->attributes;
$put = $this->db->query('SELECT name FROM items WHERE typeid='.$block->item(0)->nodeValue);
$lck = $put->row_array();
$supplies[$lck['name']] = $block->item(1)->nodeValue;
}
$info = $this->implode_with_keys($supplies);
return $info;
}
Can you post the XML? It is referencing a DTD that must be accessible on your local dev machine but not on your live server. If the DTD and the entities it defines are not important to your XML, you can suppress the warning, and likely get a performance increase for not loading an extra resource, by disabling the loading of enternal entities before calling DOMDocument::load().
$link->resolveExternals = FALSE;
$link->load($cache['data']);
http://www.php.net/manual/en/class.domdocument.php#domdocument.props.resolveexternals
By the way, DOMDocument::load() accepts a file path. If your $cache['data'] is really a string of XML in the database, you need to use DOMDocument::loadXML().