SQL query error using to_char - php

The last line of my query will not run without "not a valid month" error. I am trying to convert a start time in GMT to local time using an offset in one of the columns, then compare this to a date I have in a variable. It works without using the to_char and offset.
Any insight would be appreciated.
AND to_char(CT.START_GMT + K.OFFSET/1440,'MM/DD/YYYY HH24:MM:SS') >= TO_DATE('$From_Date', 'mm/dd/yyyy')

AND CT.START_GMT + K.OFFSET/1440 >= TO_DATE('$From_Date', 'mm/dd/yyyy')
You don't need a TO_CHAR() conversion.. if CT.START_GMT is a DATE
If CT.START_GMT is not DATE/TIMESTAMP , then you need to do TO_DATE() with the right format.

Related

Using INTERVAL in PostgreSQL to add hours date/time

Here is the code in PostgreSQL I am trying to run:
select DATE_ADD(whenbooked,INTERVAL 4 HOUR) from booking WHERE id = 12310;
OR I try to run this code:
select DATE_ADD('2010-11-19 01:11:22',INTERVAL 4 HOUR)
Both codes access date/time stored in the same manner:
2010-11-19 01:11:22
PostgreSQL error code is this:
ERROR: syntax error at or near "4"
it is referring to the '4' in the line 'INTERVAL 4 HOUR'.
I can't figure out what the issue is.
I need to compare a time/stamp (written in exactly the same format as above) with the stored time/date PLUS 4 hours.
So the desired end result is to return: '2010-11-19 05:11:22'.
If this can be done in PHP or directly in SQL?
Based on your comment
I am just typing these codes into PgAdmin.
It looks like you're actually using PostgreSQL not MySQL.
You can view your existing code running on PostgreSQL in action on PostgreSQL here which gives the same error as you get above.
To correctly work with dates in PostgreSQL, you can view a list of date functions on the PostgreSQL documentation site here
What you're trying to do is this:
SELECT TIMESTAMP '2010-11-19 01:11:22' + INTERVAL '4 HOURS';
I found a method that works for me in PHP:
$desiredEndTime = strtotime("2010-11-19 01:11:22 + 10 hours");
echo date("Y-m-d H:i:s", $desiredendtime);
Looks like it converts the date/time into a weird-looking integer. And then on the second line it converts it back into my desired format. Now I can just insert that back into SQL.
Thanks!
Convert it simply to timestamp with "strtotime" method and then you can do whatever you like with that

Query in Microsoft SQL Server using Datetime Convert

I want to know how can I query data using datetime convert:
Example if in ORACLE using :
WHERE TO_CHAR(FQA_END_DATE,'YYYY-MM-DD') = '2014-03-25'
How about in SQL Server ?
WHERE ...
Please help to advice.
Thanks
In sql server you can do something like
WHERE CAST(Date_Column AS DATE) = '20140325' --<-- 'YYYYMMDD' ANSI Standard
or
WHERE CAST(Date_Column AS DATE) = CAST(GETDATE() AS DATE)
If FQA_END_DATE is a datetime datatype; you do not need to make a conversion to characters to compare it to a character string. You just need to provide the character string in an unambigious format, which for example would be the ISO format yyyyMMdd;
so if FQA_END_DATE is a datetime this would work:
WHERE FQA_END_DATE = '20140325'
If however,t hen FQA_END_DATE contains a time portion as well as date, and you're only interested in comparing on date part (and you do not have DATE data type), a good number of methods exists, but a trick is to run it past float;
WHERE CAST(FLOOR(CAST(FQA_END_DATE AS FLOAT)) AS DATETIME) = '20140325'
That converts/removes the time portion from your datetime variable.
The same can also be achieved by using DATEPART or DATEDIFF functions.
Ultimately - you can use the CONVERT to convert both sides of the WHERE into the same date format;
WHERE CONVERT(DATETIME, FQA_END_DATE, 112) = CONVERT(DATETIME, '20140325', 112)
Many many approaches exists to this issue, hopefully this will give some inspiration.
The best advice I can give you is if handling dates as string, to use the ISO format of yyyyMMdd (20140325) because SQL Server can automatically convert that.

How to convert MySQL timestamp to d-m-yyyy

$newTime = "TIME_FORMAT(date, '%e-%M-Y') AS date";
$result = mysqli_query($connect, "SELECT id, firstname, lastname, email, text, $newTime FROM gastenboek");
This is some information selecten from the database, i want to convert the sql timestamp to php days - months - years (22-11-2013).
The query works fine but date = NULL
it seems that my query is wrong, because when i do SELECT * FROM gastenboek it all works fine.
Error code: Notice: Trying to get property of non-object
I already googled the problem but i can't find the solution.
Sorry for my bad english, and thanks in advanced!
Greetings from Holland
You need to use DATE_FORMAT instead of TIME_FORMAT. And I think your format string needs to be tweaked. Try this:
DATE_FORMAT(date, '%e-%c-%Y') AS date
The docs for TIME_FORMAT say:
This is used like the DATE_FORMAT() function, but the format string may contain format specifiers only for hours, minutes, seconds, and microseconds. Other specifiers produce a NULL value or 0.
you have a , (comma) right before FROM (after $newtime)
Assuming the mysql timestamp will be equivalent as NOW() function output, you can use the following method:
SELECT concat(day(now()),"-",month(now()),"-",year(now())) as date
Not the most efficient approach but will get the job done.

Trying to find a record between two dates

My Queries looks like this
WHERE `Project`.`user_id` = 9
AND `Project`.`project_status` = 1
AND `Project`.`project_type_id` = 4
AND `Project`.`approval_date` >= '01/08/2012'
AND `Project`.`approval_date` <= '01/12/2012'
I do have a record that matches this criteria, I actually created the query based on one of the records but it does not return. I really do not want to use the BETWEEN because the application can either use start date or end date
You should convert '01/08/2012' and '01/12/2012' to MySQL date/time values using STR_TO_DATE()
e.g.
STR_TO_DATE('01/08/2012', '%d/%m/%Y')
Well that's really only part of the query -- any, "between" works differently than you imply - here's an excerpt from the MySQL manual:
•expr BETWEEN min AND max If expr is greater than or equal to min and
expr is less than or equal to max, BETWEEN returns 1, otherwise it
returns 0. This is equivalent to the expression (min <= expr AND expr
<= max) if all the arguments are of the same type. Otherwise type
conversion takes place according to the rules described in Section
11.2, “Type Conversion in Expression Evaluation”, but applied to all the three arguments.
All that aside what is the datatype of the "approval_date" column?
This looks as if it should work on first glance, however the values you have supplied are not valid datetimes. Try this instead
WHERE `Project`.`user_id` = 9
AND `Project`.`project_status` = 1
AND `Project`.`project_type_id` = 4
AND `Project`.`approval_date` >= '2012-01-08 00:00:00'
AND `Project`.`approval_date` <= '2012-01-12 00:00:00'
you can still use BETWEEN
WHERE `Project`.`user_id` = 9
AND `Project`.`project_status` = 1
AND `Project`.`project_type_id` = 4
AND (`Project`.`approval_date` BETWEEN '2012-01-08 00:00:00' AND '2012-01-12 23:59:59')
My main problem was getting the string to format into datetime. kissmyface was correct on just structuring it correctly so mysql could understand. So I used php to convert it before sending it out.
"Project.approval_date >= '".date("Y-m-d H:i:s", strtotime($start))."'";
"Project.approval_date <= '".date("Y-m-d H:i:s",strtotime($end))."'";
this was pretty much my solution. unless anyone knows away to do the exact same thing on mysql side.

Formatting an SQL timestamp with PHP

I have a mySQL database with a timestamp field. It currently only has one entry while I'm testing, it is
2010-02-20 13:14:09
I am pulling from the database and using
echo date("m-d-Y",$r['newsDate'])
My end result is showing as
12-31-69
Anyone know why?
Edit:
editedit:
disregard that edit... the FTP addon for notepad++ timed out and unfortunately doesn't display an error when it can't synch.
The date function expects an UNIX timestamp as its second parameter -- which means you have to convert the date you get from the DB to an UNIX timestamp, which can be done using strtotime :
$db = '2010-02-20 13:14:09';
$timestamp = strtotime($db);
echo date("m-d-Y", $timestamp);
And you'll get :
02-20-2010
You were passing the '2010-02-20 13:14:09' string to the date function ; that string is not a valid UNIX Timestamp.
'12-31-69' is probably 1970-01-01, in your locale ; and 1970-01-01 is the Epoch -- the date that corresponds to the 0 UNIX Timestamp.
For starters, the php date() function is expecting seconds as the second variable. So that accounts for why your date is displaying wrong. Check this source on that issue.
Which then provides us the answer to the problem, to get PHP to format the date from a SQL timestamp correctly, we just change the query a tad...
SELECT author, `when`
Change it to...
SELECT author, UNIX_TIMESTAMP(`when`)
Then use the PHP date function, with the variable that is storing the result of that above SQL query.
You could just use MySQL's date_format() function instead:
SELECT date_format(timestampfield, '%m-%d-%Y') FROM table etc....
This will save you having to round-trip your timestamp into unix time and then back into a normal date string in PHP. One datetime formatting call rather than two.
i think this will be useful to newble:
example basic subtraction 1 hour from date from MYSQL format:
$to='2013-25-10 22:56:00'; //curr time
$timestamp = strtotime($to); //convert to Unix timestamp
$timestamp = $timestamp-3600; //subtract 1 hour (3600 this is 1 hour in seconds)
echo date("Y-m-d H:i:s",$timestamp); //show new date
EDIT: After checking, it appears that MySQL returns a timestamp as a string to PHP, so this answer was bogus :)
Anyway, the reason you get a date in 1969 is probably that you're converting a zero unix time from UTC to localtime. The unix time is the number of seconds since 1970. So a value of 0 means 1970. You probaby live in a timezone with a negative offset, like GMT-6, which ends up being 31-12-69.
ok, I was wrestling with this for a week (longer but i took a break from it).
I have two specific fields in tables
creationDate > timestamp > current_timestamp
editDate > timestamp > current_timestamp
they were pulling out either dec 31 1969, or just nothing... annoying... very annoying
in mysql query i did:
unix_timestamp(creationDate) AS creationDate
unix_timestamp(editDate) AS editDate
in php convert i did:
$timestamp = $result_ar['creationDate'];
$creationDate = date("Y-M-d (g:i:s a)", $timestamp)
echo($creationDate);
$editstamp = $result_ar['editDate'];
$editDate = date("Y-M-d (g:i:s a)", $editstamp)
echo($editDate);
this solved my problem for me returning
2010-Jun-28 (5:33:39 pm)
2010-Jun-28 (12:09:46 pm)
respectively.
I hope this helps someone out..

Categories