I have data in MySQL table like :
ID dates value
1 2011-12-18 10:11:12 test1
2 2011-12-18 12:11:12 test2
3 2011-11-18 10:19:11 test3
When I tried to get value in module it not work, I write code like :
$query=$this->db->get_where('datas', array('dates' => date('Y-m-d')));
I need to get data from whole day like get data only from 2011-12-18.
Thanks
Try This:
$date="2018-09-19";
$this->db->select('*');
$this->db->from('tbl_name');
$this->db->where('DATE(date_time_col)', $date);
$query = $this->db->get();
$res= $query->result();
try with $this->db->like();
so it will be
$query=$this->db->like('dates', array('dates' => date('Y-m-d')));
the % will be added automatically
also you have datas as key, and should be dates based on database structure
You can do the following:
In your table, create an int field with the name ts.
When you insert data into this table, use the time() method to insert a timestamp into ts.
$data = array(
'dates' => '<your date value>' ,
'value' => '<your value>' ,
'ts' => time()
);
$this->db->insert('datas', $data);
Then, when querying:
Use the mktime() method to create a timestamp range for the start & end of today.
$start = mktime() - date('h')*3600;
$end = mktime();
$this->db->where('ts >=', $start);
$this->db->where('ts <=', $end);
Make sure you've indexed the ts column. This will also be faster than the above LIKE method on larger datasets.
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'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 have created an events calendar with codeigniters calendar class which you can view here: Events Calendar
I have it set up where the events are showing up on the calendar and when you click "view events" on a particular day, all the events with that start date pull up and are shown in a modal window.
Well... the problem is that unless its the START DATE of a particular event, the modal window details don't pull up. I know this is because i'm saying in my query to pull events where the start date equals a certain date...
I'm kind of stumped on how to modify this to say, "pull all records where this day is ANYWHERE BETWEEN the start and end date of the event.
Do I need to run a while loop or something and loop through each day of the month? Any ideas on an easier way to do this are appreciated.
the start and end dates are set up as 'Y-m-d H:i:s' in the database and the $query_date variable being passed in is 'Y-m-d', which i change to the same format in the first few lines of the function.
function get_list_events($query_date) {
$start_date_start = date('Y-m-d H:i:s', strtotime($query_date.' 00:00:00'));
$start_date_end = date('Y-m-d H:i:s', strtotime($query_date.' 23:59:59'));
$this->db->where('active', 1);
$this->db->where("start_date BETWEEN '$start_date_start%' AND '$start_date_end%'", NULL, FALSE);
$query = $this->db->get('events');
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'id' => $row->id,
'title' => $row->title,
'description' => $row->description,
'cost' => $row->cost,
'image' => $row->image,
'start_date' => $row->start_date,
'end_date' => $row->end_date,
'venue' => $row->venue,
'venue_address' => $row->venue_address,
'venue_city' => $row->venue_city,
'venue_state' => $row->venue_state,
'venue_zipcode' => $row->venue_zipcode,
'contact_name' => $row->contact_name,
'contact_email' => $row->contact_email,
'contact_phone' => $row->contact_phone,
'contact_website' => $row->contact_website,
'create_date' => $row->create_date,
'active' => $row->active,
);
}
return $data;
}
I guess your start_date column has the DATETIME or the TIMESTAMP data type. If that isn't true, please update your question.
There's a common trap in date-range processing in all kinds of SQL, due to the fact that when you compare a pure DATE with a DATETIME, they hardly ever come out equal. That's because, for example, DATE('2011-07-1') means the same thing as 2011-07-01 00:00:00.
So you need
start_date >= '$start_date_start'
AND start_date < '$start_date_end' + INTERVAL 1 DAY
instead of what you have, which is
start_date BETWEEN '$start_date_start%' AND '$start_date_end%' /*wrong!*/
The second clause with the < ... + INTERVAL 1 DAY picks up all possible times on the last day of your interval.
Edit Now that you've disclosed that you have two DATETIME columns, called start_date and end_date, it sounds like you're looking for items which start on or before a specific date, and end on or after that same date. Try something like this:
WHERE DATE(start_date) <= DATE('$specific_date')
AND DATE(end_date) >= DATE('$specific_date')
The trick on queries like this is to spend the majority of your time thinking through and specifying the results you want. If you do this, the SQL is often perfectly obvious.
I need to convert this query from php to mongoDB query
$query = "select * from table where data_added like '%data%';
I have date stored in variable
$date = "2013-09-02";
and in my mongo Document the date sorted as :
$dateAdded = new MongoDate(strtotime('2013-09-02 12:21:55'));
I tried
$date = new MongoDate(strtotime("$date"));
$mongo->find(array('date_added'=>array('$lt'=>$date)));
and
$mongo->find(array('date_added'=>$date));
but without success .
so I need to query usin (Y-m-d) not (Y-m-d h:i:s)
so how to use LIKE query for data in mongo
Thanks
You need to do a range query. Create a timestamp, for example using strtotime(), to get the unix timestamp at the start of the day, and another one and the end of the day.
Depending on if you want these two ends inclusive or exclusive, you then use
// Both points/seconds inclusive
->find(array("date" => array('$gte' => $startOfDay, '$lte' => $endOfDay)));
// Both seconds exclusive
->find(array("date" => array('$gt' => $startOfDay, '$lt' => $endOfDay)));
See http://cookbook.mongodb.org/patterns/date_range/
I have a date field in a mysql table formatted like this: Y-m-d.
I want to export every post that has a date between $fromDate and all the way to $toDate
I am sure its easy but now i am totaly blocked from ideas.
I am using codeigniter if that helps.
Best Regards
Audun
Try building a query similar to this and passing it to mysql_query (assuming your date format in $fromDate and $toDate match the date format of your mysql column)
<?
$query="SELECT * FROM table WHERE date BETWEEN '$fromDate' AND '$toDate'";
?>
In CodeIgniter documentation about Active Record at http://www.codeigniter.com/userguide2/database/active_record.html, there is a sub-section with title "$this->db->where();".
You can use comparison signs (like !=, <, >, <= and >=) just after the name of the column on which condition(s) is checked. For instance :
$this->db->where('id <', $id);
$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
$this->db->where($array);