More problems with dates.
Basically I'm trying to populate a table with some datetime values from Zend. The datatype of the column that I'm trying to populate is 'datetime'. However, no matter which constant I try the column is always populated with zeroes. I've tried Zend_Date::ISO_8601, Zend_Date::DATETIME, Zend_Date::now() to no avail. Pleas help.
Here's the code:
if(!$exists){
$date_time = Zend_Date::DATETIME;
/*
* Create the new site record
*/
$site_row = $site_table->createRow();
//if(isset($n_r['id'])) $row->id = $n_r['id'];
$site_row->name = $n_r['name'];
$site_row->active = 1;
$site_id = $site_table->total() + 1;
$address_id = $address_table->total() + 1;
$site_row->id = $site_id;
$site_row->address_id = $address_id;
/*
* Create the address record to go with it
*/
$address_row = $address_table->createRow();
$address_row->postcode = $n_r['postcode'];
$address_row->address_line1 = $n_r['name'];
$address_row->start_date = $date_time;
$address_row->id = $address_id;
$address_row->save();
$array[] = $site_row->toArray();
$site_row->save();
/*
* Create the new site2address record to go with it
*/
$site2address_row = $site2address_table->createRow();
$site2address_row->site_id = $site_id;
$site2address_row->address_id = $address_id;
$site2address_row->start_date = $date_time;
}else{
$array[] = $exists;
}
mySQL expects input for DATETIME columns to be
YYYY-MM-DD HH:MM:SS
independent from the locale you are using. I can't see a Zend constant for that, you may need to format it yourself.
you can use mysql function like NOW() for more convenience way. if you are at mysql code bellow may work for you.
$address_row->start_date = new Zend_Db_Expr('NOW()');
Related
basically i have three tables,
sms_content,
sms_content_cities,
sms_content_categories,
sms_content has foregin key in both other tables. i want to save record in these tables at once.
Like i want to save sms_content_id in other tables which are sms_content_cities and sms_content_categories.
Here is my code:
$model = new SmsContent();
$model->version = 0;
$model->sms_from = 1;
$model->sms_text = $inputs['sms_text'];
$date = strtotime($inputs['start_date']);
$startDate = date("Y-m-d H:i:s", $date);
$noteID = $date;
$model->start_date = $startDate;
$endDate = date("Y-m-d", strtotime($inputs['end_date']));
$model->end_date = $endDate;
$model->title_audio = (isset($inputs['title_audio'])) ? $inputs['title_audio'] : "";
$model->audio = (isset($inputs['audio'])) ? $inputs['audio'] : "";
$model->enabled = 1;
$model->ivr_enabled = 1;
$model->note_id = DB::raw($noteID);
$model->sort_order = 0;
$model->save();
this code is saving my in sms_content table , but i dont know how to save his values in other table, like i want to save sms_content "Id" in other tables as a foregin key.
After creating a record in the database, you can access its id immediately like the following:
$model->id
Now as you can access it, just create a new object of other Model and save it there.
As soon as you do $model->save(), an id is saved in the $model collection. You can simply access it using $model->id.
Try dd($model->id); after save() statement.
You can access model properties as soon as you save them in your database. In order to update your other models with data an id you need, you can simply do something like this:
$model1 = new SmsContent();
$model1->version = 0;
$model1->sms_from = 1;
//... and so on
$model1->save();
$model2 = new Model2();
$model2->sms_content_id = $model1->id;
//... and so on
$model2->save();
$model3 = new Model3();
$model3->sms_content_id = $model1->id;
//... and so on
$model3->save();
I want to know how can I work with dates in MySQL and PHP. I want to know how many days remain, i want to know how many day is defference(distance) between nowTime and createTime ?
<?php
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
////ISSET
if (isset($_POST['projectId'])) {
$projectId = $_POST['projectId'];
$result = mysql_query("SELECT *FROM projects WHERE projectId='$projectId'") ;
if (mysql_num_rows($result)>0) {
$result = mysql_fetch_array($result);
$response["title"]=$result["title"];
$response["exp"]=$result["exp"];
$response["userIdKarmand"]=$result["userIdKarmand"];
$response["userIdKarfarma"]=$result["userIdKarfarma"];
$response["cost"]=$result["cost"];
$response["zemanat"]=$result["zemanat"];
$response["time"]=$result["time"];
$response["createTime"]=$result["createTime"];
$response["type"]=$result["type"];
$response["nowTime"]=DATE("y-m-d");
$response["remainTime"]=DATE("y-m-d")- strtotime($response["createTime"])+$response["time"];
echo json_encode($response);
}
else {
}
}
else {
}
?>
It's not working. What can I do? I want compare createtime and nowtime and find out how many days I have time?
strtotime converts formatted string date format to timestamp. You can't substract timestamp data from string data. You have to first convert $response["remainTime"] to timestamp using strtotime() and then substract strtotime($response["createTime"]) from this value.
The best way to go through a MySQL result would be to loop through the fetch_assoc() function.
$result = mysql_query("SELECT * FROM projects WHERE projectId = '$projectId'");
// while() through an associative array from the MySQL object
while($result =& $result->fetch_assoc()) {
$response["title"] = $result["title"];
$response["exp"] = $result["exp"];
$response["userIdKarmand"] = $result["userIdKarmand"];
$response["userIdKarfarma"] = $result["userIdKarfarma"];
$response["cost"] = $result["cost"];
$response["zemanat"] = $result["zemanat"];
$response["time"] = $result["time"];
$response["createTime"] = $result["createTime"];
$response["type"] = $result["type"];
$response["nowTime"] = DATE("Y-m-d H:i:s"); // this is how you would need to format dates to work with MySQL
$remainTime = (strtotime($response['nowTime']) - strtotime($response['createTime'])); // this would return how many seconds have passed since $response['createTime'] until now
$response["remainTime"] = $remainTime;
}
echo json_encode($response);
This would allow you to have how many seconds since $response['createTime'] held in the $remainTime variable.
I am trying to get last inserted id in cake php 3.
if($this->request->is('post')){
$this->loadModel('MyPlaylists');
$playlistData = [];
$playlistData["user_id"] = $_POST['user_id'];
$playlistData["play_list_name"] = $_POST['play_list_name'];
$playlistData["section"] = $_POST['section'];
$playlistData["created"] = date('Y-m-d H:i:s', strtotime('now'));
$playlistData["status"] = 'active';
$playlist_en = $this->MyPlaylists->newEntity();
$this->MyPlaylists->patchEntity($playlist_en, $playlistData);
$this->MyPlaylists->save($playlist_en);
$id=$this->MyPlaylists->id;
}
I have also tried:
$id=$this->MyPlaylists->lastInsertedId;
But it is giving error as below screenshot:
Your code needs some fine tuning.
$playlist_en = $this->MyPlaylists->newEntity();
$playlist_en = $this->MyPlaylists->patchEntity($playlist_en, $playlistData);
$isValid = $this->MyPlaylists->save($playlist_en);
if ($isValid) {
$id = $playlist_en->id;
debug($id); // Check your last insert id
} else {
pr($playlist_en->errors()); // Check errors
}
Make a query selecting the first element of the query ordered in DESC. way
$newPlaylist = $this->Playlists->find('all',['order' => ['Playlists.id' => 'DESC']])->select(['id']);
i have this sql query written in php:
$query = sprintf("UPDATE bank_info SET
amount_dollar = amount_dollar +'$amount_dollar' ,
amount_euro = amount_euro + '$amount_euro' ,
amount_local = amount_local + '$amount_local'
WHERE bank_id = '$bank_id' ");
this query works fine, but i want to transform this query using FluentPDO.
i want to use arrays to SET the values .
for example:
$table='bank_info'; //table name
$arrVal=array(); //values needs to be SET
$arrVal['amount_dollar = amount_dollar+?']=$amount_dollar;
$arrVal['amount_euro = amount_euro+?']=$amount_euro;
$arrVal['amount_local = amount_local+?']=$amount_local;
$arrWhere=array(); //where condition
$arrWhere['bank_id']=$bank_id;
this is the query:
$query = $this->pdo->update($table)->set($arrVal)->where($arrWhere);
$query->execute();
I think the problem is in the $arrVal, cant find the proper way to SET and add value to the current value for a column in the table.
I used array to select and get values from the DB/tables for many times so i think the $arrWhere is not the problem.
well, found the answer,
for ex.:
This is working for me:
$id = 5;
$field = 'stock';
$amount = 1;
$increment = array($field => new FluentLiteral($field.' + '.$amount));
$fpdo->update('products')->set($increment)->where('id', $id)->execute();
i'm trying to count the views of a post using cakephp but the saving is not happinging using this code
$this->autoRender=false;
$unformattedData = $this->GeneralNews->findById($id);
$data = $unformattedData['GeneralNews'];
$total = $data['views'];
$total = $total+1;
$this->GeneralNews->views =$total;
print_r($this->GeneralNews->views);
$this->GeneralNews->save($this->request->data);
the print_r show me the views that's already on the table adding to them +1 .. but it's never saved in the DB .. what is the problem ?
Try this code :
$unformattedData = $this->GeneralNews->findById($id);
$unformattedData['GeneralNews']['views'] = $unformattedData['GeneralNews']['views'] + 1;
$this->GeneralNews->save($unformattedData);
Perhaps even easier:
$this->GeneralNews->id = $id;
$views = $this->GeneralNews->field('views');
$this->GeneralNews->saveField('views', $views + 1);
Hope this helps.