Different Query Result in SQL Server 2005 - php

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.

Related

PHP auto correct date in specific format

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.

PHP SQL Query to compare DATE from predefined datetime format

My datetime format on my SQL Table looks like this
Table1:
Col1
FromDate
--------------------------
10 August 2017 - 02:10 pm
Col2
ToDate
--------------------------
10 August 2017 - 08:00 pm
What would be the applicable SQL SELECT query and or php code to check if ..
$submitFrdate = "10 August 2017 - 3:00PM";
$submitTodate = "10 August 2017 - 6:00PM";
will be compared to the table using it's DATE only if any of the dates from either the first or second variable collides to the dates of any of the 2 table columns? I understand that I can trim the time stamp using substr() to make it into just 10 August 2017 but the data on the table includes the time. Other than that, I am not sure whats the easiest way to make sure either of the submitted data doesn't hit either of the column's dates REGARDLESS of the time.
Thanks in advance.
MySQL has a function called STR_TO_DATE() which can reverse-format a date string into MySQL's native date format. Then you can do comparisons in the SQL.
Perhaps better solution is to use the DATETIME type for the column. Then you can directly do comparisons with them.

How insert only year in mysql date column

I have a date that sometimes can be only a year, years and month or a full date. What is the best practice to save it on the db?
I need to do search later as: Give me all date between 2004 - 2008 or give me all date between 2004-05-21 to 2005-12-31
I would suggest to use DATE format (https://dev.mysql.com/doc/refman/5.5/en/datetime.html) and then if needed use more specific functions to query records by date ranges like
WHERE YEAR(created) BETWEEN 2010 AND 2011
etc.
More date\time related functions here:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

Selecting Date and Time intervals from MYSQL

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.

Cocoa Core Data to PHP: Converting date

I have dates that are being stored in a database by core data. I then am using php to print out this date information but the date is coming out wrong.
When I store Aug 2, 2009 in core data it comes out in the php as Fri, August 4, 1978. How do I fix the conversion?
I'm guessing a bit here, but the limited evidence fits the hypothesis...
NSDate has an absolute reference date of 1 Jan 2001 (GMT).
PHP time() uses the Unix Epoch date of 1 Jan 1970 (GMT).
It looks like you have an offset of 31 years - or rather 978307200 seconds.
(NSTimeInterval) delta = [[NSDate dateWithTimeIntervalSinceReferenceDate:0] timeIntervalSince1970];
Solution would be to either create your dates in Cocoa with the reference date of 1970, or to add/subtract the offset in Cocoa or PHP.
James

Categories