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'
Related
Below is the field that I want to be autofilled to 3 days in the future every time the user opens this form to create a new record. Right now it defaults to the current day. I've searched all over but I'm not entirely sure what I'm looking for. Thanks for your help.
<td><?php
echo $form->input(
'Quote.date_due',
array(
'type'=>'date',
'empty'=>false,
'between'=>'<br />',
'after'=>$html->link(
$html->image(
'calendar.png',
array('width'=>'24','height'=>'24','alt'=>'Select Date')
),
'#',
array(
'onclick'=>'displayCalendarSelectBox(document.forms[1].elements[10],document.forms[1].elements[8],document.forms[1].elements[9],false,false,this)'
),
null,
null,
5
)
)
);
?></td>
In PHP the strtotime() and date() functions are useful for this.
Example:
echo date('Y-m-d', strtotime('+3 days'));
Output:
2019-01-13
Just keep in mind that timezones can effect this, and that it is 3 days from the current time and not 3 days from today at midnight.
i'm trying to build a query that will select all of the records in my DB between now (current month) and the previous 3 months.
My query somewhat works, but i want to ignore the day of the month. At the moment, it's selecting the last # of months to the current DAY as well but i want to ignore the current day and use the start and end of the months.
Here's my query:
$dateS = Carbon::now()->subMonth(3);
$dateE = Carbon::now();
$TotalSpent = DB::table('orders')
->select('total_cost','placed_at')
->whereBetween('placed_at',[$dateS,$dateE])
->where(['deleted' => '0', 'delivery_address_id' => $DeliveryAddress->id])
->sum('total_cost');
Any help would be appreciated. It's really bugging me!
This will do the trick I guess
$dateS = Carbon::now()->startOfMonth()->subMonth(3);
$dateE = Carbon::now()->startOfMonth();
$TotalSpent = DB::table('orders')
->select('total_cost','placed_at')
->whereBetween('placed_at',[$dateS,$dateE])
->where(['deleted' => '0', 'delivery_address_id' => $DeliveryAddress->id])
->sum('total_cost');
startOfMonth() begins with 1st date of the month
Use Carbon:
$data = Data::where("created_at",">", Carbon::now()->subMonths(6))->get();
You need to import the namespace to use Carbon:
use Carbon\Carbon;
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.
So I'm trying to grab only data which are in last 24 hours, 1 week, 1 month. I have working code, but if I get it correctly it's "other way". By that I mean, If date is at the moment 30-Nov-2016 and I set the Data value to 1-Dec-2016, then the 24 Hour one still grabs the information, but it shouldn't. If date is 30-Nov-2016 and I set it to 28-Nov-2016, then the 24 Hour one doesn't grab it. For me It sounds like it's backwards. I hope the explanation is understandable.
$getItemsOneDay = Deposit::where('steam_user_id',0)->where('status', Deposit::STATUS_ACTIVE)->where('created_at', '>', Carbon::now()->subMinutes(1440))->get();
$getItemsOneWeek = Deposit::where('steam_user_id',0)->where('status', Deposit::STATUS_ACTIVE)->where('created_at', '>', Carbon::now()->subMinutes(10080))->get();
$getItemsOneMonth = Deposit::where('steam_user_id',0)->where('status', Deposit::STATUS_ACTIVE)->where('created_at', '>', Carbon::now()->subMinutes(43200))->get();
You wrote created_at > time.
So you ask laravel to search all data greater than now(). So you look into the future.
Either you want to...
look at points before a certain point in time: created_at < time
look at points after a certain point in time: created_at > time
look at points within an interval of passed time: created_at > start && created_at < end
If i got you right you are searching for the third option. So you need to do something like ...->where("created_at",">",Carbon::now()->subDay())->where("created_at","<",Carbon::now())->...
Hopefully, I got you right here.
->where('created_at', '>=', Carbon::now()->subDay()->toDateTimeString())->get();
to get the data created within last 24 hours
Check the below function; it is working fine.
public function getanswer_24hours(Request $request)
{
$ans = Answer::where('user_id',$request->user_id)->where('created_at', '>=', Carbon::now()->subDay()->toDateTimeString())->get();
if(count($ans) > 0)
{
$result = 'yes';
return response()->json(['data' =>['answer' => $ans,'result' => $result, 'message' => 'get Answer Before 24hours SuccessFully!!','status' => true],200,'ok']);
}else{
$result = 'no';
return response()->json(['data' =>['answer' => [], 'result' => $result, 'message' => 'get Answer Failed!!'],404,'no']);
}
}
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.