How to add leading zeros - php

I would like to know how can I get the leading zero's here. I already tried %H AND %h but still not working.Any help would be appreciable.Thank you
SELECT * FROM events WHERE eventDate = DATE_FORMAT(CURDATE(), '%d-%m-%Y') AND time BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 2 HOUR),'%H:%i') AND DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 10 MINUTE),'%H:%i')
UPDATE:
It just doesn't work between 00:00 and 02:00.. Do someone has any ideia why it's happening??

As you said your problem only exists when time between 00:00 and 02:00, it caused by this code:
DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 2 HOUR),'%H:%i')
Let's break your code into few parts:
select date_format(curdate(), '%d-%m-%Y') as date_only,
DATE_FORMAT(DATE_SUB(now(), INTERVAL 2 HOUR),'%H:%i') as min2h,
DATE_FORMAT(DATE_ADD(now(), INTERVAL 10 MINUTE),'%H:%i') as add10m
# assume now = 2016/10/28 01:00
# the date_only will return 2016/10/28
# the min2h will return 23:00
# the add10m will return 01:10
back to your query, you will have a query like this:
SELECT * FROM events WHERE eventDate = '20161028' AND time BETWEEN '23:00' AND '01:10'
That's why you can't get the result which the statement is not correct. You should convert your eventDate and time to datetime first, so that you can compare the datetime correctly when minus 2 hours is yesterday or add 10 minutes is tomorrow
select * from events
where str_to_date(concat(`eventDate`, " ", `time`), '%d-%m-%Y %H:%i') # convert o datetime, depends on your format
between date_sub(#report_date, interval 2 hour)
and date_add(#report_date, interval 10 minute)
Fiddle

Related

mysql query get 1st specific day between 2 dates

How do I get the first Sunday between 2 dates?
i have only 2 fields in myTable (dt_from and dt_to)
dt_from = '2016-08-6';
dt_to = '2016-08-19';
SELECT firstsunday FROM myTable BETWEEN dt_from AND dt_to;
How about getting the first Sunday just after from date itself?
SELECT DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) from myTable as A;
http://sqlfiddle.com/#!9/7fce5
If Sunday isn't between the dates:
SELECT DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) as firstsunday from myTable as A where DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) between A.date_from and A.date_to;
http://sqlfiddle.com/#!9/83d0f0/4
A little bit shorter:
SELECT firstsunday from (SELECT A.date_from, A.date_to, DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) as firstsunday from myTable as A) as B where B.firstsunday between B.date_from and B.date_to;
http://sqlfiddle.com/#!9/6adab3/1
Try this:
SELECT dt, DAYNAME(dt) as day
FROM myTable WHERE dt BETWEEN date1 AND date2
ORDER BY dt
WHERE day = "Sunday"
LIMIT 0,1
assume you only need to sue the db to get the date range so you dont want any king of query.
<?php
echo date('m/d/Y', strtotime('next Sunday', strtotime('2016-08-6')));
you can add some validation to make sure its before your end date

SQL query to show rows that are 60+ in the past

I have a row in my database with the datetime as
2013-11-07 13:04:57
then i am running this Query:
SELECT * from customer_communication
WHERE customer_seq = '276'
and datetime < DATE_SUB(DATE(now()), INTERVAL 60 DAY)
order by datetime DESC
so it should not be returning this row
i want to show all rows with a datetime that are 60+ days in the past
You can use DATEDIFF function
For example,
DATEDIFF(date1, date2) < 60
The date 2013-11-07 13:04:57 is more than 60 days in the past:
SELECT DATE_SUB(DATE(NOW()), INTERVAL 60 DAY);
=> 2013-11-10
Any date on or before November 10th will be displayed (from January 9th)
# Show difference in days.
SELECT DATEDIFF(DATE(NOW()),'2013-11-07');
=> 63
Additionally, you should be using the <= comparator if you wish to show rows that have a date set to exactly 60 days ago from the current day.
The problem is that your current query will select rows that are older than one datetime value. You need to change comparison sign to:
datetime > DATE_SUB(DATE(now()), INTERVAL 60 DAY)
And date 2013-11-07 13:04:57 more than 60 days in the past.

Filter a range of dates with SQL

I have to filter the results of an SQL starting at the current day and displaying all records until the current day + 10 days. So if it's the 22th of December I should display all records starting that day and that aren't after the the 1st of January. How can I do this? I tried a simple query like the one below but it seems to only display the records until the last day of the current month and then instead of showing the records of the next year goes back to the first month of the current year.
SELECT *
FROM mytable
WHERE DAY(mydatefield) >= DAY(CURRENT_TIMESTAMP)
AND mydatefield <= DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 10 DAY)
EDIT 1
I'm using Symfony 2 + Doctrine 2 querybuilder so the query must be compatible with them
EDIT 2
I solved the Doctrine 2 problem by using the query suggested by eggyal with this PHP code
$queryBuilder->where($queryBuilder->expr()->gte('mydatefield', 'CURRENT_DATE()'))->andWhere($queryBuilder->expr()->lte('mydatefield', 'DATE_ADD(CURRENT_DATE(), 10, \'DAY\')'));
The problem lies in your erroneous use of MySQL's DAY() function, which returns the day of the month. You should use instead DATE(); you can also simplify with the BETWEEN ... AND ... comparison operator:
SELECT *
FROM mytable
WHERE DATE(mydatefield) BETWEEN CURDATE() AND CURDATE() + INTERVAL 10 DAY
Note that, in order to benefit from index optimisation, you could instead do:
SELECT *
FROM mytable
WHERE mydatefield >= CURDATE() AND mydatefield < CURDATE() + INTERVAL 11 DAY
You should be able to use this:
SELECT *
FROM mytable
WHERE date(mydatefield) >= date(CURRENT_TIMESTAMP)
AND date(mydatefield) <= date(DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 10 DAY))
See SQL Fiddle with Demo
try this
SELECT *
FROM mytable
WHERE DATE( mydatefield ) BETWEEN CURRENT_DATE AND CURRENT_DATE + INTERVAL 10 DAY

Mysql Query back to midnight only

I have this query that looks up results from a database for the last twenty minutes, now i know how to look up in hours, days, etc, but is it possible to look up only as far back as midnight of that day. so when ever the query is run and what ever time it only looks back as far as midnight?
SELECT * FROM ip_stats WHERE date >= ( NOW() - INTERVAL 20 MINUTE ) and ip='$ip'
This is my code but is there away in which i can replace the interval for a specific time.
Any help would be appreciated.
Looking back to midnight of the current day is the same as looking at the current date with no time component. You can therefore use DATE() to truncate the datetime column date to only the date portion, and compare it to CURDATE().
SELECT * FROM ip_stats WHERE DATE(date) = CURDATE() and ip='$ip'
SELECT * FROM ip_stats WHERE date >= ( NOW() - INTERVAL 20 MINUTE ) AND date >= CURDATE() and ip='$ip'
Just make the column date be bigger than the curdate (which is the starting of this day).
SELECT * FROM ip_stats
WHERE date >= ( NOW() - INTERVAL 20 MINUTE )
and date > CURDATE()
and ip='$ip'
You can use CURDATE() to get rows since midnight of the current day:
SELECT * FROM ip_stats WHERE date >= CURDATE() and ip='$ip'
SELECT *
FROM ip_stats
WHERE date >= GREATEST( NOW() - INTERVAL 20 MINUTE, CURRENT_DATE )
AND ip = '$ip'

HOw to SELECT * (all) which date start from the last day of last month to the first day of next month

use PHP and MySQL and want to use SELECT statement which date_post(datetime variable) start at the last date of last month and to date the first day of next month, help me please.
Thank you in advance.
my database: 'id', 'content', 'image', 'date_post',
etc. and I try to use
$today = getdate();
$thisyear=$today['year'];
$thismon=$today['mon'];
$date_start=$thisyear.'-'.$thismon.'-01';
$date_end=$thisyear.'-'.($thismon+1).'-01';
$sql="SELECT *, DATE_FORMAT(date_post, '%d-%m-%Y') AS datepost
FROM my_table
WHERE date_post BETWEEN date('$date_start')
AND date('$date_end')
ORDER BY date_post DESC";
It makes with one query in MySQL, without any PHP:
SELECT * FROM `table_name`
WHERE DATE(`date_post`) >= DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, CONCAT('%Y-%m-', DAY(LAST_DAY(CURDATE() - INTERVAL 1 MONTH))))
AND DATE(`date_post`) <= DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-01');
Ensuring that the query will not scan the full table but will use the index of date_post (if there is one!):
SELECT *
FROM myTable
WHERE date_post < LAST_DAY(CURDATE())
+ INTERVAL 2 DAY
AND date_Post >= LAST_DAY( LAST_DAY( CURDATE()) - INTERVAL 1 MONTH )
If it is run today ( 2011-07-01 ), it will give all datetimes between 2011-06-31 00:00:00 and 2011-08-01 23:59:59.

Categories