Codeigniter timezone mysql settings - mysql

Just realised WHY my site is now showing all datetime variables as -1 hr... I'm using Codeigniter for the first time! (Never had this problem before)
So, I have included the following code in my main index.php file
/*
|---------------------------------------------------------------
| DEFAULT TIMEZONE
|---------------------------------------------------------------
|
| Set the default timezone for date/time functions to use if
| none is set on the server.
|
*/
if( ! ini_get('date.timezone') )
{
date_default_timezone_set('GMT');
}
However, it's still showing as -1 hr, so I'm assuming I need to set some sort of default setting for MySQL...
I have included the following line of code in my model:
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->db->query("SET time_zone='+0:00'");
}
Still no difference... Help!
My code is:
<h3><?=date('D, jS F # g:ia', strtotime($row->datetime))?></h3>
The $row->datetime variable is nothing more than a DATETIME column value from my MySQL database. The echoed variable in view is ALWAYS 1 hour less than the value in my database...
My model code is:
function coming_up()
{
$this->db->query("SET time_zone='+0:00'");
$query = $this->db->query('SELECT * FROM events1 WHERE datetime >= NOW() ORDER BY datetime LIMIT 2');
return $query->result();
}

In config/autoload.php, set a model to load on each page load. then call $this->db->query("SET time_zone='+0:00'"); in that model constructor.
config/autoload.php
$autoload['model'] = array('default_model');// for ex, "say default_model"
In application/models, create a new model file with name of "default_model.php" and add below code.
application/models/default_model.php
class Default_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->db->query("SET time_zone='+0:00'");
}
}
On each page load, this constructor will be called and mysql timezone will be set to +0:00.

Add these line in your config file and then check, it is working for me
$config['time_reference'] = 'gmt';# Default should be GMT
date_default_timezone_set('UTC');# Add this line after creating timezone to GMT for reflecting

am also face this problem.
i tried this way..
$this->db->query("SET LOCAL time_zone='Asia/Kolkata'");
$query="SELECT * FROM offers where NOW() between offfer_from and offer_to";
$res=$this->db->query($query);
it will work fine in my project and i'm not using anything new default model.. if you want global solution means you can use auto-load some model.

SET default time_zone for MySQL :
Go to : Your_Project/system/core/Model.php And then update this function :
public function __construct() {
$this->db->query("SET time_zone='YOUR_TIME_ZONE'");
}
SET default_time_zone for PHP :
Go to Your_Project/index.php
And then update here :
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
date_default_timezone_set('YOUR_TIME_ZONE');

I like the solution proposed by Kumar however, I didn't need to set this globally, only when showing dates stored in UTC time in my db.
In my api model I have the following function.
public function setDBTimeOffset(){
$dt = new DateTime();
$offset = $dt->format("P");
$result = $this->db->query("SET time_zone='$offset';");
return $result;
} # END FUNCTION setDBTimeOffset
From my controller, I call the following first.
$this->api->setDBTimeOffset();
Followed by the call to the function that queries the db. For example, I am fetching some user history.
$data["viewData"]["allHistory"] = $this->api->getUserHistory("ALL", 0, 10000);
When I display the output, the dates are in my timezone. I hope this helps someone, somewhere in time (couldn't resist).

You can set it in the index.php file in your project folder on top and after <?php
<?php
date_default_timezone_set('Asia/Bangkok');

Related

Laravel 6 - Updating the model timestamps

After some actions inside of my Laravel 6 app, I'm trying to update my model object $last (class CurrentConditions). As you can see below, I'm trying to update update_time property to current timestamp. Except this property, I have another timestamp property inside the CurrentConditions model: external_update_time and I want this property to stay the same after updating and saving $last object. Problem is that external_update_time property is updated to current time after saving the model.
/** #var CurrentConditions $last */
$last = $this->getLastUpdatedCurrentConditions($cityID);
$last->update_time = Carbon::now("UTC");
$last->save();
Here is my model:
class CurrentConditions extends Model
{
protected $table = 'current_conditions';
...
public $timestamps = false;
protected $dates = ['update_time', 'external_update_time'];
...
}
And here is my migration code:
Schema::create("current_conditions", function (Blueprint $table) {
$table->bigIncrements("current_conditions_id");
...
$table->timestamp("external_update_time");
$table->timestamp("update_time")->useCurrent();
});
So why is external_update_time updating as well? Thanks for any help.
Please check that external_update_time is not set on Update to set CurrentTimeStamp
in the schema table design
This is how MySQL 5.7 works - first timestamp will be updated with current timestamp. You should modify order of columns in your migration (assuming it's not deployed yet to live server) to achieve what you want:
$table->timestamp("update_time")->useCurrent();
$table->timestamp("external_update_time");

Need to change timezone dynamically for existing project

There is an existing project only for one country. But for now, it should be used for multiple countries. So I need some place to change timezones. I decided to update & insert in only London/Sydney timezone. I need to change the timezone for all select queries. I just tried with middlewares, but I could not achieve it. Please give your suggestions.
Linux server, MySQL, Laravel, Vagrant
namespace App\Http\v2018_06_12\Middleware;
use App;
use App\Order;
use Closure;
class LocaleMiddleware
{
public function handle($request, Closure $next, $guard = null)
{
$locale = ($request->hasHeader('locale')) ? $request->header('locale') : 'uk';
$timezone = env('APP_TIMEZONE');
if ('aus' == $locale) {
$timezone = 'Australia/Sydney';
}
config(['app.timezone' => $timezone]);
date_default_timezone_set($timezone);
print_r(Order::select('delivery_time')->orderBy('id', 'DESC')->first()->toArray());
return $next($request);
}
}

How to return a default column value for mass assignment of a model?

I am using mass assignment to create a server model like so:
/** #var Model $server */
$server = Server::create($request->all());
return response($server->jsonSerialize(), Response::HTTP_CREATED);
Now my server also has a status property which maps to a MySQL status column which has a default value of 0 in MySQL.
Because the status is not being set implicitly in my request attributes(no need since the default is fine) it does not return the status property on the model. It will return all of the properties in my $request->all() though. These are validated and coming from a form.
How can I return my full $server model including the default value for status? I am forced to fire off another query to re-fetch the model I have just created, just so I can also include the status property with the MySQL default?
It doesn't do a selecet after an insert so you wont get any values that the DB has to set itself. You can refresh the model instance though (cause it to select that row):
$model->refresh();
If you're returning it as JSON, you can add status to protected $appends = ["status"];
class Server extends Model {
protected $appends = ["status"];
...
}
That way, even if you don't set status in your Server::create() method, it should still be visible in your JSON response as server.status. Also, perhaps adjust your return to
return response()->json(["server" => $server], 200); // or whatever HTTP code you need.
Since $appends only works with accessors, perhaps use $request->merge() to "fill in the gaps" for data you want returned in your JSON response:
$request->merge([
"status" => "active", // Or whatever the default is from the database
...
]);
Since the response is returning everything in $request->all(), merge it with default values from MySQL. Note: currently hardcoded, would need to be updated if migration/schema changed, or via SQL command to get defaults.

Calling stored procedure in codeigniter

I am using latest codeigniter and trying to call stored procedure from my model. Also I am using mysqli as database driver. Now I am having an error when I call two stored procedures. Following is the error:
Error Number: 2014
Commands out of sync; you can't run this command now
call uspTest();
Filename: E:\wamp\www\reonomy-dev\system\database\DB_driver.php
Line Number: 330
Note that when I call a single stored procedure it works fine. Here is the code for model.
class Menus_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function getMenus()
{
$query = $this->db->query("call uspGetMenus()");
return $query->result();
}
public function getSubMenus()
{
$query = $this->db->query("call uspTest()");
return $query->result();
}
}
Here is the code from controller
class MYHQ extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('menus_model');
}
public function index()
{
$menu = $this->menus_model->getMenus();
$submenu = $this->menus_model->getSubMenus();
}
}
Is there any solution without hacking the core of codeigniter??
I follow the blog of Mr. Tim Brownlaw:
http://ellislab.com/forums/viewthread/73714/#562711
First, modify application/config/config.php, line 55.
$db['default']['dbdriver'] = 'mysqli'; // USE mysqli
Then, add the following into mysqli_result.php that is missing this command for some strange reason (under /system/database/drivers/mysqli/mysqli_result.php).
/**
* Read the next result
*
* #return null
*/
function next_result()
{
if (is_object($this->conn_id))
{
return mysqli_next_result($this->conn_id);
}
}
Then, in your model, add $result->next_result().
Below is my example.
function list_sample($str_where, $str_order, $str_limit)
{
$qry_res = $this->db->query("CALL rt_sample_list('{$str_where}', '{$str_order}', '{$str_limit}');");
$res = $qry_res->result();
$qry_res->next_result(); // Dump the extra resultset.
$qry_res->free_result(); // Does what it says.
return $res;
}
Having the same problem I found another approach which doesn't change the core, but instead uses a small helper.
Edit: The below linked asset is nowhere to be found.
See CoreyLoose post.
https://ellislab.com/forums/viewthread/71141/#663206
I had to make a small adjusment to his helper though. The line
if( get_class($result) == 'mysqli_stmt' )
could possibly produce a warning since the $result sometimes is passed as a boolean. I just put a check prior to this line and now it works perfectly, with no tinkering with the core!
This seems to be a bug in CodeIgniter. How come it's still in there is beyond me.
However, there's a couple of ways to overcome it.
Check here: http://codeigniter.com/forums/viewthread/73714/
Basically, you modify mysqli_result.php to include next_result() function and make sure to call it after every stored proc. call.
Just note that it assumes you're using mysqli as your DB driver... but you can probably do something similar with any other. You can change your driver in /application/config/database.php It's the line that says
$db['default']['dbdriver'] = 'mysql';
by default. Change it to:
$db['default']['dbdriver'] = 'mysqli';
You could also just close/reopen a DB connection between the calls, but I would definitely advise against that approach.
change dbdriver to "mysqli"
put this function to your model and use it to call stored procedure
function call( $procedure )
{
$result = #$this->db->conn_id->query( $procedure );
while ( $this->db->conn_id->next_result() )
{
//free each result.
$not_used_result = $this->db->conn_id->use_result();
if ( $not_used_result instanceof mysqli_result )
{
$not_used_result->free();
}
}
return $result;
}

session_regenerate_id and database handler

i am using database handler for my sessions which working fine but now i stack into a problem on authentication.
When user login with username/password i do session_regenerate_id and after that i am trying to select the current session_id.
Here is my code
session_regenerate_id();
echo $checkQ=" SELECT * FROM my_sessions WHERE id='".session_id()."' ";
......
but i dont get any results. The session_id is the correct one.
After finish load the page and copy paste the SQL Command to phpMyAdmin i get the results.
I know thats its stupid but the only reason i can think of is that session_regenerate_id() "is too slow" so when i try to read the session_id at next line the session_id has not created in database yet.
Can anyone help me!
I know it has been a while, I hope you have found an answer since this was posted, but I'll add my solution for posterity's sake.
The call to session_generate_id() will cause the value of session_id() to change:
<?php
$before = session_id();
session_regenerate_id();
$after = session_id();
var_dump($before == $after); // outputs false
This problem manifested for me because in the session write handler I was doing this (without such bogus method names, of course):
<?php
class MySQLHandler
{
function read($id)
{
$row = $this->doSelectSql($id);
if ($row) {
$this->foundSessionDuringRead = true;
}
// snip
}
function write($id, $data)
{
if ($this->foundSessionDuringRead) {
$this->doUpdateSql($id, $data);
}
else {
$this->doInsertSql($id, $data);
}
}
}
The write() method worked fine if session_regenerate_id() was never called. If it was called, however, the $id argument to write() is different to the $id passed to read(), so the update won't find any records with the new $id because they've never been inserted.
Some people suggest to use MySQL's "REPLACE INTO" syntax, but that deletes and replaces the row, which plays merry havoc if you want to have a creation date column. What I did to fix the problem was to hold on to the session ID that was passed to read, then update the session ID in the database during write using the id passed to read as the key:
<?php
class MySQLHandler
{
function read($id)
{
$row = $this->doSelectSql($id);
if ($row) {
$this->rowSessionId = $id;
}
// snip
}
function write($id, $data)
{
if ($this->rowSessionId) {
$stmt = $this->pdo->prepare("UPDATE session SET session_id=:id, data=:data WHERE session_id=:rowSessionId AND session_name=:sessionName");
$stmt->bindValue(':id', $id);
$stmt->bindValue(':rowSessionId', $this->rowSessionId);
$stmt->bindValue(':data', $data);
$stmt->bindValue(':sessionName', $this->sessionName);
$stmt->execute();
}
else {
$this->doInsertSql($id, $data);
}
}
}
I think I'm having the same problem you are having. It's unclear to me whether this is a PHP (cache) feature or a bug.
The issue is that, when using a custom SessionHandler and calling session_regenerate_id(true), the new session is not created until the script terminates. I have confirmed that by doing the same thing you did: SELECTing the new session id from the database. And the new session is not there. However, after the script finishes, it is.
This is how I fixed it:
$old_id = session_id();
// If you SELECT your DB and search for $old_id, it will be there.
session_regenerate_id(TRUE);
$new_id = session_id();
// If you SELECT your DB for either $old_id or $new_id, none will be there.
session_write_close();
session_start();
// If you SELECT your DB for $new_id, it will be there.
Therefore the solution (workaround) I came about was to force PHP to write the session. I hope this helps.