What I want is to make a database update with codeigniter where I put a value to current timestamp:
My settings array:
$settingsMachineUpdateEvent[] = array(
'id' => $eventId,
'etid' => '5',
'insert_by' => '2',
'modified_date' => $currentTime
);
And my $currentTime:
$currentTime = date_default_timezone_get();
And I send the update like this:
$this->db->update_batch('balance_events', $settingsMachineUpdateEvent, 'id');
My problem is the $currentTime var, I want to assign it the current time stamp so I can update my modified_date column from mysql with the current timestamp value. I also try'ed with NOW(), time(), and assign it directly from array, nothing worked, I will get 0000-00-00 00:00:00 after the update. Where is the issue? Thanks!
Instead of this:
$currentTime = date_default_timezone_get();
try
$currentTime = date('Y-m-d H:i:s'); // for current time
$currentTime = time(); // for current timestamp
Explanation: date_default_timezone_get() is used to get the timezone, but in your case you need the time or timestamp to store in the table.
Related
I am trying to select a record from specified date
$yesterday = date('Y-m-d',strtotime("-1 days"));
$this->db->get_where('tablename', array('postid' => $dailystat['postid'], 'timestamp >=' => $yesterday));
But i am not getting any record even if there are entries in table. I also want to make sure that query select the only record which was created on specified date.
Any help will be appreciated..
$yesterday = date('Y-m-d',strtotime("-1 days"));
$this->db->get_where('tablename', array('postid' => $dailystat['postid'], 'timestamp >=' => $yesterday, 'timestamp <' => date('Y-m-d')));
If you share your table schema and sample data, I can give you correct answer. Still I can guess you are compairing date string with timestamp.
The code $yesterday = date('Y-m-d',strtotime("-1 days")); will return $yesterday value as '2017-04-10'. But actually in your database you are compairing with timestamp field, which hold the timestamp in numeric value.
You can use php strtotime function to convert any date to respective time stamp. strtotime($yesterday).
Correct Code will be :
$yesterday = date('Y-m-d',strtotime("-1 days"));
$this->db->get_where('tablename', array('postid' => $dailystat['postid'], 'timestamp >=' => strtotime($yesterday)));
Again please make sure, your database field timestamp is storing only date in form of timestamp.
Another solution is, you can use mysql date compare functions.
I am working on a WordPress plugin that allows the user to select a time using a jQuery dropdown script. The time is a string in the format of "5 : 34 PM". I need to save that value to my mysql database in the datetime format.
So far, I can save the date and time but the AM/PM is not being factored in.
Here is my PHP function:
function db_tables_insert() {
global $wpdb;
$table_name = $wpdb->prefix . 'tweettweet';
$tweet = $_POST["tweet"];
$time = $_POST["timepicker"];
$time=preg_replace('/\s+/', '', $time);
$date = "2015-02-08:";
$datetime = $date.$time;
$wpdb->insert(
$table_name,
array(
'time' => $datetime,
'text' => $tweet,
)
);
}
In this example, the value saved to the database would be "2015-02-08 12:13:00" (assuming the user selected 12:13 for the time). The problem is, that value is the same whether the user selects 12:13 am or 12:13 pm.
I need a way to convert the string so that the "am/pm" is taken into consideration when saved to the database.
strtotime should be able to handle this.
echo date('Y-m-d H:i:s',strtotime('2014-12-12 12:34 AM'));
returns 2014-12-12 00:34:00
I'm trying to store user signup date and time using codeigniter 3.0.6 and using
NOW()
as
$this->db->set('user_nicename', $nameVar);
$this->db->set('user_login', $emailVar);
$this->db->set('user_pass', $passwordVar);
$this->db->set('user_registered', 'NOW()');
$this->db->insert('doc_users');
but it is not storing date time in database
see database image
use date() like this
date('Y-m-d H:i:s'); # output 2015-12-22 16:41:25
Final Code is
date_default_timezone_set('Asia/Karachi'); # add your city to set local time zone
$now = date('Y-m-d H:i:s');
$this->db->set('user_nicename', $nameVar);
$this->db->set('user_login', $emailVar);
$this->db->set('user_pass', $passwordVar);
$this->db->set('user_registered', $now);
$this->db->insert('doc_users');
this works for me in Codeigniter 3.0.6
$this->db->set('user_registered', 'NOW()', FALSE);
try this
first load date helper
To follow ci structure you have to use mdate() function otherwise you can also use date function
$this->db->set('user_nicename', $nameVar);
$this->db->set('user_login', $emailVar);
$this->db->set('user_pass', $passwordVar);
$this->db->set('user_registered', mdate("%Y-%m-%d %H:%i:%s"));
$this->db->insert('doc_users');
I would recommend using load the date helper.
mdate('%Y-%m-%d %H:%i:%s', now());
There is no need to use multiple sets you could simply put it in a array.
$data = array(
'user_nicename' => $nameVar,
'user_login' => $emailVar,
'user_pass' => $passwordVar,
'user_registered' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$this->db->set($data);
$this->db->insert('doc_users');
Also make sure your user_registered database type is DATETIME
To set your current date_time_zone
Go to the config.php and add the code below
Example: date_default_timezone_set('Pacific/Auckland');
For your time zone you can find list here http://php.net/manual/en/timezones.php
$data = [
'type'=>$this->input->post('txttype')
//for getting input value of form
];
$this->db->set('column_name', 'NOW()', FALSE);
$this->db->insert('table_name', $data);
Also you can make a DB query to get current DB date/time:
$this->db->select('NOW() as db_date')->get()->row()->db_date
In Codeigniter, you have to load date helper as
$this->load->helper('date');. than after you have to define city for
timezone as date_default_timezone_set('Asia/Kolkata');.than after
you can set your time zone according your requirnment.
$this->load->helper('date'); // load Helper for Date
date_default_timezone_set("UTC");
echo $date=gmdate("F j, Y").'<br>'; // ie. May 23, 2018
if (function_exists('date_default_timezone_set'))
{
date_default_timezone_set('Asia/Kolkata'); // Specify your time zone according to your city
}
date_default_timezone_set('Asia/Kolkata'); // Defined City For Timezone
$currentDate =time();
$datestring = '%Y-%m-%d - %h:%i %a';
$time = time();
$better_date= mdate($datestring, $time).'<br>'; // i.e : 2018-05-23 - 09:52 am | For AM | PM result
$c_date=date("Y-m-d H:i:s").'<br>'; // 2018-05-23 09:52:36 | For Seconds Result
// Insert Date and Time to the Database
$data=array(
'name' =>'Rudra',
'email' =>'test#me.com',
'city' =>'Kolakata',
'data_time_on'=>$c_date
);
$this->db->insert('tableName',$data);
I am working with the DateTime object and have this problem to obtain the activity of a specific day.
In the controller I do the following query:
$date = new \DateTime('today');
$activity = $em->getRepository('MyBundle:myEntity')->findOneBy(array(
'activity_date' => $date
));
Result for this query is null, but when I define the parameter date in this way:
$date = new \DateTime('Wednesday, January 14, 2015');
I get the activity that matches this date. Why doesn't today work?
I believe that commenter #prodigitalson is right. When you say today that is in fact now which is formatted with YYYY-MM-DD HH:MM:SS.
Now, if your DBMS column is of type date, DBMS will first normalize two values and only then proceed with comparing.
If your incoming parameter has a value of 2014-01-14 16:47:20, comparing it to DBMS value of 2014-01-14 00:00:00 will no match the record.
Try the following:
$date = new \DateTime(); // no need for explicit `today`
$date->setTime(0,0,0); // reset hours, minutes and seconds to 0
$activity = $em->getRepository('MyBundle:myEntity')->findOneBy(array(
'activity_date' => $date
));
Will this work?
code igniter date helper in not working for me. I initialized the date helper and while inserting in database. I see that field with timestamp are returning 0000-00-00 . Code for insertion is:
function add_todo($list)
{
date_default_timezone_set("Asia/kathmandu");
$date = now();
$data = array(
'list' => $list,
'dates'=> now()
);
$insert = $this->db->insert('todo', $data);
}
I cannot get is working. What is the problem here.
Id imagine its not working because you don't have the correct configuration.
This is from the codeigniter wiki for the now() function:
Returns the current time as a Unix timestamp, referenced either to your server's local time or GMT, based on the "time reference" setting in your config file. If you do not intend to set your master time reference to GMT (which you'll typically do if you run a site that lets each user set their own timezone settings) there is no benefit to using this function over PHP's time() function.
try using this
$datetime = date('Y-m-d H:i:s');
This would be revised code but I'm not at a machine were i can test it. However, this should work..
function add_todo($list)
{
date_default_timezone_set("Asia/kathmandu");
$datetime = date('Y-m-d H:i:s');
$data = array(
'list' => $list,
'dates'=> $datetime
);
$insert = $this->db->insert('todo', $data);
}
You can try out this
date_default_timezone_set('Asia/Kathmandu');
$dateTime = date('Y-m-d H:i:s');
$data = array(
'date'=> $datetime
);
$insert = $this->db->insert('todo', $data);