How to datetime format accurate asia/jakarta - codeigniter

I have input data for date and time but after being stored in the database the time is not in accordance with the time of day I was in Asia/Indonesia. What's the solution?
public function permohonan()
{
$id_permohonan = $this->input->post("id_permohonan", true);
$id_member = $this->session->userdata('id_member');
$data_diminta = $this->input->post("data_diminta", true);
$tujuan_penggunaan = $this->input->post("tujuan_penggunaan", true);
$status_permohonan = 1;
$tanggal = date("Y-m-d H:i:s");
$keterangan = "";
$data = array(
'id_permohonan' => $id_permohonan,
'id_member' => $id_member,
'data_diminta' => $data_diminta,
'tujuan_penggunaan' => $tujuan_penggunaan,
'status_permohonan' => $status_permohonan,
'tanggal' => $tanggal,
'keterangan' => $keterangan,
);
$this->Tb_permohonan_model->insert($data);
$this->session->set_flashdata('message', 'Create Record Success');
redirect(site_url('frontend'));
}
And the data stored in the database is: 2018-08-13 05:14:21
this is not accurate with my time.

this may help you.
In the application/config/autoload.php put this below line.
date_default_timezone_set("Asia/Indonesia");
It will set the default time zone.

You can set default timezone in config file e.g: date_default_timezone_set(Asia/Jakarta).

Related

How to Automatically update expiry date from month in laravel?

I have to update the expiry date by counting from the month. Please guide me. While saving data expiry date is working fine. But I don't understand how to update it.
My Controller Code For Save Data
public function pay_success(Request $request){
$input = $request->all();
date_default_timezone_set('asia/calcutta');
$input['months'] = $request->months;
$expiry_date = Carbon::now()->addMonths($input['months']);
$input['expiry_date'] = $expiry_date;
$input['password'] = bcrypt($input['password']);
$user = User::create($input);
//Send Email
$email = $input['email'];
$messageData = ['email' =>$input['email'],'name' =>$input['name'],'package' =>$input['package'],'months' =>$input['months'],'amount' =>$input['amount'],'expiry_date' =>$input['expiry_date']];
Mail::send('emails.mail',$messageData,function($message) use($email){
$message->to($email)->subject('Registration with AddSpy');
});
$arr = array('msg' => 'Payment successful.', 'status' => true);
return Response()->json($arr);
}
My Update Code is
public function update(Request $request) {
date_default_timezone_set('asia/calcutta');
$months = $request->months;
$expiry_date = Carbon::now()->addMonths($months);
$request['expiry_date'] = $expiry_date;
$data = ['id'=>$request->id, 'name'=>$request->name, 'phone'=>$request->phone, 'country'=>$request->country, 'state'=>$request->state,
'purpose'=>$request->purpose, 'package'=>$request->package, 'months'=>'$months', 'quantity'=>$request->quantity, 'amount'=>$request->amount, 'expiry_date'=>'$expiry_date'];
DB::table('users')->where('id',$request->id)->update($data);
return response()->json($data);
}
Anyone please suggest me a answer. I do changes in my code but It gives this message "message": "SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '$expiry_date' for column addspy.users.expiry_date at row 1 (SQL: update users set id = 47, name = Ayush, phone = 6393611129, country = India, state = UP, purpose = parent, package = basic, months = $months, quantity = 1, amount = 4000, expiry_date = $expiry_date where id = 47)",
"exception": "Illuminate\Database\QueryException",
Thanks in advance
You're sending strings to the database ('$months' & '$expiry_date'). Simply removing the quotes should fix your problem.
i.e.
$data = [
'id' => $request->id,
'name' => $request->name,
'phone' => $request->phone,
'country' => $request->country,
'state' => $request->state,
'purpose' => $request->purpose,
'package' => $request->package,
'months' => $months,
'quantity' => $request->quantity,
'amount' => $request->amount,
'expiry_date' => $expiry_date,
];

How to update in mysql using codeigniter

How can I update my data into my database? There is no error but it doesn't update in my database
This is my model
public function updateNewServices($prod_name,$prod_id,$service_type,$service_gl_acc,$min_amnt,
$def_amnt,$max_amnt,$client,$services_id)
{
$services_id = $this->db->escape_str($services_id);
$prod_name = $this->db->escape_str($prod_name);
$prod_id = $this->db->escape_str($prod_id);
$service_type = $this->db->escape_str($service_type);
$service_gl_acc = $this->db->escape_str($service_gl_acc);
$min_amnt = $this->db->escape_str($min_amnt);
$def_amnt = $this->db->escape_str($def_amnt);
$max_amnt = $this->db->escape_str($max_amnt);
$client = $this->db->escape_str($client);
$basic_data = array(
"prod_name" => $prod_name,
"prod_id" => $prod_id,
"service_type" => $service_type,
"service_gl_acc" => $service_gl_acc,
"min_amnt" => $min_amnt,
"def_amnt" => $def_amnt,
"max_amnt" => $max_amnt,
"client" => $client
);
$this->db->where("services_id=",$services_id);
$this->db->update("services",$basic_data);
}
1) First you should change line $this->db->where('services_id', $services_id);
2) second one you should check data type in database and update data value type also .
3) You are using escape_str. so confirm with each data value data type and with db also.

how to get last inserted id while inserting..but it inserting same the same id..how to resolve this

controller
i need to insert store_merchant_id from $data..but in my coding it takes only the first id..
$data = array(
'merchant_firstname' => $merchant_firstname,
'merchant_lastname' => $merchant_lastname,
'merchant_password' => $merchant_password,
'merchant_email'=>$merchant_email,
);
$merchant_id = Merchant_model::merchant_submit($data);
$data1 = array(
'store_merchant_id'=>$merchant_id,
'store_name'=>$store_name,
'store_phone'=>$store_phone,
'store_address1'=>$store_address1,
);
$return = Merchant_model::merchant_submit1($data1);
Model
return DB::table('le_store')->insert($data1);
I guess you need to get the last inserted id and do something with that
So, i am doing the same what you did
Step 1 : Building the input
1.
$data = array(
'merchant_firstname' => $merchant_firstname,
'merchant_lastname' => $merchant_lastname,
'merchant_password' => $merchant_password,
'merchant_email'=>$merchant_email,
);
Step 2 : Saving the Data
$Merchant = Merchant_model::create($data);
Step 3 : Getting the inserted id
return $Merchant->id;
Now the $Merchant->id will have the last inserted id, You shall return or save this to another model that you wish to do.
Update :
As you want to update the inserted id to another table, Just do this
As usual like previous
$data = array(
'merchant_firstname' => $merchant_firstname,
'merchant_lastname' => $merchant_lastname,
'merchant_password' => $merchant_password,
'merchant_email'=>$merchant_email,
);
$Merchant = Merchant_model::create($data);
Then build $data1
$data1 = array(
'store_merchant_id'=>$Merchant->id,
'store_name'=>$store_name,
'store_phone'=>$store_phone,
'store_address1'=>$store_address1,
);
Note :
store_merchant_id'=>$Merchant->id' should be like this
Then just create it
Merchant_model::create($data1);
Hope this helps you

CodeIgniter: Insert a row into MySQL with timestamp

In CodeIgniter, I want to insert a row into a db that includes a timestamp for "now".
$message_row = array(
"sender_email" => $recruiter_email,
"recipient_email" => $recruiter_email,
"message" => $recruiter_email,
"send_date" => "NOW()" // hmmm how do I do this?
);
$this->db->insert('messages', $message_row);
If I use "NOW()" it will just enter the string, and not actual have MySQL use its NOW keyword.
In order to use NOW() function in CodeIgniter you need to use "set" function
$table = 'user';
$message_row = array(
"sender_email" => $recruiter_email,
"recipient_email" => $recruiter_email,
"message" => $recruiter_email
);
$this->db->set('send_date', 'NOW()', false);
$this->db->insert($table, $message_row);
if you need to be timezone aware, i recommend you store the datetime in UTC format.
so:
$now = new DateTime ( NULL, new DateTimeZone('UTC'));
$message_row = array(
"sender_email" => $recruiter_email,
"recipient_email" => $recruiter_email,
"message" => $recruiter_email,
"send_date" => $now->format('Y-m-d H:i:s')
);
when you retrieve the information, you have to know the user's timezone, using a $row that contains the send_date:
$then = new DateTime( $row->send_date, new DateTimeZone('UTC'));
$then->setTimeZone(new DateTimeZone($userstimezone));
echo $then->format('Y-M-D H:i:s');

Laravel 4 - Return the id of the current insert

I have the following query
public static function createConversation( $toUserId )
{
$now = date('Y-m-d H:i:s');
$currentId = Auth::user()->id;
$results = DB::table('pm_conversations')->insert(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
return $results;
}
How would i return the id of the row just inserted?
Cheers,
Instead of doing a raw query, why not create a model...
Call it Conversation, or whatever...
And then you can just do....
$result = Conversation::create(array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now ))->id;
Which will return an id...
Or if you're using Laravel 4, you can use the insertGetId method...In Laravel 3 its insert_get_id() I believe
$results = DB::table('pm_conversations')->insertGetId(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
This method requires that the id of the table be auto-incrementing, so watch out for that...
The last method, is that you can just return the last inserted mysql object....
Like so...
$result = DB::connection('mysql')->pdo->lastInsertId();
So if you choose that last road...
It'll go...
public static function createConversation( $toUserId )
{
$now = date('Y-m-d H:i:s');
$currentId = Auth::user()->id;
$results = DB::table('pm_conversations')->insert(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
$theid= DB::connection('mysql')->pdo->lastInsertId();
return $theid;
}
I would personally choose the first method of creating an actual model. That way you can actually have objects of the item in question.
Then instead of creating a model and just save()....you calll YourModel::create() and that will return the id of the latest model creation
You can use DB::getPdo()->lastInsertId().
Using Eloquent you can do:
$new = Conversation();
$new->currentId = $currentId;
$new->toUserId = $toUserId;
$new->ip = Request::getClientIp();
$new->time = $now;
$new->save();
$the_id = $new->id; //the id of created row
The way I made it work was I ran an insert statement, then I returned the inserted row ID (This is from a self-learning project to for invoicing):
WorkOrder::create(array(
'cust_id' => $c_id,
'date' => Input::get('date'),
'invoice' => Input::get('invoice'),
'qty' => Input::get('qty'),
'description' => Input::get('description'),
'unit_price' => Input::get('unit_price'),
'line_total' => Input::get('line_total'),
'notes' => Input::get('notes'),
'total' => Input::get('total')
));
$w_id = WorkOrder::where('cust_id', '=', $c_id)->pluck('w_order_id');
return $w_id;

Resources