I was just wondering how you can echo a range of dates in PHP
ideally i want the system to display this week wednesday to next week wednesday
for example
17/11/10 to 24/11/10
can anyone point me in the right direction?
$oBeginDate = new DateTime('last wednesday');
$oEndDate = new DateTime('next wednesday');
echo $oBeginDate->format('d/m/Y') . ' to ' . $oEndDate->format('d/m/Y');
i see no mention about mysql in your question, but if you're really talking about it:
select * from my_table where date_fld between '17/11/10' and '24/11/10'
create a date, and add days to it...
$m= date("m");
$de= date("d");
$y= date("Y");
for($i=0; $i<8; $i++){
echo date('d-m-y:D',mktime(0,0,0,$m,($de+$i),$y));
echo "<br>";
}
use strtotime
$now = '17/11/2010';
$next_week = date('d/m/Y', strtotime('+1 week', strtotime($now)));
Since MySQL is tagged...
SELECT now(),
case
when weekday(now()) = 3 then date_sub(now(), interval 1 day)
when weekday(now()) = 2 then curdate()
when weekday(now()) = 1 then date_add(now(), interval 1 day)
when weekday(now()) = 0 then date_add(now(), interval 2 day)
when weekday(now()) = 4 then date_sub(now(), interval 1 day)
when weekday(now()) = 5 then date_sub(now(), interval 2 day)
when weekday(now()) = 6 then date_sub(now(), interval 3 day)
end as current_wednesday,
case
when weekday(now()) = 3 then date_add(now(), interval 6 day)
when weekday(now()) = 2 then date_add(now(), interval 7 day)
when weekday(now()) = 1 then date_add(now(), interval 8 day)
when weekday(now()) = 0 then date_add(now(), interval 9 day)
when weekday(now()) = 4 then date_add(now(), interval 5 day)
when weekday(now()) = 5 then date_add(now(), interval 4 day)
when weekday(now()) = 6 then date_add(now(), interval 3 day)
end as next_wednesday
Related
I have a Table in Database which has records of Logins.
Table name: user_logins
ID | timestamp
1 2019.01.03 (Year, Month, Day)
2 2019.01.04
3 2019.01.05
4 2019.01.05
5 2019.01.07
6 2019.01.07
7 2019.01.09
I want to Show only Count of Records by this Week.
From Monday to Sunday (04-02-2019 ... 10-02-2019)
My PHP and SQL Code is:
$mo = mysql_num_rows(mysql_query('SELECT * FROM user_logins WHERE DAYNAME(DATE(timestamp)) = "monday" and timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFWEEK(CURDATE())-0 DAY)'));
this should show the records of 04-02-2019
Here is my SQL Fiddle link:
SQL Fiddle
This:
DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
gives this week's Monday.
So:
SELECT * FROM user_logins
WHERE
timestamp
BETWEEN DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
and
NOW()
Try following query:
SELECT id FROM `user_logins`
WHERE timestamp >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND timestamp < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
Demo
In my DB I have a column called dato_tid (Datatype = date)
right now I have 2 post
1 where the date is 2018-07-18
2 where the date is 2018-07-20
I need to select the post, that has less then 24 hours to go
SELECT * FROM `udflyt` WHERE dato_tid > DATE_ADD(CURDATE(), INTERVAL -1 day)
this line will select both posts
SELECT * FROM `udflyt` WHERE dato_tid > DATE_ADD(CURDATE(), INTERVAL -24 HOUR)
and so will this, I did try to change the > to < but the same.
SELECT * FROM `udflyt` WHERE dato_tid > (now() - interval 1 day )
this line will also get both posts
So what do I need to do, Thanks
Actually, 24 hours to go means you should ADD a day, not subtract a day.
SELECT *
FROM `udflyt`
WHERE dato_tid <= DATE_ADD(CURDATE(), INTERVAL +1 day);
Will provide only the record with date '2018-07-18' (which is what you are looking for, I believe.
The below shows the values used for comparison for both doing addition and subtraction.
SELECT *, DATE_ADD(CURDATE(), INTERVAL +1 day), DATE_ADD(CURDATE(), INTERVAL -1 day)
FROM `udflyt`
WHERE dato_tid <= DATE_ADD(CURDATE(), INTERVAL +1 day);
I have to check expiry date of vehicle, I have taken interval of 1 day but its taking positive value so its not showing as EXPIRED rather its taking the positive value and showing as EXPIRES TOMORROW.
SELECT
vehicle_reg_num,
vehicle_type,
insurance_validity,
tax_validity,
fc_validity
FROM vms_vehicles_list
WHERE ((insurance_validity BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY)
OR insurance_validity BETWEEN DATE_ADD (NOW(),INTERVAL -7 DAY) AND NOW())
OR (tax_validity BETWEEN NOW() AND DATE_ADD(NOW(),INTERVAL 7 DAY)
OR tax_validity BETWEEN DATE_ADD(NOW(),INTERVAL -7 DAY) AND NOW())
OR (fc_validity BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY))
OR fc_validity BETWEEN DATE_ADD(NOW(),INTERVAL -7 DAY) AND NOW())
AND vehicle_delete_flag=0;
Your query should be like this.
SELECT
vehicle_reg_num,
vehicle_type,
insurance_validity,
tax_validity,
fc_validity
FROM vms_vehicles_list
WHERE ((insurance_validity BETWEEN NOW() AND SUBDATE(NOW(),7)
OR insurance_validity BETWEEN SUBDATE(NOW(),7) AND NOW())
OR (tax_validity BETWEEN NOW() AND SUBDATE(NOW(),7)
OR tax_validity BETWEEN SUBDATE(NOW(),7) AND NOW())
OR (fc_validity BETWEEN NOW() AND SUBDATE(NOW(),7)))
OR fc_validity BETWEEN SUBDATE(NOW(),7) AND NOW()) AND vehicle_delete_flag=0;
I am calculating weekly values comparing the last week and the same days in the year before. As we just have a leap year, my approach causes problems.
I am calculating the dates like this and perform a corresponding select:
$today_raw = date('Y-m-d');
$yearAgo = date('Y-m-d', strtotime('-1 year', strtotime($today_raw)));
$weekAgo = date('Y-m-d', strtotime('-6 day', strtotime($today_raw)));
$weekYearAgo = date('Y-m-d', strtotime('-1 year', strtotime($weekAgo)));
$stmt_currentWeek = $pdo->prepare("SELECT X FROM Y WHERE Z BETWEEN '$weekAgo' AND '$today'");
$stmt_weekLastYear = $pdo->prepare("SELECT X FROM Y WHERE Z BETWEEN '$weekYearAgo' AND '$yearAgo'");
It's obvious that the SELECT returns the wrong number of values as $weekYearAgo is simply wrong as it does not reflect the leap year differences.
What am I doing wrong?
Just use mysql DATE Functions:
$stmt_currentWeek = $pdo->prepare("SELECT X FROM Y WHERE Z >= DATE_SUB(NOW(), INTERVAL 1 WEEK)");
$stmt_weekLastYear = $pdo->prepare("SELECT X FROM Y WHERE Z BETWEEN DATE_SUB(DATE_SUB(NOW(), INTERVAL 1 YEAR), INTERVAL 1 WEEK) AND DATE_SUB(NOW(), INTERVAL 1 YEAR)");
1 Year And 1 Week AGO:
DATE_SUB(DATE_SUB(NOW(), INTERVAL 1 YEAR), INTERVAL 1 WEEK)
1 Year AGO:
DATE_SUB(NOW(), INTERVAL 1 YEAR)
1 Week AGO:
DATE_SUB(NOW(), INTERVAL 1 WEEK)
In the first SQL you dont need Between 1 WEEK AGO and NOW you can just rewrite it to >= 1 WEEK AGO
I want to get all the records from mysql database who have updated their records within 30 days from the current date for that i have used the below query but it is not working properly. $tda is the current date and $prevmonth is the date of exactly
30 days back from the current date. Please help. Thanks.
$da=date('d');
$tda=date('d-m-Y');
$prevmonth = date(''.$da.'-m-Y', strtotime('-1 months'));
$sql_q=executeQuery("select * from ".reg." where 'uid' !=".$_SESSION['uid']." AND Updatedate >= '$prevmonth' AND Updatedate <='$tda '");
You can do it in mysql as
`Updatedate` < DATE(NOW() - INTERVAL 30 DAY)
OR
`Updatedate` < DATE_SUB(CURDATE(), INTERVAL 30 DAY)
For Varchar
STR_TO_DATE(Updatedate, '%Y-%m-%d') < DATE_SUB(CURDATE(), INTERVAL 30 DAY)
UPDATE :
The query posted in the comment is wrong and should be
$sql_q=executeQuery("select * from registration
where
`uid` != ".$_SESSION['uid']."
AND STR_TO_DATE(Update_date, '%d-%m-%Y') < DATE_SUB(CURDATE(), INTERVAL 30 DAY)") ;
If you are looking for data within last 30 days then
$sql_q=executeQuery("select * from registration
where
`uid` != ".$_SESSION['uid']."
AND STR_TO_DATE(Update_date, '%d-%m-%Y') >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)") ;
I like doing something like this: AND UpdateDate > NOW() - INTERVAL 30 DAY AND UpdateDate < NOW().
If your Updatedate column is a DATETIME column then you can do the following:
SELECT *
FROM table
WHERE uid <> ?
AND Updatedate >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
AND Updatedate <= NOW();
Or:
SELECT *
FROM table
WHERE uid <> ?
AND Updatedate BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW();
Or if it's a timestamp then this:
SELECT *
FROM table
WHERE uid <> ?
AND FROM_UNIXTIME(Updatedate) >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
AND FROM_UNIXTIME(Updatedate) <= NOW();