Help with a MySQL Unix timestamp Query - php

I am trying to create a custom MySQL for use with the Expression Engine CMS. The purpose of the query is to display events that are happening today or in the future.
The problem is that the EE field type that allows you to put in the date and converts it into a unix timestamp. If I pick the 26th July it puts in the date value "25th July 23:00".
As you see from my query below it almost works but I need to add 24 hours onto the values that are used in the conditional part of the statement. I want events that occur on the day "for example today 25th July" to be displayed up until 23:00 hours that day then be removed.
I almost have it I am just stuck on how to add 24 hours to the conditional.
SELECT t.entry_id,
t.title,
t.url_title,
d.field_id_13 AS event_lineup,
d.field_id_14 AS event_details,
d.field_id_15 AS event_day,
d.field_id_16 AS event_flyer_front,
d.field_id_17 AS event_flyer_back,
d.field_id_18 AS event_facebook,
d.field_id_12 AS event_date
FROM `exp_weblog_titles` AS t
NATURAL JOIN `exp_weblog_data` AS d
WHERE d.weblog_id = 5
AND CAST(d.field_id_12 AS UNSIGNED) >= (unix_timestamp(Now()))
ORDER BY d.field_id_12 ASC

What I think might be happening is your timestamps get adjusted for the time zone, and that adjustment is configured differently in the CMS and on the server.

Related

Mysql query or code for time range search

Is there anybody who write query for time range. I am searching from last 6 days when I get the result it will all about dates. Where are times?
I need a solution for searching e.g. from 12:00:00 PM to 04:00:00 PM in datetime field. So how can I search that from mysql table on whatever dates the have. But the result will how on that time range. Any body have solution for that.
Use the TIME() function, it extract the time portion from a datetime field https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_time
SELECT TIME(datetime_col) FROM table
WHERE TIME(datetime_col) BETWEEN '12:00:00' AND '16:00:00'

Get top unique results, getting odd .. results

i'm pretty new to programming and can't seem to figure out my mistake here.
I have a calendar setup, every time a user changes a day I make a new row. This way all changes are logged (again new, id imagine there is a better way.) each row has some displayed information but its primary differentiated by a month a day and a year. IE 1 31 2013 .
I need to get the most recent unique row for each day of the month. So if I run a query for 1/31/2013, I need to return only the most recently created row WHERE month= '1' AND etc.
I'm using..
SELECT t.* FROM(SELECT * FROM calendar a
WHERE NOT EXISTS(SELECT * FROM calendar b
WHERE b.day = a.day AND b.lastaltered > a.lastaltered)) t
WHERE t.month = '12' AND t.year = '2013'
From PHP, if that matters.
Now it works fine if a user makes changes slowly. But I found if someone is quick with it like entering multiple days which end up having a very close time stamp ("lastaltered") it doesn't return that row with my current query. I tested this by modifying the time stamp to a later date and it then returned normally. I hope that explains my problem well enough. I'm still not clear as to why altering the time stamp caused the row to return.
Thank you for your time!
- Jer
Ah ... managing time in a database that doesn't understand what time is (and none of them do).
Go here, read the first book - it explains the intricacies of time and the difficulties of using one data type (DateTime) to represent different concepts:
A fixed instant: 10:15am 4 December 2013 UTC
A recurring instant: 10:15am every day, every Tuesday or the last Monday of a month
An instant defined from an anchor: 2 hours from now
A floating interval: 2 hours
An anchored interval: 2 hours from 10:15am to 12:15pm 4 December 2013 UTC
An instant that does not exist: 2:30 am Sunday 5 October 2014 Australian Eastern Daylight Time
An instant that happens twice 1 hour apart: 2:30am Sunday 6 April 2014 Australian Eastern Daylight Time
... and you get the idea.
The cleanest way to handle your problem is to have a ValidFrom and ValidTo DateTime field, when the user creates the row the ValidFrom is set to now and the ValidTo is set to NULL and a trigger executes that sets all the old entries with a NULL ValidTo to now. This will give a complete audit trail and you can get the current entry by querying for the one with the NULL ValidTo.

Select between two times and dates in SQL PHP

I am trying to find a way to select the row from database which the dates are between yesterday 5p.m. and today 4:59p.m.
The database are filled with orders and I am trying to make it display all the orders starting from yesterday's 5p.m. till today's 4:59p.m. I have to make it display those entries everyday so that my client is able to know what had been ordered today until the cut-off time.
I had found a few but only display time from 0000 - 23:59 while what I need is 17:00 - 16:59.
Is there any way to do so?
EDIT:
The query that I had so far:
"SELECT date FROM xcart_orders WHERE date between '".strtotime(date("F j, Y", time() - 60 * 60 * 24))."' and '".strtotime(date('F j, Y'))."'"
This will return me result between now and yesterday. E.g. Currently is 12PM, it will return me the results from yesterday 12PM till now(12PM).
HOWEVER, what I want is it will always show me the result from yesterday 5PM till today 4:59PM. This is necessary as my client is doing e-commerce and the cut-off time for delivery is 5PM. Hence, he needs to consolidate the orders from last cut-off time to current cut-off time.
My apologies for forgetting to inform about this; the date in the database is in UNIX time stamp, hence, my query has 'strtotime'.
Sorry causing confusion, my English isn't good.
Regards,
FT
Check this and try
$yesterday= date("Y-m-d", time()-86400)." 00:00:00";
$yesterday2= date("Y-m-d", time()-86400)." 23:59:59";
Change the time if you like
Try this
SELECT x FROM y WHERE date between '2014-05-07 08:00' and date_add('2014-05-07 08:00', INTERVAL 1 DAY)
The result should be every data you select from date 2014-05-07 8am to 2017-05-08 7.59am
Hope it helps. =)

SQLite3 Time Problem 11 getting selected before 2

I have a SQLite database that has a time field set as a text type. And it contains values like 11:30 PM, 2:30 PM, etc and I need to select time and date by date & time ASC. I use the following query SELECT * FROM schedule ORDER BY date ASC, time ASC LIMIT 50
But the problem is I get values such as 11:30PM before 2:30PM
How can I avoid this?
Thanks!
the problem is that you are storing in human readable format a value that should be meaningful for a machine. Since sqlite doesn't have a native time type, you'll have to make do with the next nearest approximation, which is a numeric type. You could store both date and time as a single number, for instance as seconds since January 1st, 1970, and then format those values for presentation to users at the last moment.

Archive Builder PHP

Before i start id like to say ive posted this question as more of a discussion rather than Problem Question.
In my Database i have news posts lets say with 3 columns (Id, title, date). Wher Id and title are self Explanitory the date is stored in mktime() values, in other words the number of seconds passed since 1 January 1970.
Now what i want to do is build an archive link that will display as such
July 2009
June 2009
March 2009
Feburary 2009
December 2008
Note the months on which there were no posts are not displayed.
Now as an initial thought i was thinking
Start with the last day of the current Month
And get the Value of the First day of the current Month
Do a MySQL COUNT Query/mysql_num_rows for posts that were date >= First_Day_Seconds AND date <= Last_Day_Seconds
Display or put the values in an Array
Do another Query to Check if Any more values are found WHERE date < First_Day_Seconds (break if no rows were found)
Now the above is just something on the top of my head. But if you got any ideas to speed this process up please share.
Will say in advance, date needs to be in mktime format
I would suggest using a database "native" time format, but it works with UNIX timestamps as well.
You can simply do:
SELECT DISTINCT FROM_UNIXTIME(date, '%M %Y') FROM posts;
Optionally with a WHERE clause limiting the dates to past or future dates. Possibly an ORDER clause thrown in for good measure. That should be pretty much all that's needed, let the database do as much work as possible.
If you need more formatting options, select the dates with "%Y-%m" instead and format them in PHP:
date($myCustomFormat, strtotime("$date-01"));
You can use this query to get years
"SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";
now use can use this query to get month of that year
$tday = date("Y", $datetime);
$s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";

Categories