Codeigniter Model fetch rows from -7 days - php

How do I fetch all of the rows that have a duedate -1 days from current day?
public function getNumTasksDueTomorrow()
{
$this->db->select('*');
$this->db->from('tasks');
$this->db->where("user_id",$this->session->userdata('user_id'));
$this->db->order_by("tasks_id", "desc");
$this->db->where("tasks_duedate", ' -1 day from current day');
$query_result=$this->db->get();
$result=$query_result->row();
return $result;
}

First get a mysql date for yesterday something like:
$date = date('Y-m-d H:i:s', strtotime('yesterday'));
Then change your date query to:
$this->db->where("tasks_duedate <", $date);
This will give you all tasks with a due date older than yesterdays date. However I think you mean any date in the past, so just change $date to now.
$date = date('Y-m-d H:i:s');
And of course I have assumed your date record is a mysql timestamp, you may have used a date, just remove the time formats.
Hope that helps,
Paul.

Related

How to change date format to strtotime in codeigniter

I want to convert date format to strtotime in codeigniter to cross check the expire date from the database.
My code look like this below
$expiredate = date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s",strtotime($tra->t_started)). '+30days'));
if(strtotime(date("Y-m-d H:i:s")) > strtotime($expiredate)){
$ra->is_paid="1";
//package is expired for 30days.
} else{
$ra->is_paid="0";
//package is expired for 30days.
}
I need help to solve this problems. Thanks
Several issues here:
First, you don't need to convert dates with strtotime to compare them.
Second, to add a certain amount of days to a date, you use +30 day (singular) not +30 days
You could simply:
$now = date('Y-m-d H:i:s');
$expire_date = date('Y-m-d H:i:s', strtotime($tra->t_started."+30 day"));
if ($now >= $expire_date)
{
// 30 or more days overdue
}
else
{
// Less than 30 days overdue
}

PHP Comparing time and listing data within

I have the following variables...
$pickupDate = $row['pickupDate'];
$pickupTime = $row['pickupTime'];
$dateRetrieve = date("y/m/d");
$currentDate = "20".$dateRetrieve;
$currentTime = date("h:i:sa")
I have an SQL table providing the Pickup Date and Time. I would like to compare the current date/time with the ones in the table, and list all the entities within 2 hours of the current date/time.
Thanks for your help!
You Can Use As Following
$currenttime = date('Y-m-d H:i:s');
//DISPLAY CURRENT TIME
$lasttwohour = date('Y-m-d H:i:s', strtotime('-2 hour'));
//TWO HOUR AGO TIME
$query = "select * from tbl_nm where '$pickupTime' between '$currenttime' AND '$lasttwohour' order by id ";
Or if you dont have datetime field you can remove ('Y-m-d') from $currenttime and $lasttwohour.
I Hope it might helpful.

One year back query datetime

I want to get all records from now between a year ago but it's not working exactly.
Made in CodeIgniter
This is my query , antything wrong with it ? :
$currentDate = date("Y-m-d H:m:s");
$yeardate=date('Y-m-d H:m:s', strtotime('-1 year'));
$this->db->select('TimeStamp');
$this->db->where('Transaction', 'COMPLETED');
$this->db->where('TimeStamp>=',$yeardate);
$this->db->where('TimeStamp<=',$currentDate);
$query = $this->db->get('R_Logs');
$results = $query->result();
Thanks in advance
I forgot to add a space between TimeStamp <= and TimeStamp >= .
My bad.
I think it might be easier to try Epoch time and then query off of that - e.g. Get epoch for a specific date using Javascript
That will convert the date to number of days since January 1, 1970. then from that you can subtract 365 days ( or use the yearInteger %4 trick to find out if it is a leap year)
Change these two lines any try once:
$currentDate = date("Y-m-d H:i:s");
$yeardate = strtotime(date('Y-m-d H:i:s') . ' -1 year');
Dump your query and debug:
echo $this->db->last_query();

Add days to current date from MySQL with PHP

I have a fixed date from MySql
startDate = 07/03/2011
I wanted to add 60 days on top this date to have an endDate.
$startDate = $result['startDate'];
$endDate = ??? + strtotime("+60 days");
echo $endDate;
From my research, I know it has something do with strtotime, but all the sites I come across with based the start date from current workstation's time. My date is already fixed and entered prior to running and getting the endDate.
Help? Thanks in advance!
In addition to PHP solutions others are providing, you can create the endDate right inside of MySQL and save yourself some of the trouble:
SELECT startDate, DATE_ADD(startDate, INTERVAL 60 DAY) AS endDate FROM table;
-- Or by months (not exactly the same thing)
SELECT startDate, DATE_ADD(startDate, INTERVAL 2 MONTH) AS endDate FROM table;
Relevant documentation here...
You could reformat the results of strtotime()
$startDate = $result['startDate']; // 07/03/2011
$endDate = date("m/d/Y", strtotime("$startDate +60 days"));
Demo: http://codepad.org/9rWnoeQb
$startDate = "07/03/2011";
$endDate = strtotime("+60 days",time($startDate));
$formatted = date('m/d/Y',$endDate);
echo $endDate . "<br/>" . $formatted;
86400 seconds in a day, times number of days.. and add it to current time.
$nextMonth = time()+86400*60;
echo date("Y-m-d H:i:s", $nextMonth);

Trying to find the previous six days given any date

I'm using strtotime just fine for finding the previous week and next week's entries in my database, but what I can't seem to find is how to find the previous six days if the user selects a past date.
Here's how I know what today and six days previous are:
$today = date("Y-m-d");
$minus6 = date('Y-m-d', strtotime('-6 days'));
Now how can I switch $today with $dateString as provided by my users' input?
I thought something like this based on my google searches, but it yields no results:
$dateString = 2010-01-25; // for example
$minus6 = date('Y-m-d', strtotime('-6 days, $dateString');
Am I missing some fundamental information regarding dates, strtotime, or what? Thanks for any help.
The second param to strtotime is a timestamp from which the first argument will be calculated:
echo date('Y-m-d', strtotime('-6 days', strtotime($dateString));
But you can also do it like Gavin suggested.
You should put the actual date before any of the modifiers to strtotime(). For example:
$dateString = 2010-01-25; // for example
$minus6 = date('Y-m-d', strtotime('-6 days, $dateString'));
Should become:-
$dateString = "2010-01-25"; // for example
$minus6 = date('Y-m-d', strtotime("$dateString -6 days"));
...or pass it in as an explicit timestamp as a second parameter to strtotime() as per Gordon's answer.

Categories