MySQL query select all were date is equal to today on datetime - php

I have a database table something like this:
Col_1 | Datetime
test1 2015-12-19 09:00:00
test2 2015-12-18 12:30:00
test3 2015-12-19 10:00:00
test4 2015-12-19 16:45:00
I am trying to select in my query all results where the datetime is equal to today's date, although when I simply use something like:
$today = date("Y-m-d");
SELECT * FROM table WHERE datetime = '$today'
It just doesn't seem to work? And if I add the time in to $today it would only select those results which are equal to the exact current time which is not what I want?
Any idea as to how I can do this? I've even gone so far as to this which is totally stupid (Although it was about 3am in the morning when I coded it):
$now = date("Y-m-d H:m:s", strtotime('midnight'));
$todays = date(strtotime('midnight'));
$take24hours = date("Y-m-d H:m:s", strtotime('-24 hours', $todays));
$results = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM table WHERE date_added > '$take24hours' AND date_added < '$now')"));

Can do this entirely in sql transaction ( no need for php date formatting ) :
SELECT * FROM table WHERE date(`datetime`) = current_date;

CURDATE() will get current date.
SELECT * FROM table where DATE(date)=CURDATE()

Related

get todays birth date from db

I am having column DOB which have dob of person and store it in (y-m-d) format. i want to know what will be the query to get today's birth date.
I had tried date(y-m-d) but it only show today's date result.
i.e. if there is 2018-2-20 as dob in my db then it will show in my result but if there is 1995-2-20 this date is not shown as result so basically i just want to display who's birthday is today.
You can try like this
MySQL Query :
SELECT *
FROM
YourTable
WHERE
DATE_FORMAT(BirthDayField,'%m-%d') = DATE_FORMAT(NOW(),'%m-%d')
This query convert your desired date and current date to m(month)-d(day) format and compare
Note : Here BirthDayField is column name with data type DATE
Details about MySQL DATE_FORMAT function
first get the current date.
$today = date("yyyy-mm-dd");
then init the query where you say what should be selected.
$query = sprintf("SELECT * FROM bla WHERE `DOB` = %s", $today);
I guess you try something like this, using a case statement to determine if a record is a current birthday
select *,
case when day( date( dob ) )=day( date( now() ) ) and month( date( dob ) )=month( date( now() ) ) then 1
else 0
end as 'birthday'
from table
You can write like this
SELECT *
FROM persons
WHERE DATE_ADD(birthday,
INTERVAL YEAR(CURDATE())-YEAR(birthday)
+ IF(DAYOFYEAR(CURDATE()) >= DAYOFYEAR(birthday),1,0)
YEAR)
BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 0 DAY);
I think this will help you to getting the users who have b'day today.

year and month in where clause in oracle 10g

I have my date in format March 2014. How do I get month and year in number format to compare it in Oracle 10g query.
<?php
$date = "March 2014";
?>
My query that I am trying to get is
select EMP_NAME where month = "03" and year = "2014".
Here is my table structure
EMP_ID | EMP_NAME | C_DATE
1 | ABC | 01-FEB-14
2 | XYZ | 03-MAR-14
Note: I am writing the coding in php
For a sargable predicate you should avoid altering every row of data to suit the single parameter.
Instead: Adjust the parameter to suit the data
select
*
from your_table
where C_DATE>= to_date('March 2014','MON YYYY', 'NLS_DATE_LANGUAGE = American')
and C_DATE< add_months(to_date('March 2014','MON YYYY', 'NLS_DATE_LANGUAGE = American'),1)
Note the data - C_DATE - is not affected by any function, yet you get the desired outcome (all records of March 2014); this method permits use of indexes on the field C_DATE for query efficiency.
You can try with TO_CHAR(date, 'MON') for the months and TO_CHAR(date, 'YYYY') for the year.
The query will be
select EMP_NAME where TO_CHAR(C_DATE, 'MON') = month and TO_CHAR(C_DATE, 'YYYY') = year
And in php you'll have to manage the date to get year and month and pass them to the query
$dt = new DateTime("March 2014");
$month = $dt->format("m"); //03
$year = $dt->format("Y"); //2014
//Here connect to the db..
$db->query("SELECT EMP_NAME FROM yourTable WHERE TO_CHAR(C_DATE, 'MON') = :month AND TO_CHAR(C_DATE, 'YYYY') = :year");
$db->bindParam(":month", $month);
$db->bindParam(":year", $month);

Show today orders from table

I do not understand how to get output from my sql table .
the table date is stored in int . for example date output from one row is like 1362157869 .
i want to show today orders in query :
php : $today = date("y-m-d", time());
query : SELECT * FROM test WHERE date = '$today'
but it didn't work . i also try this :
SELECT * FROM test WHERE date LIKE '$today'
Query without PHP var:
SELECT * FROM test WHERE date = DATE(NOW());
You are storing your date as a UNIXTIMESTAMP, so you have to convert it to DATETIME using FROM_UNIXTIME, and then you have to extract only the date part, ignoring the time, using the DATE() function:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = CURDATE()
Please see fiddle here. You can then extract yesterday records with something like this:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = CURDATE() - INTERVAL 1 DAY
Or a specific date with this:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = '2013-03-01'
To make use of an index
Assiming that your date column could contain not only the date part but also the time, you could also use this that will return all today records:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP(CURDATE())
and this for yesterday:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP(CURDATE() - INTERVAL 1 DAY)
AND date < UNIX_TIMESTAMP(CURDATE())
or this for a specific date:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP('2013-03-01')
AND date < UNIX_TIMESTAMP('2013-03-01' + INTERVAL 1 DAY)

grab all rows but the last month in sql

Here is a query to get all rows in the past month.
$time = time() - 9676800;
$q = $this->db->query("
select id
from ipAddress
where date > {$time}
");
But how can i adjust this query to get all rows BESIDES the past month? Basically I want to end up deleting all rows over 1 month old
So long as your methodology for calculating "last/past month" satisfies you, then it's simple:
where date <= {$time}
You can use date_format, date_sub functions to get last month's dates.
Find answers here:
mysql last month date statement
and here
MySQL Query to calculate the Previous Month
You can do it like so
$time = strtotime('-1 Month');
$q = $this->db->query("
select id
from ipAddress
where `date` <= {$time}
");
but if date is a TIMESTAMP, DATE, or DATETIME string like 2013-02-27 22:16:38 or 2013-02-27 then you need something like
$time = date('Y-m-d H:i:s', strtotime('-1 Month'));
$q = $this->db->query("
select id
from ipAddress
where `date` <= '{$time}'
");
Or purely in SQL
select id
from ipAddress
where `date` <= DATE_SUB(NOW(), INTERVAL 1 MONTH)
And if I remember correctly, date is a reserved mysql word so use backticks in your sql.

from date to date - search and display

I have a database holding two bits of information. from date to date. I need this entry to appear in my calender on every day, but I can only manage to get to appear on the first and last date.
example of DB:
job_no | date1(from)| date2(to)
________________________________
1 |2013-01-28 | 2013-02-03
2 |2013-01-14 | 2013-01-18
Edit for question. the search bar I have allows for ONE date input and the the calender finds entries through date1 and the next 6 days.
I cannot have a search which contains two date inputs because my users are so used to this way and i do not want to increase searching time. I started to think that I had to find the dates between the dates, add that to an array then use an if statement to find matches...but even saying this makes no sense to me.
regarding job 1, I need my calender to show this job up on all dates 28/29/30/31/01/02/03.
My current search SELECT * FROM jobs WHERE date1='$searchinput' PER day and calender this search. I use strtotime to increase the input date by +1 to add to the search for each day.
Calender page.
What I want with my results. User searched Date 28th.
Mon 28 | Tues 29 | Wed 30 ...... | Fri 03 |
_________________________________________________________________________
Job no 1 | job no 1 | job no 1 ...... | job no 1
What I have now.
Mon 28 | Tues 29 | Wed 30 ...... | Fri 03 |
_________________________________________________________________________
Job no 1 | blank | blank | job no 1
each day has a new select query right now. It matches days with date 1 and date 2. I dont need this as before I only had jobs out on one day now they go out for more than one day and need the job to be noted on all days it is out by only using a job from date and job to date.
EDIT 2:
SELECT * FROM calender_db NATURAL JOIN job_db
WHERE section='two'
AND date1 < '$day' AND date2 > '$day'
OR date1 = '$day' OR date2 = '$day'
This query selects what I need, but as I am using OR the first WHERE CLAUSE can be null. I need that to always be in the clause. I have been looking at using IIF or CASE to rectify but do not how to implement 100%...?
why not use BETWEEN
SELECT * FROM tableName WHERE date BETWEEN 'date1' AND 'date2'
SQLFiddle Demo
UPDATE 1
SELECT *
FROM tableName
WHERE date BETWEEN '2013-01-28' AND '2013-01-28' + INTERVAL 6 DAY
SQLFiddle Demo
To generate a list of days between two dates:
$days = array();
$stop = strtotime($date2);
for ($current = strtotime($date1); $current <= $stop; $current = strtotime('+1 days', $current)) {
$days[] = date('d', $current);
}
echo join('/', $days);
Demo
Update
Misunderstood the question it seems, if both dates are stored as columns and you're querying with a single date:
SELECT *
FROM jobs
WHERE 'date_from_input' BETWEEN date1 AND date2
Update 2
Your latest query can be written as:
SELECT *
FROM calender_db NATURAL JOIN job_db
WHERE section='two' AND '$day' BETWEEN date1 AND date2
Try like this
SELECT * FROM Mytimetable WHERE date BETWEEN 'date1' AND 'date2'
Query between date1 - date2
SELECT *
FROM `objects`
WHERE (date_field BETWEEN '2013-01-30 14:15:55' AND '2013-02-19 10:15:55')
OR
SELECT *
FROM `objects`
WHERE (date_field BETWEEN date1 AND date2)
query to select dates between two dates with PHP variables.
i will explain with exapmle :
$date1 = "2013-02-19";
$date2 = "2013-02-25";
then sql query will be :
$query = " SELECT * FROM table_xyz where (date_item BETWEEN date('".$date1."') AND date('".$date2."')";
hope it solves your problem
SELECT
*
FROM
tablename
WHERE
date between '$date1' and '$date2'
order by
date

Categories