i have a problem with query SQL.
I need do a query that it result my order to last week ( 7days to now) I use this code
$this->db->where("account_orders.data > now()-7",NULL);
date in my db is -> 2019-04-05
Why don't it work ?
I dont know why are using this now()-7, you can use range here with where clause if you want orders between 7 days to now, as:
$dateLastWeek = date('Y-m-d', strtotime('-7 days')); // last week day
$dateCurrent = date('Y-m-d'); // current day
In CI, you can use range as like:
$this->db->where("account_orders.data >=", $dateLastWeek);
$this->db->where("account_orders.data <=", $dateCurrent);
If you just need last week orders then, you can use just $dateLastWeek date in your query as:
$this->db->where("account_orders.data", $dateLastWeek);
Note that, i am using Y-m-d date format due your input which you mentioned in your question 2019-04-05
Related
I have a date as 2010-12-24.
I need to store the date as 2010-12-01 in database table. This should be applicable for all dates.
When inserting another date 2010-12-25, it should also be stored as 2010-12-01, and for 2017-01-29, it be 2017-01-01.
How can I set the day to default 1?
I hope this is clear. Any help is appreciated. Thanks.
A much simpler way is to use PHP's built-in date functions :
// here's your date
$date='2017-01-12';
// and here we transform it as we want
$date_first = date('Y-m-01', strtotime($date));
// check out the result :)
echo $date_first;
it can be done in mysql just fine, though.
set #date = '2017-12-25';
select str_to_date(CONCAT(YEAR(#date),' ',MONTH(#date),' 1'), '%Y %m %d');
You don't need to use the #date variable, just replace #date with the date you are need to insert and use str_to_date to replace the day with 1.
You can achieve it directly in MySql as follows:
SELECT DATE_SUB('2010-12-25', INTERVAL DAY('2010-12-25')-1 DAY)
Explanation:
1. DAY('2010-12-25')-1 DAY // will return 24
2. DATE_SUB('2010-12-25', INTERVAL 24) // will return 25-24 = 01 i.e. 2010-12-01
This will work in all cases.
This should work, adjust to your requirement
$today = date("Y-m", strtotime("2017-12-25"));
$dbDate = date ("Y-m-d", strtotime($today."-1"));
echo $dbDate;
Pass $dbDate to the query.
<?php
$date='2017-01-12';
$date=explode('-',$date);
$date[2]=str_replace($date[2],'01',$date[2]);
$date=implode('-',$date);
print_r($date);
And the output is :
2017-01-01
So it will always replace the day with 01.
After you can execute the insert query for the new date as it is already modified in the php.
I am working on a booking system and need to select only the records where a certain field is for today. Not today and in the future, but only today.
The query currently is:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->where('requested_booking_time', ?????);
$advancedBookings->get();
The requested_booking_time is the field that I wish to be checked, the date stored is in this format:
2017-08-23 08:00:00
So I want to only select rows that are on the same day as the current day.
As i understand, you want that records which is created today; then just get the today's date.
$today = date("Y-m-d");
now your query is like this.
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->where('requested_booking_time', $today)->get();
You can use whereDate and format your dates with Carbon:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->whereDate('requested_booking_time', '=' , Carbon::today()->toDateString());
$advancedBookings->get();
Or format the your date before the query with Carbon.
You should try this:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->whereDate('requested_booking_time', '=', date('Y-m-d'))
->get();
I'm trying to hide an event two days after it's passed in php (using a mysql query). There are two date options. Start date, and End date. I can't seem to figure out how to make the query work.
$query->select('*');
$query->from('#__events_items');
$query->where('date2 >= "'.$today.'"');
$query->where('date2 <= "'.$sixmths.'"');
$query->where('state = 1');
$query->order('date1 asc');
I've tried
$today = #date('Y-m-d');
$enddate = #date('Y-m-d',(strtotime(#date('Y-m-d')."+ 2 days")));
But obviously the end date won't be greater or equal to $enddate.. Any help would be greatly appreciated!
You can either modify today like so:
$today = date('Y-m-d', strtotime('-2 days'));
Or modify your query like so :
$query->select('*');
$query->from('#__events_items');
$query->where('date2 >= "'.$today.'" - INTERVAL 2 DAY');
$query->where('date2 <= "'.$sixmths.'"');
$query->where('state = 1');
$query->order('date1 asc');
I would say the second is probably better form since having $today represent two days ago would be nasty self documenting code.
I'm trying to do a system that compare an specific date ($date) with the atual date (date('Y/m/d')), but I need to color this $date when he is one week to the atual date (near the maturity date). I don't know if you can understand me, my english is bad...
Thank you for reading this.
Do it in the MySQL query:
SELECT maturity_date,
maturity_date < DATE_ADD(NOW(), INTERVAL 1 WEEK) AS near_maturity,
...
Then your PHP code can use if ($row['near_maturity') to color the date.
Something like this may work:
date_default_timezone_set('America/Los_Angeles');
$date = DateTime::createFromFormat('m-d-Y', '04-15-2013');
$maturityDate = DateTime::createFromFormat('m-d-Y', '04-20-2013');
$maturityDateMinus10Days = DateTime::createFromFormat('m-d-Y', '04-10-2013');
if ($date > $maturityDateMinus10Days
&& $date < $maturityDate) {
echo 'date is within 10 days of maturity 10';
}
You can test it by copying and pasting here http://writecodeonline.com/php/
Ref. PHP - add 1 day to date format mm-dd-yyyy
Ref. How can I check if the current date/time is past a set date/time?
I was looking at this post, and it is close to what I need:
PHP - How to count 60 days from the add date
However, in that post, the calculation is performed by adding 60 days to the current date. What I need to do is calculate the date based on a variable date (and not the current date).
Something like this:
$my_date = $some_row_from_a_database;
$date_plus_10_days = ???;
Anyone know how to do that?
Thanks
You can put something before the "+10 days" part:
strtotime("2010-01-01 +10 days");
Use date_add
http://www.php.net/manual/en/datetime.add.php
$my_date = new DateTime($some_row_from_a_database);
$date_plus_10_days = date_add($my_date, new DateInterval('P10D'));
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$dateVariable = strtotime('2017-01-29');//your date variable goes here
$date_plus_60_days = date('Y-m-d', strtotime('+ 60 days', $dateVariable));
echo $date_plus_60_days;
If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$date_plus_60_days = new DateTime("2006-12-12");
$date_plus_60_days->modify("+60 days");
echo $date_plus_60_days->format("Y-m-d");
I see you are retriving data from a database.
If you are using mysql you can do it on the select:
Example: you need the last date of the table and this date-7 days
select max(datefield) as ultimaf, DATE_SUB(max(datefield),INTERVAL 7 DAY) as last7
from table
It´s easy use curdate() if you want todays date.
If you need a dynamic between that selects the count of last 7 days:
select count(*) from table
where DATE_SUB(CURDATE(),INTERVAL 7 DAY)<=datefield"
date('Y-m-d H:i:s', strtotime("2014-11-24 06:33:39" +35 days"))
this will get the calculated date in defined format.
Suppose today's date is
date_default_timezone_set('Asia/Calcutta');
$today=date("Y-m-d");
And i can add 10 days in current date as follows :
$date_afte_10_days = date('Y-m-d', strtotime("$today +10 days"));