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.
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 running a sweepstakes page on my wordpress site. I already have the logic setup when create a sweepstakes post that the sweepstakes page already pulls in post based on custom fields for "start_date" and "end_date". The issue I am having is if a sweepstakes is set to start on 03/01/2014 and end on 03/19/2014 once the time hits midnight (12:00) the post is removed when it needs to continue throughout the 19th. Stated below is the query code I am using. And it works for posts with custom fields of start date and end date. I just almost need a default for the post to be removed in this case 03/19/2014 at time 23:59:59. I need a default time for 23:59:59. Is there a global change that will make post remove on the date specified just at 23:59:59? I hope this explains it.
<?php // The Query
$startdate = date('Y-m-d G:i:s');
$enddate = date('Y-m-d G:i:s');
$args = array(
'post_type' => 'page',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'sweepstakes_startdate',
'value' => $startdate,
'compare' => '<='
),
array(
'key' => 'sweepstakes_enddate',
'value' => $enddate,
'compare' => '>='
)
)
);
$the_query = new WP_Query( $args );
No, there's no real shorthand for specifying that, aside from specifying the literal '23:59:59'.
Seems like the quickest patch is just some PHP logic to reset $enddate before it's sent to your query.
If the time component of $enddate is midnight, then add one day and subtract one second to the value sent to the query.
This does change the specification a bit; it means that specifying an enddate value of '2014-03-19 00:00:00' is equivalent to specifying and enddate value of '2014-03-19 23:59:59', which basically means that it's not possible to specify a sweepstakes ending exactly midnight. But you could end a sweepstakes at time 00:00:01.
In MySQL, we'd typically specify the "end" of a period (like you describe) as midnight of the following day (rather than '23:59:59' of the preceding day), and use a "less than" comparison rather than a "less than or equal to" comparison.
For example:
WHERE mydatetimecol >= '2014-03-19'
AND mydatetimecol < '2014-03-20'
rather than
WHERE mydatetimecol >= '2014-03-19'
AND mydatetimecol <= '2014-03-19 23:59:59'
MySQL does have some handy INTERVAL operations on DATETIME values...
WHERE mydatetimecol >= '2014-03-19'
AND mydatetimecol < '2014-03-19' + INTERVAL 1 DAY
If I absolutely had to "back up" one second, I'd use an interval operation, for example:
WHERE mydatetimecol >= '2014-03-19'
AND mydatetimecol <= '2014-03-19' + INTERVAL 1 DAY + INTERVAL -1 SECOND
Note that some temporal values can actually have milliseconds precision, such as '23:59:59.997' where you could potentially leave a gap between the end of one period and the beginning of another, if you used '23:59:59'.
This isn't really a problem with temporal datatypes stored in MySQL (but other RDMBS such as SQL Server can store fractional seconds). And it's probably not a problem for your particular application (but in the more general case, we typically want for rows to fall into a particular bucket, and not fall between the cracks between the buckets.)
And, I'd be leery of adding 86,399 seconds to a date value, depending on whether the timezone is daylight savings time or not, there's some days that are 23 hours or 25 hours.
$start_scheduled_date = date("Y-m-d H:i:s", mktime(0,0,0));
$end_scheduled_date = date("Y-m-d H:i:s", mktime(23,59,59));
output:
2015-12-30 15:15:00
2015-12-31 00:00:00
2015-12-31 23:59:59
I want to insert a date into the clients table my db schema is below, I want to insert them into the start_day and end_day fields.
I have the below in validations in ClientController.php
If I insert a foreign date_format other than the one defined below I am thrown an error, but if I do insert the correct one it reverts to 0000-00-00. But if I change the field type to text for example the date_format is inserted fine.
$rules = array(
'project_name' => 'required',
'project_brief' => 'required',
'start_day' => array('required', 'date_format:"m-d-Y"'),
'end_day' => array('required', 'date_format:"m-d-Y"')
);
I'm not sure where the problem lies to be honest. I've even tried to convert the time doing the below:
$start_day_old = Input::get('start_day');
$start_day = date("d-m-Y", strtotime($start_day_old));
$project = new Project;
$project->start_day = $start_day
$project->save();
However the results were the same. Does anyone know how I can rectify this issue?
You can't insert a date formated as dd-mm-yyyy in mysql's date field, it should be yyyy-mm-dd, so in your code here
$start_day = date("d-m-Y", strtotime($start_day_old));
Change it to
$start_day = date("Y-m-d", strtotime($start_day_old));
So, if a date is 15-10-2010 then it'll become 2010-10-15 and it's a valid date for date field in the mysql database.
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 spent a good amount of time last night scouring the internet trying to find a solution to finding all of the records created from the last hour in the MySQL database. The one answer I found which is posted everywhere doesn't even work..
This is what I've come up with based on what I found online and thought might work:
$recentUploads = $this->Upload->find('all', array(
'conditions' => array('Upload.created LIKE' => date('Y-m-d H:i:s', strtotime('-1 hour')))
));
But still no luck at all. Any thoughts?
You want records where the timestamp is greater than or equal to the time 1 hour ago:
$recentUploads = $this->Upload->find(
'all',
array(
'conditions' => array(
'Upload.created >=' => date('Y-m-d H:i:s', strtotime('-1 hour'))
)
)
);
you need this function for any time you want to use :
function SinceDate($timebyminuites)
{
$to_time = strtotime(date('Y-m-d H:i:s'));
$from_time = $timebyminuites*60;
return date('Y-m-d H:i:s',round(abs($to_time - $from_time),2));
}
when you want get the datetime from one hours ago
you can use the function like this :
$onehoursago = SinceDate(60);
and the query will be like this :
select * from Upload where Upload.created >= '$onehoursago'