Get week from unix timestamp retrieved from database - php

I have a MySQL database. In a couple of tables, the information that gets stored needs to be retrievable by week. So, I want to be able to do a SELECT FROM *database* WHERE week = *week*. The problem that I have is that the week part is stored as a unix timestamp (to allow for more versatilty like getting the date and time, just time, etc...).
So the question: How can I retrieve this record WHERE date = *date* when the stored date is a unix timestamp and date I'm matching it against is not?
If my question is too confusing and something needs to be rephrased or said in a clearer manner please comment and let me know.

MySQL has a built-in WEEK() method for handling dates: MySQL WEEK() Reference
Unfortunately however, MySQL's WEEK() method only supports DATE datatypes rather than a UNIX TIMESTAMP. Therefore, we must first convert the timestamp to a date so we can then pass that date to the WEEK() method:
SELECT
*
FROM
my_table
WHERE
WEEK(
DATE_FORMAT(
FROM_UNIXTIME('my_unix_timestamp_col'),
'%e %b %Y'
)
) = 51
If you have a column which is the DATE data-type, the query can be simplified (and can also use indexes):
SELECT * FROM my_table WHERE WEEK(my_date_col) = 51

Related

Select date stored as Unix Timestamp and compare with given Unix TimeStamp

I am stuck for couple of Days on SQL specific scenario. The scenario is as follows,
I have a table, lets call it traffic which has 2 columns -> date and `vehicle (well many more but those are the two I need to match).
The date column is stored as Unix Timestamp. Now this would have been easy to just compare the current date (obtain from php from time() function) however the trick here is that some of these dates have time attached to them also.
For example if you run strtotime(13-02-2017 13:00) and strtotime(13-02-2017) you will get 2 different results. Basically I only care to match the date and not the time.
So I need some way to select the vehicle and date from the database that are equalled to the current Unix Timestamp but with the trick explained above, so I just need to much the date ONLY if possible.
You can use FROM_UNIXTIME() to convert a timestamp to a datetime, and then use the DATE() function to get the date part of that.
WHERE DATE(FROM_UNIXTIME(date)) = CURDATE()
However, this can't use an index, so another way that can make use of an index is to check if it's in a range of timestamps for the current date:
WHERE date BETWEEN UNIX_TIMESTAMP(CURDATE()) AND UNIX_TIMESTAMP(CURDATE()) + 86399
(there are 86400 seconds in a day).
SELECT * FROM traffic WHERE DATE(date) = DATE(CURRENT_TIMESTAMP);

MySQL query: Date > Date not working

Hi there please help me if you can. Here is my senario:
I have a MySQL database with a column that holds a date in the form of a varchar. The format of the date is the following 29/05/2014 (i.e. d/m/Y).
I'm trying to compare the value of this column with todays date and return any rows where the date is earlier than todays date.
I'm using a php variable to store todays as follows:
$date = date("d/m/Y");
Here is my SQL query:
SELECT * FROM patients WHERE last_seen < '$date'
What gets returned
So what is returned is very unusual (to me). All records where the last_seen "day" is less than todays "day". It seems to be overlooking the month and year. So in other words if I last_seen = "30/05/2014" and todays date is "29/05/2014" this record is still returned.
Does anyone have any ideas what I might be doing wrong here?
Thanks
You really, really shouldn't store dates in a varchar field - use date or datetime or timestamp data type.
That said, sometimes you don't have control over the database and you have to deal with somebody else's bad design decision. In this case, to compare dates, convert the varchar strings to dates and compare them that way. So, in your case, you can have something like this:
$date = date("d/m/Y");
and then
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < str_to_date('$date', '%d/%m/%Y')
or simpler
SELECT * FROM patients WHERE date(last_seen) < current_date
This way you are actually comparing dates and not strings containing dates. Naturally, this assumes that all dates are stored in the same format.
EDIT: I just tested the last option - and, apparently, date('30/05/2014') returns NULL on my system (mysql 5.5 on linux), hence I suggest the best way is
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < current_date
You need to store your date as DATE or DATETIME in your database.
Then you can use:
SELECT * FROM patients WHERE DATE(last_seen) < CURRENT_DATE

compare user entered date with the date field stored in database

I am creating a website where user can create entertainment programs on a particular venue.I need to check whether there is any program fixed on that particular venue for the start time and end time entered by the user.
I have 2 column names in database named starttime and 'endtime' which contains the value in the form of yyyy-mm-dd hh:mm:ss. I think I need to convert the value stored in the both fields in database to timestamp. But how to write the query.
eg:
SELECT *
FROM table_name
WHERE
timestamp(starttime)>='user entered value'
AND timestamp(endtime)<='user entered value '
Is this query possible.?
If "user entered value" is unix timestamp, you can use UNIX_TIMESTAMP(field) function to convert that field to timestamp. If it's not, you have to first convert it to timestamp using strtotime or similar function. Or you can go the other way, and use datetime value inside the query by converting string to datetime using STR_TO_DATE(date, format) MySQL function.
For instance, if user enters the date in YYYY-mm-dd hh:mm format, use following query:
SELECT * FROM table_name WHERE starttime >= STR_TO_DATE('2012-02-01 12:00', '%Y-%m-%d %H:%i') AND endtime <= STR_TO_DATE('2013-01-01 00:00', '%Y-%m-%d %H:%i')
Personally I like to use UNIX timestamps inside the query, but that's just my quirk.
Why not use unix time? It makes dealing with dates much simpler.
$current_time=time();
This creates an integer.
Your query:
SELECT * FROM table
WHERE starttime
BETWEEN starttime AND endtime

Mysql select - Get the dates of timestamps

At the mysql table, there are stored values of timestamps (like 1265138145).
What i want is to display the dates (eg 27/2/2011,10/3/2011,15/3/2011, 16/03/2011 etc) which belong to these timestamps. Is this possible?
(but only display one time the date, eg if there is 1265138145 and 1265138140 then display only one time the date - which is 16/3)
There are a variety of ways of doing this, the FROM_UNIXTIME command probably being the easiest.
For example: SELECT FROM_UNIXTIME(<timestamp field>, '%d/%m/%Y');
I'm not sure what you mean about "only display one time the date", but using DISTINCT on the necessary column should help.
i.e.: SELECT DISTINCT(FROM_UNIXTIME(<timestamp field>, '%d/%m/%Y')); may be all you require.
From within MySQL, use ADDDATE and interval of unixtimestamp seconds to the epoch, e.g.
select adddate('1970-01-01', interval 1265138145 second)
then display only one time the date
Use DISTINCT in your query, e.g.
select distinct date(adddate('1970-01-01', interval 1265138145 second))
from tbl ..
Both queries above return the column as a datetime value, which you can apply default formatting to in PHP.
Note about using FROM_UNIXTIME - you get your local UTC offset added to the time, which is unlikely to be what you want, unless the data was populated using UNIX_TIMESTAMP in the first place.
FROM_UNIXTIME: Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone
<?php
print date("d/m/Y", $timestamp);
?>
http://us.php.net/manual/en/function.date.php
Fetch your data and use just date() function.
echo date('d/m/Y', $row['date']);
use FROM_UNIXTIME(unix_timestamp), FROM_UNIXTIME(unix_timestamp,format) http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
in mySQL statement

sql - BETWEEN timeframes with a weird timestamp

I have a column that is populated with a date format that is useful for another application but not the mysql format for timestamps. Here is what it looks like:
2010.01.28 12:00 ("time" column) instead of the mysql timestamp: 2010-01-28 12:00:00 ("updatetime" column).
I need to do a date specific search between two time periods and for a normal mysql timestamp I would run this:
SELECT * FROM table WHERE updatetime BETWEEN '$dateStart' and '$dateEnd'
but this doesn't work with the "time" column formatted as it is. I would prefer to keep that column formatted as such as a different application requires that date format, so does anyone know a MYSQL way to search BETWEEN two timeperiods with the 2010.01.28 12:00 format? Running from a PHP script
You can either format the times in the same format in PHP, or in MySQL
WHERE updatetime BETWEEN
date_format('$datestart','%Y.%m.%d %H:%s')
AND date_format('$dateend','%Y.%m.%d %H:%s')
You might want to try something like this
SELECT * FROM table WHERE STR_TO_DATE(updatetime, "%d.%m.%Y %h:%i")
BETWEEN '$dateStart' and '$dateEnd'
to convert the string into a date - that is if the values of $dateStart and $dateEnd are already in the correct format for comparison with a sql date

Categories