Background:
I have a database table filled with over 500 rows. The data is from a websites json API. There is a column called created_at which mimics the json API not the local row created_at time. Here is an example of the date / time format from the API 2009-12-10T20:08:00+0000. The column in the database has the type varchar which may be problematic later on.
Problem:
I to wish setup a specific query or if a query cannot be achieved run all of the database rows through a loop. I want the ability to query posts at set date intervals, for instance, todays date is Aug 28th 2011, I want the weekly, monthly, and yearly posts that match that date.
Weekly would look like this (ie. Aug 28th 2011, Aug 21th 2011, Aug 14th 2011, Aug 07th 2011 ).
Monthly would look like this (ie. Aug 28th 2011, Jul 28th 2011,Jun 28th 2011, May 28th 2011 ).
Yearly would look like this (ie. Aug 28th 2011, Aug 28th 2010, Aug 28th 2009 ).
Special cases
If one of these is not present I would like the ability to retrieve whichever preceding or proceeding row date is closer to the date requested. If their are multiple rows for one date I just want the first one.
Any help would be greatly appreciated.
UPDATE
I have added a column with the type datetime and have pulled and updated the rows with the new created_at_datetime column which correspondes to date('Y-m-d H:i:s', $jsonDate). I still have little idea how to form the mysql query.
If you have to convert the date, use strftime(), http://php.net/manual/en/function.strftime.php
Use srttotime() to calculate dates in the past/future; get the previous month and last day of the month, then add a 24:59:59 signaling the last possible second of the month, and a 00:00:00 for the 1st possible second.
To get only 1 result in the database call, use LIMIT 1, and order by asc/desc.
Related
I am stuck with a MySQL table created by another developer. Its a holiday cruise table with fields like destinations, ships and sailing dates (among other fields).
Destinations Ships Sailing Dates
Antarctica MS QE2 2 Dec 2018, 1 Jan 2019
Antarctica MS TBS 2 Feb 2019, 3 Mar 2019
Antarctica MS JRB 9 Nov 2018, 5 Dec 2018
As indicated, each ship can have multiple sailing dates, entered as comma separated text. But I am interested only in the first sailing date of each row.
For example, "2 Dec 2018" is the first date on row one. Likewise, "2 Feb 2019" is the first date on row two, and so on.
I need a PHP SELECT statement to sort this entire table on the first dates of each row. To make things simpler, I thought of adding another date field to the table, and copy the first dates of each row onto that new date field, and then sort the table based on that field. But that is a round-about solution, which I don't prefer.
Is there a simple PHP/SQL statement which can achieve this? I thought of the PHP Explode() function. But I am not sure how I can incorporate that into a SELECT statement. Any help would be highly appreciated.
Update: Thanks for your suggestions. I totally agree, this table is a mess. Let's say I move the dates to a new table, with multiple rows for each row in the old table, like the following.
Destinations Ships Voyage ID
Antarctica MS QE2 1
Antarctica MS TBS 2
Antarctica MS JRB 3
Dates Table
--Voyage ID------Sailing Dates
-------1------------2 Dec 2018
-------1------------1 Jan 2019
-------2------------2 Feb 2019
-------2------------3 Mar 2019
-------3------------9 Nov 2018
-------3------------5 Dec 2018
Now, what SQL command should I use to sort the old table by the first dates of each Voyage ID in the new DATES table.
You are suffering from a horrible database design. Instead of having one table for the ships, one for destinations, and one for actual trips, you have just one table. This is not what a relational database is supposed to be.
Moreover dates are not stored as dates but as strings and there are even multiple dates in one string. It can hardly be worse. So the best advice is to re-write the whole database.
If you don't want to re-design the database or if you are not allowed to, then my advice is: Do not try to tackle this in a SQL query. Simply read all records from the database instead and then do the string manipulation and sorting in PHP.
In my code users post date in content. Which is may be in different formats the format which is used in majority auto sets in MySQL date time field format i.e. yyyy-mm-dd hh:mm:ss. My problem is most of them have typo mistakes or different formats which my code doesn't pick correctly and it returns date something like this 1970-01-01 05:00:00. I am in deep from this issue. Is there any function that auto corrects the date time even if there is a typo mistake in it and if time is not available it auto adds the time to it?
Here are some examples of different formats I get
30 September 2017 | 09 31 AM
29 September 2017 | 02:30 PM
27/07/2016 | 08:20 PM
19/09/2017| 01:32 PM
14-July-2017 03:31 PM
September 5 2017
April 7 2016 04:55 PM
Here is my current PHP code
$get_date = ""; //Date in text form
$show_dated = strtotime(str_replace('|', '', $get_date);
$get_date = date("Y-m-d H:i:s", $show_dated);
echo $get_date;
Try using date_parse() instead of strtotime(). I did this for a while, and it functioned better. Ultimately my solution was to build a custom parser based on the confused mess of user inputs. I eyeballed 2000 entries to develop a 'gold standard' set of results, and then fine-tuned an algorithm to match until it performed 100% correctly.
I save event datetime in db in GMT. How can I show today's events to user?
This particularly is creating issue when date is changed due to timezone conversion.
Example:
User creates an event for 12 Aug 2015 23:45 (this is in his own timezone)
When I save it in db date becomes 13 Aug 2015 (GMT)
Now If I need to show to user all events on 12 Aug. How do I do that? What my queries will be like..
You can use Carbon's copy() function for this.
See the similar question from this thread:
How can I change the timezone of the outputted date in Laravel 4?
I have a table where there is a date field.
The values of the items inserted there are full date names.
How will I be able to sort the selected result by proper date?
Example table
February 10, 2010
January 5, 2010
January 4, 2010
January 5, 2009
January 6, 2010
March 21, 2010
What I want to happen
January 5, 2009
January 4, 2010
January 5, 2010
January 6, 2010
February 10, 2010
March 21, 2010
Is there a mysql function for this?
Here is my query. Date field is named date
$sql = "SELECT * FROM `$table` ORDER BY date";
Unfortunately, this only sorts the result alphabetically.
How should I modify/change the query?
Your future insights and feedback are greatly appreciated! :)
UPDATE
So the problem is that the field is string/varchar. Is it possible to change this after the data has been stored? And would the query above automatically sort it correctly?
This is because the column in the database is setup as a string/varchar, versus a datetime field. There are a few ways to approach this, but the easiest would be to convert your fields to datetime fields instead.
An inefficient solution would be to do:
$sql = "SELECT * FROM `$table` ORDER BY STR_TO_DATE(`date`)";
This will convert your strings to dates, and then sort them, but will have to do this for every row in your database which could get cumbersome. Bottom line: If you're working with datetimes, use a datetime field.
You need to store dates either as a datetime or timestamp field. I would recommend using a timestamp field, that way you can easily format it to whatever you want when retrieving it.
I have a table in SQL Server 2005, and it has column period (datetime).
The value of the column period is 8/7/2009 12:00:00 AM, when I query using php script it give me output Aug 7 2009 12:00AM.
Why it's not 8/7/2009 12:00:00 AM ???
Thank you
Your question reads like this:
The value of the column period is
{ a date, year 2009 month August day 7th at 12:00 AM}
When I query using php script it give me output "Aug 7 2009 12:00AM", i.e. one representation of the date.
Why it's not "8/7/2009 12:00:00 AM", i.e. another representation of the date
A datetime represents a point in time. It is a specific concept. You are comparing two separate representations of the datetime, or in other words, two ways of formatting the datetime value as a textual string.
The issue you are trying to solve is then, "How do I format a datetime as a string in PHP"? For which the answer would be to refer to the date_format (aka DateTime::format) function
To wit, the display specifier for 8/7/2009 12:00:00 AM is n/j/Y h:i:s A.