Use Unix timestamp as a condition in mysql - php

I have a table that is similar to the one below. As you can see, I have stored the dates as Unix timestamps. My goal is to only return the results which are for the upcoming dates, not the ones which are in past. How can I achieve this?
id | date
1 1331506800 //Mar 12 2012
2 1366149600 //Apr 17 2013
3 1413928800 //Oct 22 2014
4 1436652000 //Jul 12 2015
Desire result:
id | date
1 1413928800 //Oct 22 2014
2 1436652000 //Jul 12 2015

SELECT *
FROM table
WHERE date > UNIX_TIMESTAMP()

$h->query("SELECT * FROM thetable WHERE date > " . time());
This will also work in other databases than MySQL which do not have UNIX_TIMESTAMP() function.

Try this::
Select * from table
where DATEDIFF(STR_TO_DATE(dateColumn, '%e %b %Y %k:%i')),now())>0

Related

Mysql SELECT by date format as 24 hour

I am trying to select data from a table mysql but I need to format a varchar date column in a military format, but cannot get my desire output my query is below.
"SELECT STR_TO_DATE(`hour`, '%Y-%m-%d %k:%i') AS `time` FROM `dataTable`"
When I tried to echo the time below format appear.
2017-09-21 00:00:00
My desire output is below which is in 24 hour format and removing the seconds, any suggestions would be great.
2017-09-21 18:00
Use the Date_format function of mysql.
"SELECT DATE_FORMAT(`hour`, '%Y-%m-%d %H:%i') AS `time` FROM `dataTable`"
Explanation:
The STR_TO_DATE function always return the date in the format of 2008-09-15 22:23:00
SELECT STR_TO_DATE('Monday 15th September 2008 22:23:00', '%W %D %M %Y %H:%i:%s');
+----------------------------------------------------------------------------+
| STR_TO_DATE('Monday 15th September 2008 22:23:00', '%W %D %M %Y %H:%i:%s') |
+----------------------------------------------------------------------------+
| 2008-09-15 22:23:00 |
+----------------------------------------------------------------------------+
1 row in set (0.00 sec)
Check the above example: the second parameter in STR_TO_DATE function is the format of the parameter 1 to tell the function that in which format parameter 1 is passed in the function.
Reference taken from this link.

How to select a php date() format in SQL

I have an SQL table with a field "time_created" in the form "Wed, 19 Aug 2015 03:58:00 -0600".
I need to check this field against today and perform a where statement according to it, as in
SELECT * FROM table WHERE UNIX_TIMESTAMP() - UNIX_TIMESTAMP(time_created) >= 3600*24*30
to select records where time_created is older then a month.
Problem is UNIX_TIMESTAMP doesn't understand formats as "Wed, 19 Aug 2015 03:58:00 -0600". Any other way?
Try:
SELECT *
FROM table
WHERE DATE(NOW()) > DATE_SUB(STR_TO_DATE(time_created, '%a, %e %b %Y %T'), INTERVAL 1 MONTH)
This will find all records where the current date (DATE(NOW())) is bigger than time_created subtract ` month.

TIME in mysql minus 1 hour

i am selecting the time from a DATETIME field in mysql as a valid RFC2822 dateformat.
But i want mysql to select the date but directly subtract 1 hour of the time.
This is my script now:
$sql_select_bezig = "SELECT
DATE_FORMAT(verzonden_op,'%a, %d %b %Y %T') AS rfc_date,
message_id
FROM
email_tracking
WHERE
DATE(verzonden_op) > DATE_SUB(now(), INTERVAL 2 WEEK)";
Now it is only selecting the date and displays it like Sun, 14 Sep 2014 20:13:33 +0100.
That's ok, but how do i subtract 1 hour?
Select DATE_FORMAT(Date_SUB(date, 2, HOURS)...) AS ... from ...
In a SQL statement, we can substitute an expression for a column name.
Anywhere column name verzonden_op appears in your statement, you can replace that with the expression that VMai suggested...
verzonden_op - INTERVAL 1 HOUR

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

how i group do this mysql query

i want to make charts system
and i think it must be like that
1 jan 2009 = 10 post
2 jan 2009 = 2 post
4 jan 2009 = 10 post
6 jan 2009 = 60 post
and i have posts table that has id,user_id,date
how i can select from posts to show it like that
Try ..
SELECT DATE_FORMAT(`date`, '%e %M %Y') as `post_date`, COUNT(*)
FROM your_table
GROUP BY `post_date`
More information to reference:
DATE_FORMAT
Aggregate Functions

Categories